/
get-oauth-authentication.ts
47 lines (42 loc) · 1.22 KB
/
get-oauth-authentication.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
import {
RequestInterface,
OAuthOptions,
StrategyOptionsWithDefaults,
OAuthAccesTokenAuthentication
} from "./types";
export async function getOAuthAuthentication(
state: StrategyOptionsWithDefaults,
options: OAuthOptions,
customRequest?: RequestInterface
): Promise<OAuthAccesTokenAuthentication> {
const request = customRequest || state.request;
// The "/login/oauth/access_token" is not part of the REST API hosted on api.github.com,
// instead it’s using the github.com domain.
const route = /^https:\/\/(api\.)?github\.com$/.test(
state.request.endpoint.DEFAULTS.baseUrl
)
? "POST https://github.com/login/oauth/access_token"
: `POST ${state.request.endpoint.DEFAULTS.baseUrl.replace(
"/api/v3",
"/login/oauth/access_token"
)}`;
const parameters = {
headers: {
accept: `application/json`
},
client_id: state.clientId,
client_secret: state.clientSecret,
code: options.code,
state: options.state,
redirect_uri: options.redirectUrl
};
const {
data: { access_token: token, scope }
} = await request(route, parameters);
return {
type: "token",
tokenType: "oauth",
token,
scopes: scope.split(/,\s*/).filter(Boolean)
};
}