-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
62 lines (58 loc) · 1.69 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const fs = require("fs");
const jwt = require("jsonwebtoken");
const rp = require("request-promise");
const zlib = require("zlib");
// const appId = process.env["GH_APP_ID"];
// const appClientId = process.env["GH_APP_CLIENT_ID"];
// const appClientSecret = process.env["GH_APP_CLIENT_SECRET"];
// const appCallbackUrl = process.env["GH_APP_AUTH_CALLBACK_URL"];
//
async function auth(installationId, appId, pemPath) {
// const pem = fs.readFileSync(pemPath(), "utf-8");
const iat = ~~(new Date().getTime() / 1000) - 5;
const exp = iat + 60 * 8;
const payload = {
iss: appId,
iat,
exp,
};
const token = jwt.sign(payload, fs.readFileSync(pemPath, "utf-8"), { algorithm: "RS256" });
const options = {
method: "POST",
headers: {
"User-Agent": "gh-app-token",
"Authorization": `Bearer ${token}`,
"Accept": "application/vnd.github.machine-man-preview+json",
},
body: {},
url: `https://api.github.com/installations/${installationId}/access_tokens`,
json: true,
};
return rp(options).then(body => {
return body["token"];
});
}
module.exports = {
auth,
}
// export function authWidhCode({ code }: { code: string }) {
// const options = {
// url: "https://github.com/login/oauth/access_token",
// method: "POST",
// headers: {
// "User-Agent": "simple-gh-pr-app-example",
// },
// body: {
// code,
// client_id: appClientId,
// client_secret: appClientSecret,
// redirect_url: appCallbackUrl,
// },
// json: true,
// } as rp.OptionsWithUrl;
// return rp(options).then(x => {
// const token = x.access_token;
// const error = x.error;
// return { token, error };
// });
// }