@terrafirmagreg-team/auth 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/lib/github-oauth.ts +48 -0
- package/src/lib/oauth.ts +7 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrafirmagreg-team/auth",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "GitHub OAuth sign-in and cookie sessions",
|
|
6
6
|
"license": "LGPL-3.0-only",
|
|
@@ -15,11 +15,11 @@
|
|
|
15
15
|
"src"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@terrafirmagreg-team/data": "^1.2.
|
|
19
|
-
"
|
|
18
|
+
"@terrafirmagreg-team/data": "^1.2.5",
|
|
19
|
+
"oauth4webapi": "^3.8.8"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@types/node": "^26.6.
|
|
22
|
+
"@types/node": "^26.6.6"
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as oauth from 'oauth4webapi';
|
|
2
|
+
|
|
3
|
+
const server: oauth.AuthorizationServer = {
|
|
4
|
+
issuer: 'https://github.com',
|
|
5
|
+
authorization_endpoint: 'https://github.com/login/oauth/authorize',
|
|
6
|
+
token_endpoint: 'https://github.com/login/oauth/access_token',
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export interface GitHubOAuth {
|
|
10
|
+
createAuthorizationURL(state: string, scopes: string[]): URL;
|
|
11
|
+
validateAuthorizationCode(code: string): Promise<string>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createGitHubOAuth(
|
|
15
|
+
clientId: string,
|
|
16
|
+
clientSecret: string,
|
|
17
|
+
redirectUri: string,
|
|
18
|
+
): GitHubOAuth {
|
|
19
|
+
const client: oauth.Client = { client_id: clientId };
|
|
20
|
+
const auth = oauth.ClientSecretPost(clientSecret);
|
|
21
|
+
return {
|
|
22
|
+
createAuthorizationURL(state, scopes) {
|
|
23
|
+
const url = new URL(server.authorization_endpoint!);
|
|
24
|
+
url.searchParams.set('client_id', clientId);
|
|
25
|
+
url.searchParams.set('redirect_uri', redirectUri);
|
|
26
|
+
url.searchParams.set('response_type', 'code');
|
|
27
|
+
url.searchParams.set('state', state);
|
|
28
|
+
if (scopes.length > 0) url.searchParams.set('scope', scopes.join(' '));
|
|
29
|
+
return url;
|
|
30
|
+
},
|
|
31
|
+
async validateAuthorizationCode(code) {
|
|
32
|
+
const res = await oauth.authorizationCodeGrantRequest(
|
|
33
|
+
server,
|
|
34
|
+
client,
|
|
35
|
+
auth,
|
|
36
|
+
new URLSearchParams({ code }),
|
|
37
|
+
redirectUri,
|
|
38
|
+
oauth.nopkce,
|
|
39
|
+
);
|
|
40
|
+
const tokens = await oauth.processAuthorizationCodeResponse(
|
|
41
|
+
server,
|
|
42
|
+
client,
|
|
43
|
+
res,
|
|
44
|
+
);
|
|
45
|
+
return tokens.access_token;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
package/src/lib/oauth.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { decrypt, encrypt } from '@terrafirmagreg-team/data';
|
|
2
|
-
import {
|
|
2
|
+
import { createGitHubOAuth, type GitHubOAuth } from './github-oauth';
|
|
3
3
|
import type { AuthConfig } from './config';
|
|
4
4
|
|
|
5
5
|
const STATE_TTL = 10 * 60 * 1000;
|
|
@@ -77,8 +77,8 @@ button:hover{opacity:.85}
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
export function createOAuthFlow(getConfig: () => AuthConfig): OAuthFlow {
|
|
80
|
-
function github(config: AuthConfig, self: string):
|
|
81
|
-
return
|
|
80
|
+
function github(config: AuthConfig, self: string): GitHubOAuth {
|
|
81
|
+
return createGitHubOAuth(
|
|
82
82
|
config.clientId,
|
|
83
83
|
config.clientSecret,
|
|
84
84
|
`${self}${config.callbackPath}`,
|
|
@@ -139,14 +139,16 @@ export function createOAuthFlow(getConfig: () => AuthConfig): OAuthFlow {
|
|
|
139
139
|
if (url.searchParams.get('error') || !code)
|
|
140
140
|
return Response.redirect(returnUrl, 302);
|
|
141
141
|
try {
|
|
142
|
-
const
|
|
142
|
+
const accessToken = await github(config, self).validateAuthorizationCode(
|
|
143
|
+
code,
|
|
144
|
+
);
|
|
143
145
|
return new Response(null, {
|
|
144
146
|
status: 302,
|
|
145
147
|
headers: {
|
|
146
148
|
location: returnUrl,
|
|
147
149
|
'set-cookie': setCookieHeader(
|
|
148
150
|
config.cookieName,
|
|
149
|
-
encrypt(
|
|
151
|
+
encrypt(accessToken, config.tokenSecret),
|
|
150
152
|
COOKIE_MAX_AGE,
|
|
151
153
|
),
|
|
152
154
|
},
|