-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.ts
86 lines (80 loc) · 2.29 KB
/
index.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { getUserAgent } from "universal-user-agent";
import { request as defaultRequest } from "@octokit/request";
import { createOAuthAppAuth } from "@octokit/auth-oauth-app";
import { auth } from "./auth.js";
import { hook } from "./hook.js";
import { getCache } from "./cache.js";
import type { AuthInterface, State, StrategyOptions } from "./types.js";
import { VERSION } from "./version.js";
export { createOAuthUserAuth } from "@octokit/auth-oauth-user";
export type {
// strategy options
StrategyOptions,
// auth options
AppAuthOptions,
OAuthAppAuthOptions,
InstallationAuthOptions,
OAuthWebFlowAuthOptions,
OAuthDeviceFlowAuthOptions,
// authentication objects
Authentication,
AppAuthentication,
OAuthAppAuthentication,
InstallationAccessTokenAuthentication,
GitHubAppUserAuthentication,
GitHubAppUserAuthenticationWithExpiration,
} from "./types.js";
export function createAppAuth(options: StrategyOptions): AuthInterface {
if (!options.appId) {
throw new Error("[@octokit/auth-app] appId option is required");
}
if (!Number.isFinite(+options.appId)) {
throw new Error(
"[@octokit/auth-app] appId option must be a number or numeric string",
);
}
if (!options.privateKey) {
throw new Error("[@octokit/auth-app] privateKey option is required");
}
if ("installationId" in options && !options.installationId) {
throw new Error(
"[@octokit/auth-app] installationId is set to a falsy value",
);
}
const log = Object.assign(
{
warn: console.warn.bind(console),
},
options.log,
);
const request =
options.request ||
defaultRequest.defaults({
headers: {
"user-agent": `octokit-auth-app.js/${VERSION} ${getUserAgent()}`,
},
});
const state: State = Object.assign(
{
request,
cache: getCache(),
},
options,
options.installationId
? { installationId: Number(options.installationId) }
: {},
{
log,
oauthApp: createOAuthAppAuth({
clientType: "github-app",
clientId: options.clientId || "",
clientSecret: options.clientSecret || "",
request,
}),
},
);
// @ts-expect-error not worth the extra code to appease TS
return Object.assign(auth.bind(null, state), {
hook: hook.bind(null, state),
});
}