@stonyx/oauth 0.1.1-beta.9 → 0.1.1-beta.90

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.
Files changed (36) hide show
  1. package/README.md +4 -0
  2. package/dist/auth-request.d.ts +35 -0
  3. package/dist/auth-request.js +68 -0
  4. package/dist/main.d.ts +22 -0
  5. package/dist/main.js +79 -0
  6. package/dist/oauth-flow.d.ts +30 -0
  7. package/dist/oauth-flow.js +83 -0
  8. package/dist/providers/discord.d.ts +30 -0
  9. package/dist/providers/discord.js +43 -0
  10. package/dist/session-manager.d.ts +20 -0
  11. package/dist/session-manager.js +30 -0
  12. package/dist/token-manager.d.ts +15 -0
  13. package/dist/token-manager.js +24 -0
  14. package/package.json +45 -8
  15. package/src/{auth-request.js → auth-request.ts} +26 -6
  16. package/src/{main.js → main.ts} +36 -10
  17. package/src/{oauth-flow.js → oauth-flow.ts} +31 -7
  18. package/src/providers/{discord.js → discord.ts} +29 -3
  19. package/src/{session-manager.js → session-manager.ts} +19 -6
  20. package/src/token-manager.ts +35 -0
  21. package/src/types/node.d.ts +3 -0
  22. package/src/types/stonyx-events.d.ts +4 -0
  23. package/src/types/stonyx-rest-server.d.ts +11 -0
  24. package/src/types/stonyx.d.ts +37 -0
  25. package/.github/workflows/ci.yml +0 -16
  26. package/.github/workflows/publish.yml +0 -51
  27. package/src/token-manager.js +0 -26
  28. package/test/config/environment.js +0 -18
  29. package/test/integration/oauth-test.js +0 -149
  30. package/test/sample/providers/mock.js +0 -40
  31. package/test/sample/requests/.gitkeep +0 -0
  32. package/test/unit/oauth-flow-test.js +0 -137
  33. package/test/unit/providers/discord-test.js +0 -115
  34. package/test/unit/session-manager-test.js +0 -85
  35. package/test/unit/state-validation-test.js +0 -118
  36. package/test/unit/token-manager-test.js +0 -76
@@ -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
- });