UNPKG

5.59 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2020 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18const payload_1 = require("./payload");
19function isGitHubCredential(spec) {
20 return !!spec.token && !!spec.scopes;
21}
22exports.isGitHubCredential = isGitHubCredential;
23function isGitHubAppCredential(spec) {
24 return !!spec.token && !!spec.permissions;
25}
26exports.isGitHubAppCredential = isGitHubAppCredential;
27const ResourceUserQuery = `query ResourceUser($id: String!) {
28 ChatId(userId: $id) {
29 person {
30 gitHubId {
31 login
32 credential {
33 secret
34 scopes
35 }
36 }
37 }
38 }
39}
40`;
41function gitHubUserToken() {
42 return async (graph, payload) => {
43 var _a, _b, _c, _d, _e, _f;
44 if (payload_1.isCommandIncoming(payload)) {
45 const chatId = (_c = (_b = (_a = payload.source) === null || _a === void 0 ? void 0 : _a.slack) === null || _b === void 0 ? void 0 : _b.user) === null || _c === void 0 ? void 0 : _c.id;
46 if (chatId) {
47 const response = await graph.query(ResourceUserQuery, { id: chatId });
48 const credential = (_f = (_e = (_d = response === null || response === void 0 ? void 0 : response.ChatId[0]) === null || _d === void 0 ? void 0 : _d.person) === null || _e === void 0 ? void 0 : _e.gitHubId) === null || _f === void 0 ? void 0 : _f.credential;
49 if (credential) {
50 return {
51 scopes: credential.scopes,
52 token: credential.secret,
53 };
54 }
55 }
56 }
57 return undefined;
58 };
59}
60exports.gitHubUserToken = gitHubUserToken;
61const ProviderQuery = `query Provider($owner: String, $repo: String, $apiUrl: String) {
62 Repo(owner: $owner, name: $repo) {
63 org @required {
64 provider(apiUrl: $apiUrl) @required {
65 id
66 }
67 }
68 }
69}
70`;
71const ProviderByRepoIdQuery = `query ProviderByRepoId($id: ID!) {
72 Repo(id: $id) {
73 org @required {
74 provider @required {
75 id
76 }
77 }
78 }
79}
80`;
81const GitHubAppTokenQuery = `query GitHubAppToken($id: ID!, $owner: String!) {
82 GitHubAppResourceProvider(id: $id) {
83 gitHubAppInstallations(owner: $owner) {
84 token {
85 secret
86 permissions
87 }
88 }
89 }
90}
91`;
92const ScmProviderQuery = `query ScmProvider($id: ID!) {
93 SCMProvider(id: $id) {
94 credential {
95 secret
96 scopes
97 }
98 }
99}`;
100function gitHubAppToken(id) {
101 return async (graph) => {
102 var _a, _b, _c, _d, _e, _f, _g, _h;
103 let repo;
104 let owner;
105 let apiUrl;
106 let providerId;
107 if (typeof id === "string") {
108 const provider = await graph.query(ProviderByRepoIdQuery, { id });
109 providerId = (_c = (_b = (_a = provider === null || provider === void 0 ? void 0 : provider.Repo[0]) === null || _a === void 0 ? void 0 : _a.org) === null || _b === void 0 ? void 0 : _b.provider) === null || _c === void 0 ? void 0 : _c.id;
110 }
111 else {
112 repo = id.repo;
113 owner = id.owner;
114 apiUrl = id.apiUrl;
115 const provider = await graph.query(ProviderQuery, { apiUrl: apiUrl || "https://api.github.com/", owner, repo });
116 providerId = (_f = (_e = (_d = provider === null || provider === void 0 ? void 0 : provider.Repo[0]) === null || _d === void 0 ? void 0 : _d.org) === null || _e === void 0 ? void 0 : _e.provider) === null || _f === void 0 ? void 0 : _f.id;
117 }
118 if (providerId) {
119 const installations = await graph.query(GitHubAppTokenQuery, {
120 id: providerId,
121 owner,
122 });
123 const token = (_g = installations === null || installations === void 0 ? void 0 : installations.GitHubAppResourceProvider[0]) === null || _g === void 0 ? void 0 : _g.gitHubAppInstallations[0].token;
124 if (token) {
125 return {
126 token: token.secret,
127 permissions: JSON.parse(token.permissions || ""),
128 };
129 }
130 // Fallback to old SCMProvider for backwards compatibility
131 const scmProvider = await graph.query(ScmProviderQuery, { id: providerId });
132 const credential = (_h = scmProvider === null || scmProvider === void 0 ? void 0 : scmProvider.SCMProvider[0]) === null || _h === void 0 ? void 0 : _h.credential;
133 if (credential) {
134 return {
135 token: credential.secret,
136 scopes: credential.scopes,
137 };
138 }
139 }
140 return undefined;
141 };
142}
143exports.gitHubAppToken = gitHubAppToken;
144class DefaultCredentialProvider {
145 constructor(graphClient, payload) {
146 this.graphClient = graphClient;
147 this.payload = payload;
148 }
149 async resolve(spec) {
150 return spec(this.graphClient, this.payload);
151 }
152}
153exports.DefaultCredentialProvider = DefaultCredentialProvider;
154//# sourceMappingURL=secrets.js.map
\No newline at end of file