@stonyx/oauth 0.1.1-beta.4 → 0.1.1-beta.41
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/README.md +4 -0
- package/package.json +10 -4
- package/src/main.js +4 -0
- package/.github/workflows/ci.yml +0 -16
- package/.github/workflows/publish.yml +0 -51
- package/test/config/environment.js +0 -18
- package/test/integration/oauth-test.js +0 -149
- package/test/sample/providers/mock.js +0 -40
- package/test/sample/requests/.gitkeep +0 -0
- package/test/unit/oauth-flow-test.js +0 -137
- package/test/unit/providers/discord-test.js +0 -115
- package/test/unit/session-manager-test.js +0 -85
- package/test/unit/state-validation-test.js +0 -118
- package/test/unit/token-manager-test.js +0 -76
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
[](https://github.com/abofs/stonyx-oauth/actions/workflows/ci.yml)
|
|
2
|
+
[](https://www.npmjs.com/package/@stonyx/oauth)
|
|
3
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
4
|
+
|
|
1
5
|
# @stonyx/oauth
|
|
2
6
|
|
|
3
7
|
OAuth2 authentication module for the Stonyx framework. Provides a generic OAuth2 Authorization Code flow with a provider pattern — ship with Discord support, extensible to any OAuth2 provider.
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"stonyx-async",
|
|
5
5
|
"stonyx-module"
|
|
6
6
|
],
|
|
7
|
-
"version": "0.1.1-beta.
|
|
7
|
+
"version": "0.1.1-beta.41",
|
|
8
8
|
"description": "OAuth2 authentication module for the Stonyx framework",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -20,19 +20,25 @@
|
|
|
20
20
|
"contributors": [
|
|
21
21
|
"Stone Costa <stone.costa@synamicd.com>"
|
|
22
22
|
],
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"config",
|
|
26
|
+
"README.md"
|
|
27
|
+
],
|
|
23
28
|
"publishConfig": {
|
|
24
29
|
"access": "public",
|
|
25
30
|
"provenance": true
|
|
26
31
|
},
|
|
27
32
|
"dependencies": {
|
|
28
|
-
"stonyx": "0.2.3-beta.
|
|
33
|
+
"stonyx": "0.2.3-beta.11",
|
|
34
|
+
"@stonyx/events": "0.1.1-beta.9"
|
|
29
35
|
},
|
|
30
36
|
"peerDependencies": {
|
|
31
37
|
"@stonyx/rest-server": ">=0.2.1-beta.11"
|
|
32
38
|
},
|
|
33
39
|
"devDependencies": {
|
|
34
|
-
"@stonyx/rest-server": "0.2.1-beta.
|
|
35
|
-
"@stonyx/utils": "0.2.3-beta.
|
|
40
|
+
"@stonyx/rest-server": "0.2.1-beta.30",
|
|
41
|
+
"@stonyx/utils": "0.2.3-beta.7",
|
|
36
42
|
"qunit": "^2.24.1",
|
|
37
43
|
"sinon": "^21.0.0"
|
|
38
44
|
},
|
package/src/main.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import config from 'stonyx/config';
|
|
2
2
|
import log from 'stonyx/log';
|
|
3
3
|
import { waitForModule } from 'stonyx';
|
|
4
|
+
import { setup, emit } from '@stonyx/events';
|
|
4
5
|
import RestServer from '@stonyx/rest-server';
|
|
5
6
|
import TokenManager from './token-manager.js';
|
|
6
7
|
import SessionManager from './session-manager.js';
|
|
7
8
|
import AuthRequest from './auth-request.js';
|
|
8
9
|
|
|
10
|
+
setup(['authenticate']);
|
|
11
|
+
|
|
9
12
|
export default class OAuth {
|
|
10
13
|
providers = new Map();
|
|
11
14
|
pendingStates = new Map();
|
|
@@ -66,6 +69,7 @@ export default class OAuth {
|
|
|
66
69
|
const tokens = await tokenManager.getTokens(code);
|
|
67
70
|
const rawUser = await flow.fetchUserInfo(tokens.accessToken);
|
|
68
71
|
const user = flow.normalizeUser(rawUser);
|
|
72
|
+
await emit('authenticate', user);
|
|
69
73
|
return this.sessionManager.create(user, tokens);
|
|
70
74
|
}
|
|
71
75
|
|
package/.github/workflows/ci.yml
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
name: CI
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
pull_request:
|
|
5
|
-
branches: [dev, main]
|
|
6
|
-
|
|
7
|
-
concurrency:
|
|
8
|
-
group: ci-${{ github.head_ref || github.ref }}
|
|
9
|
-
cancel-in-progress: true
|
|
10
|
-
|
|
11
|
-
permissions:
|
|
12
|
-
contents: read
|
|
13
|
-
|
|
14
|
-
jobs:
|
|
15
|
-
test:
|
|
16
|
-
uses: abofs/stonyx-workflows/.github/workflows/ci.yml@main
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
name: Publish to NPM
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
repository_dispatch:
|
|
5
|
-
types: [cascade-publish]
|
|
6
|
-
workflow_dispatch:
|
|
7
|
-
inputs:
|
|
8
|
-
version-type:
|
|
9
|
-
description: 'Version type'
|
|
10
|
-
required: true
|
|
11
|
-
type: choice
|
|
12
|
-
options:
|
|
13
|
-
- patch
|
|
14
|
-
- minor
|
|
15
|
-
- major
|
|
16
|
-
custom-version:
|
|
17
|
-
description: 'Custom version (optional, overrides version-type)'
|
|
18
|
-
required: false
|
|
19
|
-
type: string
|
|
20
|
-
pull_request:
|
|
21
|
-
types: [opened, synchronize, reopened]
|
|
22
|
-
branches: [main]
|
|
23
|
-
push:
|
|
24
|
-
branches: [main]
|
|
25
|
-
|
|
26
|
-
concurrency:
|
|
27
|
-
group: ${{ github.event_name == 'repository_dispatch' && 'cascade-update' || format('publish-{0}', github.ref) }}
|
|
28
|
-
cancel-in-progress: false
|
|
29
|
-
|
|
30
|
-
permissions:
|
|
31
|
-
contents: write
|
|
32
|
-
id-token: write
|
|
33
|
-
pull-requests: write
|
|
34
|
-
|
|
35
|
-
jobs:
|
|
36
|
-
publish:
|
|
37
|
-
if: "!contains(github.event.head_commit.message, '[skip ci]')"
|
|
38
|
-
uses: abofs/stonyx-workflows/.github/workflows/npm-publish.yml@main
|
|
39
|
-
with:
|
|
40
|
-
version-type: ${{ github.event.inputs.version-type }}
|
|
41
|
-
custom-version: ${{ github.event.inputs.custom-version }}
|
|
42
|
-
cascade-source: ${{ github.event.client_payload.source_package || '' }}
|
|
43
|
-
secrets: inherit
|
|
44
|
-
|
|
45
|
-
cascade:
|
|
46
|
-
needs: publish
|
|
47
|
-
uses: abofs/stonyx-workflows/.github/workflows/cascade.yml@main
|
|
48
|
-
with:
|
|
49
|
-
package-name: ${{ needs.publish.outputs.package-name }}
|
|
50
|
-
published-version: ${{ needs.publish.outputs.published-version }}
|
|
51
|
-
secrets: inherit
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
restServer: {
|
|
3
|
-
dir: './test/sample/requests',
|
|
4
|
-
},
|
|
5
|
-
oauth: {
|
|
6
|
-
providers: {
|
|
7
|
-
mock: {
|
|
8
|
-
clientId: 'test-client-id',
|
|
9
|
-
clientSecret: 'test-client-secret',
|
|
10
|
-
redirectUri: 'http://localhost:2666/auth/callback/mock',
|
|
11
|
-
scopes: ['identify'],
|
|
12
|
-
module: './test/sample/providers/mock.js',
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
sessionDuration: 3600,
|
|
16
|
-
frontendCallbackUrl: 'http://localhost:4200/auth/callback',
|
|
17
|
-
}
|
|
18
|
-
};
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import QUnit from 'qunit';
|
|
2
|
-
import RestServer from '@stonyx/rest-server';
|
|
3
|
-
import config from 'stonyx/config';
|
|
4
|
-
import { setupIntegrationTests } from 'stonyx/test-helpers';
|
|
5
|
-
import OAuth from '../../src/main.js';
|
|
6
|
-
|
|
7
|
-
const { module, test } = QUnit;
|
|
8
|
-
let endpoint;
|
|
9
|
-
|
|
10
|
-
async function getValidState(endpoint) {
|
|
11
|
-
const loginResponse = await fetch(`${endpoint}/auth/login/mock`, { redirect: 'manual' });
|
|
12
|
-
const location = loginResponse.headers.get('location');
|
|
13
|
-
const url = new URL(location);
|
|
14
|
-
return url.searchParams.get('state');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
module('[Integration] OAuth', function(hooks) {
|
|
18
|
-
setupIntegrationTests(hooks);
|
|
19
|
-
|
|
20
|
-
hooks.before(function() {
|
|
21
|
-
endpoint = `http://localhost:${config.restServer.port}`;
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
hooks.after(function() {
|
|
25
|
-
RestServer.close();
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('GET /auth/login/mock redirects to provider auth URL', async function(assert) {
|
|
29
|
-
const response = await fetch(`${endpoint}/auth/login/mock`, { redirect: 'manual' });
|
|
30
|
-
|
|
31
|
-
assert.equal(response.status, 302);
|
|
32
|
-
|
|
33
|
-
const location = response.headers.get('location');
|
|
34
|
-
assert.ok(location.startsWith('https://mock.provider/oauth/authorize?'));
|
|
35
|
-
assert.ok(location.includes('client_id=test-client-id'));
|
|
36
|
-
assert.ok(location.includes('response_type=code'));
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('GET /auth/login/nonexistent returns 404', async function(assert) {
|
|
40
|
-
const response = await fetch(`${endpoint}/auth/login/nonexistent`, { redirect: 'manual' });
|
|
41
|
-
|
|
42
|
-
assert.equal(response.status, 404);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('GET /auth/callback/mock with valid state redirects to frontend with session', async function(assert) {
|
|
46
|
-
const stateToken = await getValidState(endpoint);
|
|
47
|
-
const response = await fetch(`${endpoint}/auth/callback/mock?code=test-auth-code&state=${stateToken}`, { redirect: 'manual' });
|
|
48
|
-
|
|
49
|
-
assert.equal(response.status, 302);
|
|
50
|
-
|
|
51
|
-
const location = response.headers.get('location');
|
|
52
|
-
const redirectUrl = new URL(location);
|
|
53
|
-
assert.equal(redirectUrl.origin + redirectUrl.pathname, 'http://localhost:4200/auth/callback');
|
|
54
|
-
assert.ok(redirectUrl.searchParams.get('sessionId'), 'redirect includes sessionId');
|
|
55
|
-
assert.ok(redirectUrl.searchParams.get('expiresAt'), 'redirect includes expiresAt');
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('GET /auth with valid session returns user', async function(assert) {
|
|
59
|
-
const stateToken = await getValidState(endpoint);
|
|
60
|
-
const callbackResponse = await fetch(`${endpoint}/auth/callback/mock?code=test-code&state=${stateToken}`, { redirect: 'manual' });
|
|
61
|
-
const location = callbackResponse.headers.get('location');
|
|
62
|
-
const sessionId = new URL(location).searchParams.get('sessionId');
|
|
63
|
-
|
|
64
|
-
const response = await fetch(`${endpoint}/auth`, {
|
|
65
|
-
headers: { 'session-id': sessionId },
|
|
66
|
-
});
|
|
67
|
-
const data = await response.json();
|
|
68
|
-
|
|
69
|
-
assert.equal(response.status, 200);
|
|
70
|
-
assert.equal(data.id, 'mock-user-123');
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test('GET /auth without session returns 401', async function(assert) {
|
|
74
|
-
const response = await fetch(`${endpoint}/auth`);
|
|
75
|
-
|
|
76
|
-
assert.equal(response.status, 401);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test('GET /auth with invalid session returns 401', async function(assert) {
|
|
80
|
-
const response = await fetch(`${endpoint}/auth`, {
|
|
81
|
-
headers: { 'session-id': 'invalid-session' },
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
assert.equal(response.status, 401);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('GET /auth/logout invalidates session', async function(assert) {
|
|
88
|
-
const stateToken = await getValidState(endpoint);
|
|
89
|
-
const callbackResponse = await fetch(`${endpoint}/auth/callback/mock?code=test-code&state=${stateToken}`, { redirect: 'manual' });
|
|
90
|
-
const location = callbackResponse.headers.get('location');
|
|
91
|
-
const sessionId = new URL(location).searchParams.get('sessionId');
|
|
92
|
-
|
|
93
|
-
// Logout
|
|
94
|
-
const logoutResponse = await fetch(`${endpoint}/auth/logout`, {
|
|
95
|
-
headers: { 'session-id': sessionId },
|
|
96
|
-
});
|
|
97
|
-
assert.equal(logoutResponse.status, 200);
|
|
98
|
-
|
|
99
|
-
// Verify session is invalid
|
|
100
|
-
const authResponse = await fetch(`${endpoint}/auth`, {
|
|
101
|
-
headers: { 'session-id': sessionId },
|
|
102
|
-
});
|
|
103
|
-
assert.equal(authResponse.status, 401);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test('GET /auth/callback/mock rejects missing state token', async function(assert) {
|
|
107
|
-
const response = await fetch(`${endpoint}/auth/callback/mock?code=test-auth-code`, { redirect: 'manual' });
|
|
108
|
-
|
|
109
|
-
assert.equal(response.status, 302);
|
|
110
|
-
const location = response.headers.get('location');
|
|
111
|
-
const redirectUrl = new URL(location);
|
|
112
|
-
assert.equal(redirectUrl.searchParams.get('error'), 'auth_failed');
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test('GET /auth/callback/mock rejects invalid state token', async function(assert) {
|
|
116
|
-
const response = await fetch(`${endpoint}/auth/callback/mock?code=test-auth-code&state=bogus-state`, { redirect: 'manual' });
|
|
117
|
-
|
|
118
|
-
assert.equal(response.status, 302);
|
|
119
|
-
const location = response.headers.get('location');
|
|
120
|
-
const redirectUrl = new URL(location);
|
|
121
|
-
assert.equal(redirectUrl.searchParams.get('error'), 'auth_failed');
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test('GET /auth/callback/mock with error param redirects with error', async function(assert) {
|
|
125
|
-
const response = await fetch(`${endpoint}/auth/callback/mock?error=access_denied`, { redirect: 'manual' });
|
|
126
|
-
|
|
127
|
-
assert.equal(response.status, 302);
|
|
128
|
-
const location = response.headers.get('location');
|
|
129
|
-
const redirectUrl = new URL(location);
|
|
130
|
-
assert.equal(redirectUrl.origin + redirectUrl.pathname, 'http://localhost:4200/auth/callback');
|
|
131
|
-
assert.equal(redirectUrl.searchParams.get('error'), 'access_denied');
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
test('GET /auth/callback/mock state token cannot be reused', async function(assert) {
|
|
135
|
-
const stateToken = await getValidState(endpoint);
|
|
136
|
-
|
|
137
|
-
// First use succeeds
|
|
138
|
-
const first = await fetch(`${endpoint}/auth/callback/mock?code=test-code&state=${stateToken}`, { redirect: 'manual' });
|
|
139
|
-
assert.equal(first.status, 302);
|
|
140
|
-
const firstLocation = new URL(first.headers.get('location'));
|
|
141
|
-
assert.ok(firstLocation.searchParams.get('sessionId'), 'first use succeeds');
|
|
142
|
-
|
|
143
|
-
// Second use fails
|
|
144
|
-
const second = await fetch(`${endpoint}/auth/callback/mock?code=test-code&state=${stateToken}`, { redirect: 'manual' });
|
|
145
|
-
assert.equal(second.status, 302);
|
|
146
|
-
const secondLocation = new URL(second.headers.get('location'));
|
|
147
|
-
assert.equal(secondLocation.searchParams.get('error'), 'auth_failed', 'reuse is rejected');
|
|
148
|
-
});
|
|
149
|
-
});
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import OAuthFlow from '../../../src/oauth-flow.js';
|
|
2
|
-
|
|
3
|
-
export default class MockProvider extends OAuthFlow {
|
|
4
|
-
constructor(config) {
|
|
5
|
-
super({
|
|
6
|
-
...config,
|
|
7
|
-
authorizationUrl: 'https://mock.provider/oauth/authorize',
|
|
8
|
-
tokenUrl: 'https://mock.provider/oauth/token',
|
|
9
|
-
userInfoUrl: 'https://mock.provider/api/me',
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async exchangeCode(_code) {
|
|
14
|
-
return {
|
|
15
|
-
accessToken: 'mock-access-token',
|
|
16
|
-
refreshToken: 'mock-refresh-token',
|
|
17
|
-
expiresIn: 3600,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async fetchUserInfo(_accessToken) {
|
|
22
|
-
return {
|
|
23
|
-
id: 'mock-user-123',
|
|
24
|
-
username: 'mockuser',
|
|
25
|
-
displayName: 'Mock User',
|
|
26
|
-
email: 'mock@test.com',
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
normalizeUser(rawUser) {
|
|
31
|
-
return {
|
|
32
|
-
id: rawUser.id,
|
|
33
|
-
username: rawUser.username,
|
|
34
|
-
displayName: rawUser.displayName,
|
|
35
|
-
avatar: null,
|
|
36
|
-
email: rawUser.email,
|
|
37
|
-
raw: rawUser,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
}
|
|
File without changes
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import QUnit from 'qunit';
|
|
2
|
-
import OAuthFlow from '../../src/oauth-flow.js';
|
|
3
|
-
|
|
4
|
-
const { module, test } = QUnit;
|
|
5
|
-
|
|
6
|
-
const defaultConfig = {
|
|
7
|
-
clientId: 'test-client',
|
|
8
|
-
clientSecret: 'test-secret',
|
|
9
|
-
redirectUri: 'http://localhost/callback',
|
|
10
|
-
scopes: ['read', 'write'],
|
|
11
|
-
authorizationUrl: 'https://provider.com/oauth/authorize',
|
|
12
|
-
tokenUrl: 'https://provider.com/oauth/token',
|
|
13
|
-
userInfoUrl: 'https://provider.com/api/me',
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
module('[Unit] OAuthFlow', function() {
|
|
17
|
-
module('buildAuthorizationUrl', function() {
|
|
18
|
-
test('generates a valid authorization URL with all params', function(assert) {
|
|
19
|
-
const flow = new OAuthFlow(defaultConfig);
|
|
20
|
-
const url = flow.buildAuthorizationUrl('test-state-123');
|
|
21
|
-
|
|
22
|
-
assert.ok(url.startsWith('https://provider.com/oauth/authorize?'));
|
|
23
|
-
assert.ok(url.includes('client_id=test-client'));
|
|
24
|
-
assert.ok(url.includes('redirect_uri='));
|
|
25
|
-
assert.ok(url.includes('response_type=code'));
|
|
26
|
-
assert.ok(url.includes('scope=read+write'));
|
|
27
|
-
assert.ok(url.includes('state=test-state-123'));
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('handles empty scopes', function(assert) {
|
|
31
|
-
const flow = new OAuthFlow({ ...defaultConfig, scopes: [] });
|
|
32
|
-
const url = flow.buildAuthorizationUrl('state');
|
|
33
|
-
|
|
34
|
-
assert.ok(url.includes('scope='));
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
module('exchangeCode', function() {
|
|
39
|
-
test('makes a POST request to the token URL', async function(assert) {
|
|
40
|
-
const flow = new OAuthFlow(defaultConfig);
|
|
41
|
-
const originalFetch = globalThis.fetch;
|
|
42
|
-
|
|
43
|
-
globalThis.fetch = async (url, options) => {
|
|
44
|
-
assert.equal(url, 'https://provider.com/oauth/token');
|
|
45
|
-
assert.equal(options.method, 'POST');
|
|
46
|
-
assert.equal(options.headers['Content-Type'], 'application/json');
|
|
47
|
-
|
|
48
|
-
const body = JSON.parse(options.body);
|
|
49
|
-
assert.equal(body.grant_type, 'authorization_code');
|
|
50
|
-
assert.equal(body.code, 'test-code');
|
|
51
|
-
assert.equal(body.client_id, 'test-client');
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
ok: true,
|
|
55
|
-
json: async () => ({ access_token: 'abc', refresh_token: 'def', expires_in: 3600 }),
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const result = await flow.exchangeCode('test-code');
|
|
60
|
-
|
|
61
|
-
assert.equal(result.accessToken, 'abc');
|
|
62
|
-
assert.equal(result.refreshToken, 'def');
|
|
63
|
-
assert.equal(result.expiresIn, 3600);
|
|
64
|
-
|
|
65
|
-
globalThis.fetch = originalFetch;
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test('throws on failed token exchange', async function(assert) {
|
|
69
|
-
const flow = new OAuthFlow(defaultConfig);
|
|
70
|
-
const originalFetch = globalThis.fetch;
|
|
71
|
-
|
|
72
|
-
globalThis.fetch = async () => ({ ok: false, status: 400 });
|
|
73
|
-
|
|
74
|
-
await assert.rejects(flow.exchangeCode('bad-code'), /Token exchange failed: 400/);
|
|
75
|
-
|
|
76
|
-
globalThis.fetch = originalFetch;
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
module('refreshAccessToken', function() {
|
|
81
|
-
test('makes a refresh grant request', async function(assert) {
|
|
82
|
-
const flow = new OAuthFlow(defaultConfig);
|
|
83
|
-
const originalFetch = globalThis.fetch;
|
|
84
|
-
|
|
85
|
-
globalThis.fetch = async (_url, options) => {
|
|
86
|
-
const body = JSON.parse(options.body);
|
|
87
|
-
assert.equal(body.grant_type, 'refresh_token');
|
|
88
|
-
assert.equal(body.refresh_token, 'old-refresh');
|
|
89
|
-
|
|
90
|
-
return {
|
|
91
|
-
ok: true,
|
|
92
|
-
json: async () => ({ access_token: 'new-access', expires_in: 7200 }),
|
|
93
|
-
};
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const result = await flow.refreshAccessToken('old-refresh');
|
|
97
|
-
|
|
98
|
-
assert.equal(result.accessToken, 'new-access');
|
|
99
|
-
assert.equal(result.refreshToken, 'old-refresh', 'falls back to original refresh token');
|
|
100
|
-
assert.equal(result.expiresIn, 7200);
|
|
101
|
-
|
|
102
|
-
globalThis.fetch = originalFetch;
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
module('fetchUserInfo', function() {
|
|
107
|
-
test('sends a Bearer token in the Authorization header', async function(assert) {
|
|
108
|
-
const flow = new OAuthFlow(defaultConfig);
|
|
109
|
-
const originalFetch = globalThis.fetch;
|
|
110
|
-
|
|
111
|
-
globalThis.fetch = async (url, options) => {
|
|
112
|
-
assert.equal(url, 'https://provider.com/api/me');
|
|
113
|
-
assert.equal(options.headers.Authorization, 'Bearer my-token');
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
ok: true,
|
|
117
|
-
json: async () => ({ id: '1', name: 'Test' }),
|
|
118
|
-
};
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const result = await flow.fetchUserInfo('my-token');
|
|
122
|
-
assert.deepEqual(result, { id: '1', name: 'Test' });
|
|
123
|
-
|
|
124
|
-
globalThis.fetch = originalFetch;
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
module('normalizeUser', function() {
|
|
129
|
-
test('returns raw user by default', function(assert) {
|
|
130
|
-
const flow = new OAuthFlow(defaultConfig);
|
|
131
|
-
const raw = { id: '1', name: 'Test' };
|
|
132
|
-
const result = flow.normalizeUser(raw);
|
|
133
|
-
|
|
134
|
-
assert.deepEqual(result, { raw });
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
});
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import QUnit from 'qunit';
|
|
2
|
-
import DiscordProvider from '../../../src/providers/discord.js';
|
|
3
|
-
|
|
4
|
-
const { module, test } = QUnit;
|
|
5
|
-
|
|
6
|
-
const defaultConfig = {
|
|
7
|
-
clientId: 'discord-client-id',
|
|
8
|
-
clientSecret: 'discord-client-secret',
|
|
9
|
-
redirectUri: 'http://localhost/auth/callback/discord',
|
|
10
|
-
scopes: ['identify', 'email'],
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
module('[Unit] DiscordProvider', function() {
|
|
14
|
-
module('constructor', function() {
|
|
15
|
-
test('sets correct Discord OAuth2 URLs', function(assert) {
|
|
16
|
-
const provider = new DiscordProvider(defaultConfig);
|
|
17
|
-
|
|
18
|
-
assert.equal(provider.authorizationUrl, 'https://discord.com/oauth2/authorize');
|
|
19
|
-
assert.equal(provider.tokenUrl, 'https://discord.com/api/oauth2/token');
|
|
20
|
-
assert.equal(provider.userInfoUrl, 'https://discord.com/api/users/@me');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test('preserves client config', function(assert) {
|
|
24
|
-
const provider = new DiscordProvider(defaultConfig);
|
|
25
|
-
|
|
26
|
-
assert.equal(provider.clientId, 'discord-client-id');
|
|
27
|
-
assert.equal(provider.clientSecret, 'discord-client-secret');
|
|
28
|
-
assert.deepEqual(provider.scopes, ['identify', 'email']);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
module('exchangeCode', function() {
|
|
33
|
-
test('uses form-encoded body for Discord token exchange', async function(assert) {
|
|
34
|
-
const provider = new DiscordProvider(defaultConfig);
|
|
35
|
-
const originalFetch = globalThis.fetch;
|
|
36
|
-
|
|
37
|
-
globalThis.fetch = async (url, options) => {
|
|
38
|
-
assert.equal(url, 'https://discord.com/api/oauth2/token');
|
|
39
|
-
assert.equal(options.headers['Content-Type'], 'application/x-www-form-urlencoded');
|
|
40
|
-
assert.ok(options.body instanceof URLSearchParams);
|
|
41
|
-
|
|
42
|
-
const params = Object.fromEntries(options.body);
|
|
43
|
-
assert.equal(params.grant_type, 'authorization_code');
|
|
44
|
-
assert.equal(params.code, 'discord-auth-code');
|
|
45
|
-
assert.equal(params.client_id, 'discord-client-id');
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
ok: true,
|
|
49
|
-
json: async () => ({ access_token: 'discord-token', refresh_token: 'refresh', expires_in: 604800 }),
|
|
50
|
-
};
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const result = await provider.exchangeCode('discord-auth-code');
|
|
54
|
-
|
|
55
|
-
assert.equal(result.accessToken, 'discord-token');
|
|
56
|
-
assert.equal(result.refreshToken, 'refresh');
|
|
57
|
-
assert.equal(result.expiresIn, 604800);
|
|
58
|
-
|
|
59
|
-
globalThis.fetch = originalFetch;
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
module('normalizeUser', function() {
|
|
64
|
-
test('maps Discord user fields correctly', function(assert) {
|
|
65
|
-
const provider = new DiscordProvider(defaultConfig);
|
|
66
|
-
|
|
67
|
-
const discordUser = {
|
|
68
|
-
id: '123456789',
|
|
69
|
-
username: 'testuser',
|
|
70
|
-
global_name: 'Test User',
|
|
71
|
-
avatar: 'abc123',
|
|
72
|
-
email: 'test@example.com',
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const result = provider.normalizeUser(discordUser);
|
|
76
|
-
|
|
77
|
-
assert.equal(result.id, '123456789');
|
|
78
|
-
assert.equal(result.username, 'testuser');
|
|
79
|
-
assert.equal(result.displayName, 'Test User');
|
|
80
|
-
assert.equal(result.avatar, 'https://cdn.discordapp.com/avatars/123456789/abc123.png');
|
|
81
|
-
assert.equal(result.email, 'test@example.com');
|
|
82
|
-
assert.deepEqual(result.raw, discordUser);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test('handles missing avatar', function(assert) {
|
|
86
|
-
const provider = new DiscordProvider(defaultConfig);
|
|
87
|
-
|
|
88
|
-
const result = provider.normalizeUser({
|
|
89
|
-
id: '1', username: 'user', global_name: 'User', avatar: null, email: null,
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
assert.equal(result.avatar, null);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test('falls back to username when global_name is missing', function(assert) {
|
|
96
|
-
const provider = new DiscordProvider(defaultConfig);
|
|
97
|
-
|
|
98
|
-
const result = provider.normalizeUser({
|
|
99
|
-
id: '1', username: 'myuser', avatar: null, email: null,
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
assert.equal(result.displayName, 'myuser');
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('handles missing email', function(assert) {
|
|
106
|
-
const provider = new DiscordProvider(defaultConfig);
|
|
107
|
-
|
|
108
|
-
const result = provider.normalizeUser({
|
|
109
|
-
id: '1', username: 'user', global_name: 'User', avatar: null,
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
assert.equal(result.email, null);
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
});
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import QUnit from 'qunit';
|
|
2
|
-
import SessionManager from '../../src/session-manager.js';
|
|
3
|
-
|
|
4
|
-
const { module, test } = QUnit;
|
|
5
|
-
|
|
6
|
-
const mockUser = { id: '1', username: 'testuser' };
|
|
7
|
-
const mockTokens = { accessToken: 'abc', expiresAt: Date.now() + 60000 };
|
|
8
|
-
|
|
9
|
-
module('[Unit] SessionManager', function() {
|
|
10
|
-
module('create', function() {
|
|
11
|
-
test('generates a unique session ID and stores session', function(assert) {
|
|
12
|
-
const manager = new SessionManager(3600);
|
|
13
|
-
const session1 = manager.create(mockUser, mockTokens);
|
|
14
|
-
const session2 = manager.create(mockUser, mockTokens);
|
|
15
|
-
|
|
16
|
-
assert.ok(session1.sessionId);
|
|
17
|
-
assert.ok(session2.sessionId);
|
|
18
|
-
assert.notEqual(session1.sessionId, session2.sessionId);
|
|
19
|
-
assert.deepEqual(session1.user, mockUser);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('sets expiresAt based on duration', function(assert) {
|
|
23
|
-
const manager = new SessionManager(3600);
|
|
24
|
-
const before = Date.now();
|
|
25
|
-
const session = manager.create(mockUser, mockTokens);
|
|
26
|
-
|
|
27
|
-
assert.ok(session.expiresAt >= before + 3600 * 1000);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
module('get', function() {
|
|
32
|
-
test('returns session data for a valid session ID', function(assert) {
|
|
33
|
-
const manager = new SessionManager(3600);
|
|
34
|
-
const { sessionId } = manager.create(mockUser, mockTokens);
|
|
35
|
-
const session = manager.get(sessionId);
|
|
36
|
-
|
|
37
|
-
assert.ok(session);
|
|
38
|
-
assert.deepEqual(session.user, mockUser);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('returns null for unknown session ID', function(assert) {
|
|
42
|
-
const manager = new SessionManager(3600);
|
|
43
|
-
const session = manager.get('nonexistent');
|
|
44
|
-
|
|
45
|
-
assert.equal(session, null);
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
module('validate', function() {
|
|
50
|
-
test('returns user for a valid, non-expired session', function(assert) {
|
|
51
|
-
const manager = new SessionManager(3600);
|
|
52
|
-
const { sessionId } = manager.create(mockUser, mockTokens);
|
|
53
|
-
const user = manager.validate(sessionId);
|
|
54
|
-
|
|
55
|
-
assert.deepEqual(user, mockUser);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('returns null for an expired session and cleans it up', function(assert) {
|
|
59
|
-
const manager = new SessionManager(0);
|
|
60
|
-
const { sessionId } = manager.create(mockUser, mockTokens);
|
|
61
|
-
const user = manager.validate(sessionId);
|
|
62
|
-
|
|
63
|
-
assert.equal(user, null);
|
|
64
|
-
assert.equal(manager.get(sessionId), null);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test('returns null for nonexistent session', function(assert) {
|
|
68
|
-
const manager = new SessionManager(3600);
|
|
69
|
-
const user = manager.validate('missing');
|
|
70
|
-
|
|
71
|
-
assert.equal(user, null);
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
module('destroy', function() {
|
|
76
|
-
test('removes the session', function(assert) {
|
|
77
|
-
const manager = new SessionManager(3600);
|
|
78
|
-
const { sessionId } = manager.create(mockUser, mockTokens);
|
|
79
|
-
|
|
80
|
-
manager.destroy(sessionId);
|
|
81
|
-
|
|
82
|
-
assert.equal(manager.get(sessionId), null);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
});
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import QUnit from 'qunit';
|
|
2
|
-
|
|
3
|
-
const { module, test } = QUnit;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Tests for OAuth state token validation logic.
|
|
7
|
-
*
|
|
8
|
-
* These tests exercise the pendingStates map and validation directly,
|
|
9
|
-
* mirroring the logic in OAuth.handleCallback without requiring the
|
|
10
|
-
* full module initialization (which depends on stonyx config/modules).
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
const TEN_MINUTES = 10 * 60 * 1000;
|
|
14
|
-
|
|
15
|
-
function validateState(pendingStates, stateToken) {
|
|
16
|
-
if (!stateToken || !pendingStates.has(stateToken)) {
|
|
17
|
-
throw new Error('Invalid or missing state token');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const stateCreatedAt = pendingStates.get(stateToken);
|
|
21
|
-
pendingStates.delete(stateToken);
|
|
22
|
-
|
|
23
|
-
if (Date.now() - stateCreatedAt > TEN_MINUTES) {
|
|
24
|
-
throw new Error('State token has expired');
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
module('[Unit] State Validation', function() {
|
|
29
|
-
test('accepts a valid pending state token', function(assert) {
|
|
30
|
-
const pendingStates = new Map();
|
|
31
|
-
pendingStates.set('valid-token', Date.now());
|
|
32
|
-
|
|
33
|
-
validateState(pendingStates, 'valid-token');
|
|
34
|
-
assert.ok(true, 'did not throw');
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test('consumes the state token after validation', function(assert) {
|
|
38
|
-
const pendingStates = new Map();
|
|
39
|
-
pendingStates.set('one-time-token', Date.now());
|
|
40
|
-
|
|
41
|
-
validateState(pendingStates, 'one-time-token');
|
|
42
|
-
assert.false(pendingStates.has('one-time-token'), 'token removed from pending states');
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('rejects a missing state token', function(assert) {
|
|
46
|
-
const pendingStates = new Map();
|
|
47
|
-
|
|
48
|
-
assert.throws(
|
|
49
|
-
() => validateState(pendingStates, undefined),
|
|
50
|
-
/Invalid or missing state token/,
|
|
51
|
-
);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test('rejects an empty string state token', function(assert) {
|
|
55
|
-
const pendingStates = new Map();
|
|
56
|
-
|
|
57
|
-
assert.throws(
|
|
58
|
-
() => validateState(pendingStates, ''),
|
|
59
|
-
/Invalid or missing state token/,
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('rejects an unknown state token', function(assert) {
|
|
64
|
-
const pendingStates = new Map();
|
|
65
|
-
pendingStates.set('known-token', Date.now());
|
|
66
|
-
|
|
67
|
-
assert.throws(
|
|
68
|
-
() => validateState(pendingStates, 'unknown-token'),
|
|
69
|
-
/Invalid or missing state token/,
|
|
70
|
-
);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test('rejects an expired state token (older than 10 minutes)', function(assert) {
|
|
74
|
-
const pendingStates = new Map();
|
|
75
|
-
const elevenMinutesAgo = Date.now() - (11 * 60 * 1000);
|
|
76
|
-
pendingStates.set('expired-token', elevenMinutesAgo);
|
|
77
|
-
|
|
78
|
-
assert.throws(
|
|
79
|
-
() => validateState(pendingStates, 'expired-token'),
|
|
80
|
-
/State token has expired/,
|
|
81
|
-
);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test('expired state token is still consumed', function(assert) {
|
|
85
|
-
const pendingStates = new Map();
|
|
86
|
-
const elevenMinutesAgo = Date.now() - (11 * 60 * 1000);
|
|
87
|
-
pendingStates.set('expired-token', elevenMinutesAgo);
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
validateState(pendingStates, 'expired-token');
|
|
91
|
-
} catch {
|
|
92
|
-
// expected
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
assert.false(pendingStates.has('expired-token'), 'expired token removed from map');
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test('accepts a token just under 10 minutes old', function(assert) {
|
|
99
|
-
const pendingStates = new Map();
|
|
100
|
-
const nineMinutesAgo = Date.now() - (9 * 60 * 1000);
|
|
101
|
-
pendingStates.set('fresh-token', nineMinutesAgo);
|
|
102
|
-
|
|
103
|
-
validateState(pendingStates, 'fresh-token');
|
|
104
|
-
assert.ok(true, 'did not throw');
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test('rejects reuse of a previously valid token', function(assert) {
|
|
108
|
-
const pendingStates = new Map();
|
|
109
|
-
pendingStates.set('use-once', Date.now());
|
|
110
|
-
|
|
111
|
-
validateState(pendingStates, 'use-once');
|
|
112
|
-
|
|
113
|
-
assert.throws(
|
|
114
|
-
() => validateState(pendingStates, 'use-once'),
|
|
115
|
-
/Invalid or missing state token/,
|
|
116
|
-
);
|
|
117
|
-
});
|
|
118
|
-
});
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import QUnit from 'qunit';
|
|
2
|
-
import TokenManager from '../../src/token-manager.js';
|
|
3
|
-
|
|
4
|
-
const { module, test } = QUnit;
|
|
5
|
-
|
|
6
|
-
function createMockFlow() {
|
|
7
|
-
return {
|
|
8
|
-
exchangeCode: async (code) => ({
|
|
9
|
-
accessToken: `token-for-${code}`,
|
|
10
|
-
refreshToken: 'refresh-123',
|
|
11
|
-
expiresIn: 3600,
|
|
12
|
-
}),
|
|
13
|
-
refreshAccessToken: async (refreshToken) => ({
|
|
14
|
-
accessToken: 'new-access',
|
|
15
|
-
refreshToken,
|
|
16
|
-
expiresIn: 7200,
|
|
17
|
-
}),
|
|
18
|
-
revokeToken: async () => {},
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
module('[Unit] TokenManager', function() {
|
|
23
|
-
module('getTokens', function() {
|
|
24
|
-
test('delegates to flow.exchangeCode and sets expiresAt', async function(assert) {
|
|
25
|
-
const manager = new TokenManager(createMockFlow());
|
|
26
|
-
const before = Date.now();
|
|
27
|
-
const result = await manager.getTokens('my-code');
|
|
28
|
-
|
|
29
|
-
assert.equal(result.accessToken, 'token-for-my-code');
|
|
30
|
-
assert.equal(result.refreshToken, 'refresh-123');
|
|
31
|
-
assert.ok(result.expiresAt >= before + 3600 * 1000);
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
module('refresh', function() {
|
|
36
|
-
test('delegates to flow.refreshAccessToken and sets expiresAt', async function(assert) {
|
|
37
|
-
const manager = new TokenManager(createMockFlow());
|
|
38
|
-
const result = await manager.refresh('refresh-123');
|
|
39
|
-
|
|
40
|
-
assert.equal(result.accessToken, 'new-access');
|
|
41
|
-
assert.ok(result.expiresAt > Date.now());
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
module('revoke', function() {
|
|
46
|
-
test('delegates to flow.revokeToken', async function(assert) {
|
|
47
|
-
let revoked = false;
|
|
48
|
-
const flow = { ...createMockFlow(), revokeToken: async () => { revoked = true; } };
|
|
49
|
-
const manager = new TokenManager(flow);
|
|
50
|
-
|
|
51
|
-
await manager.revoke('some-token');
|
|
52
|
-
assert.ok(revoked);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
module('isExpired', function() {
|
|
57
|
-
test('returns true if expiresAt is in the past', function(assert) {
|
|
58
|
-
const manager = new TokenManager(createMockFlow());
|
|
59
|
-
|
|
60
|
-
assert.true(manager.isExpired({ expiresAt: Date.now() - 1000 }));
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('returns false if expiresAt is in the future', function(assert) {
|
|
64
|
-
const manager = new TokenManager(createMockFlow());
|
|
65
|
-
|
|
66
|
-
assert.false(manager.isExpired({ expiresAt: Date.now() + 60000 }));
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('returns true if tokenData is null or missing expiresAt', function(assert) {
|
|
70
|
-
const manager = new TokenManager(createMockFlow());
|
|
71
|
-
|
|
72
|
-
assert.true(manager.isExpired(null));
|
|
73
|
-
assert.true(manager.isExpired({}));
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
});
|