@stonyx/oauth 0.1.1-beta.18 → 0.1.1-beta.180

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 (38) hide show
  1. package/README.md +72 -0
  2. package/dist/auth-request.d.ts +97 -0
  3. package/dist/auth-request.js +187 -0
  4. package/dist/main.d.ts +106 -0
  5. package/dist/main.js +177 -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 -9
  15. package/src/auth-request.ts +271 -0
  16. package/src/main.ts +238 -0
  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 +19 -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 +38 -0
  25. package/.github/workflows/ci.yml +0 -16
  26. package/.github/workflows/publish.yml +0 -51
  27. package/src/auth-request.js +0 -74
  28. package/src/main.js +0 -83
  29. package/src/token-manager.js +0 -26
  30. package/test/config/environment.js +0 -18
  31. package/test/integration/oauth-test.js +0 -149
  32. package/test/sample/providers/mock.js +0 -40
  33. package/test/sample/requests/.gitkeep +0 -0
  34. package/test/unit/oauth-flow-test.js +0 -137
  35. package/test/unit/providers/discord-test.js +0 -115
  36. package/test/unit/session-manager-test.js +0 -85
  37. package/test/unit/state-validation-test.js +0 -118
  38. package/test/unit/token-manager-test.js +0 -76
@@ -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
- });