@stonyx/oauth 0.1.1-alpha.2 → 0.1.1-alpha.21
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 +170 -3
- package/dist/auth-request.d.ts +147 -0
- package/dist/auth-request.js +342 -0
- package/dist/constants.d.ts +33 -0
- package/dist/constants.js +42 -0
- package/dist/main.d.ts +45 -0
- package/dist/main.js +81 -0
- package/dist/oauth-flow.d.ts +30 -0
- package/dist/oauth-flow.js +83 -0
- package/dist/providers/discord.d.ts +30 -0
- package/dist/providers/discord.js +43 -0
- package/dist/session-manager.d.ts +20 -0
- package/dist/session-manager.js +30 -0
- package/dist/state-store.d.ts +116 -0
- package/dist/state-store.js +144 -0
- package/dist/token-manager.d.ts +15 -0
- package/dist/token-manager.js +24 -0
- package/package.json +45 -9
- package/src/auth-request.ts +434 -0
- package/src/constants.ts +46 -0
- package/src/main.ts +123 -0
- package/src/{oauth-flow.js → oauth-flow.ts} +31 -7
- package/src/providers/{discord.js → discord.ts} +29 -3
- package/src/{session-manager.js → session-manager.ts} +19 -6
- package/src/state-store.ts +179 -0
- package/src/token-manager.ts +35 -0
- package/src/types/node.d.ts +10 -0
- package/src/types/stonyx-events.d.ts +4 -0
- package/src/types/stonyx-rest-server.d.ts +11 -0
- package/src/types/stonyx.d.ts +38 -0
- package/.github/workflows/ci.yml +0 -16
- package/.github/workflows/publish.yml +0 -51
- package/src/auth-request.js +0 -74
- package/src/main.js +0 -83
- package/src/token-manager.js +0 -26
- 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
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import OAuthFlow from '../oauth-flow.js';
|
|
2
|
+
import type { TokenResult } from '../oauth-flow.js';
|
|
3
|
+
interface DiscordProviderConfig {
|
|
4
|
+
clientId: string;
|
|
5
|
+
clientSecret: string;
|
|
6
|
+
redirectUri: string;
|
|
7
|
+
scopes?: string[];
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
interface DiscordUser {
|
|
11
|
+
id: string;
|
|
12
|
+
username: string;
|
|
13
|
+
global_name?: string;
|
|
14
|
+
avatar: string | null;
|
|
15
|
+
email?: string | null;
|
|
16
|
+
}
|
|
17
|
+
interface NormalizedDiscordUser {
|
|
18
|
+
id: string;
|
|
19
|
+
username: string;
|
|
20
|
+
displayName: string;
|
|
21
|
+
avatar: string | null;
|
|
22
|
+
email: string | null;
|
|
23
|
+
raw: DiscordUser;
|
|
24
|
+
}
|
|
25
|
+
export default class DiscordProvider extends OAuthFlow {
|
|
26
|
+
constructor(config: DiscordProviderConfig);
|
|
27
|
+
exchangeCode(code: string): Promise<TokenResult>;
|
|
28
|
+
normalizeUser(rawUser: DiscordUser): NormalizedDiscordUser;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import OAuthFlow from '../oauth-flow.js';
|
|
2
|
+
export default class DiscordProvider extends OAuthFlow {
|
|
3
|
+
constructor(config) {
|
|
4
|
+
super({
|
|
5
|
+
...config,
|
|
6
|
+
authorizationUrl: 'https://discord.com/oauth2/authorize',
|
|
7
|
+
tokenUrl: 'https://discord.com/api/oauth2/token',
|
|
8
|
+
userInfoUrl: 'https://discord.com/api/users/@me',
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
async exchangeCode(code) {
|
|
12
|
+
const response = await fetch(this.tokenUrl, {
|
|
13
|
+
method: 'POST',
|
|
14
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
15
|
+
body: new URLSearchParams({
|
|
16
|
+
client_id: this.clientId,
|
|
17
|
+
client_secret: this.clientSecret,
|
|
18
|
+
grant_type: 'authorization_code',
|
|
19
|
+
code,
|
|
20
|
+
redirect_uri: this.redirectUri,
|
|
21
|
+
}),
|
|
22
|
+
});
|
|
23
|
+
if (!response.ok)
|
|
24
|
+
throw new Error(`Token exchange failed: ${response.status}`);
|
|
25
|
+
const data = await response.json();
|
|
26
|
+
return {
|
|
27
|
+
accessToken: data.access_token,
|
|
28
|
+
refreshToken: data.refresh_token || null,
|
|
29
|
+
expiresIn: data.expires_in,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
normalizeUser(rawUser) {
|
|
33
|
+
const { id, username, global_name, avatar, email } = rawUser;
|
|
34
|
+
return {
|
|
35
|
+
id,
|
|
36
|
+
username,
|
|
37
|
+
displayName: global_name || username,
|
|
38
|
+
avatar: avatar ? `https://cdn.discordapp.com/avatars/${id}/${avatar}.png` : null,
|
|
39
|
+
email: email || null,
|
|
40
|
+
raw: rawUser,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface SessionData {
|
|
2
|
+
user: unknown;
|
|
3
|
+
tokens: unknown;
|
|
4
|
+
expiresAt: number;
|
|
5
|
+
}
|
|
6
|
+
export interface SessionResult {
|
|
7
|
+
sessionId: string;
|
|
8
|
+
user: unknown;
|
|
9
|
+
expiresAt: number;
|
|
10
|
+
}
|
|
11
|
+
export default class SessionManager {
|
|
12
|
+
sessions: Map<string, SessionData>;
|
|
13
|
+
duration: number;
|
|
14
|
+
constructor(duration: number);
|
|
15
|
+
create(user: unknown, tokens: unknown): SessionResult;
|
|
16
|
+
get(sessionId: string): SessionData | null;
|
|
17
|
+
destroy(sessionId: string): void;
|
|
18
|
+
validate(sessionId: string): unknown;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
export default class SessionManager {
|
|
3
|
+
sessions = new Map();
|
|
4
|
+
duration;
|
|
5
|
+
constructor(duration) {
|
|
6
|
+
this.duration = duration;
|
|
7
|
+
}
|
|
8
|
+
create(user, tokens) {
|
|
9
|
+
const sessionId = randomUUID();
|
|
10
|
+
const expiresAt = Date.now() + (this.duration * 1000);
|
|
11
|
+
this.sessions.set(sessionId, { user, tokens, expiresAt });
|
|
12
|
+
return { sessionId, user, expiresAt };
|
|
13
|
+
}
|
|
14
|
+
get(sessionId) {
|
|
15
|
+
return this.sessions.get(sessionId) || null;
|
|
16
|
+
}
|
|
17
|
+
destroy(sessionId) {
|
|
18
|
+
this.sessions.delete(sessionId);
|
|
19
|
+
}
|
|
20
|
+
validate(sessionId) {
|
|
21
|
+
const session = this.get(sessionId);
|
|
22
|
+
if (!session)
|
|
23
|
+
return null;
|
|
24
|
+
if (Date.now() >= session.expiresAt) {
|
|
25
|
+
this.destroy(sessionId);
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return session.user;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side record for an OAuth flow that is in progress.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately holds a *digest* of the binding value rather than the value
|
|
5
|
+
* itself: a callback is only accepted when the caller presents the plaintext
|
|
6
|
+
* that hashes to `bindingHash`, so the record on its own unlocks nothing.
|
|
7
|
+
*/
|
|
8
|
+
export interface PendingState {
|
|
9
|
+
provider: string;
|
|
10
|
+
bindingHash: string;
|
|
11
|
+
createdAt: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The five reasons a callback is rejected, as fixed strings.
|
|
15
|
+
*
|
|
16
|
+
* Named rather than inlined so that collapsing two of them into one is a
|
|
17
|
+
* visible edit: distinguishing them in the server log is the whole point of
|
|
18
|
+
* logging a reason, and an operator telling an expired state from a
|
|
19
|
+
* cross-provider replay depends on them staying distinct.
|
|
20
|
+
*/
|
|
21
|
+
export declare const STATE_REJECTION: {
|
|
22
|
+
readonly unknownState: "Invalid or missing state token";
|
|
23
|
+
readonly expired: "State token has expired";
|
|
24
|
+
readonly wrongProvider: "State token was not issued for this provider";
|
|
25
|
+
readonly missingBinding: "Missing state binding value";
|
|
26
|
+
readonly unboundClient: "State token is not bound to this client";
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* A callback rejected by `StateStore.consume`.
|
|
30
|
+
*
|
|
31
|
+
* Carries two things the route layer cannot otherwise recover: that the
|
|
32
|
+
* rejection came from state validation rather than from anything downstream of
|
|
33
|
+
* it, and whether a pending record was actually consumed.
|
|
34
|
+
*/
|
|
35
|
+
export declare class StateRejection extends Error {
|
|
36
|
+
/** True when this attempt recognised a pending record and burned it. */
|
|
37
|
+
consumed: boolean;
|
|
38
|
+
constructor(reason: string, consumed: boolean);
|
|
39
|
+
}
|
|
40
|
+
export interface IssuedState {
|
|
41
|
+
/** Sent to the provider as the OAuth2 `state` parameter. */
|
|
42
|
+
stateToken: string;
|
|
43
|
+
/** Held by the client that started the flow (a cookie), never by the provider. */
|
|
44
|
+
bindingValue: string;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Issues and validates OAuth2 `state` tokens bound to the client that started
|
|
48
|
+
* the flow (#36).
|
|
49
|
+
*
|
|
50
|
+
* Presence-plus-age on a process-global map is replay-window limiting, not the
|
|
51
|
+
* CSRF binding `state` exists to provide (RFC 6749 section 10.12): any state
|
|
52
|
+
* issued to any visitor validated for any callback, so an attacker could
|
|
53
|
+
* harvest their own state and code and deliver them to a victim, logging the
|
|
54
|
+
* victim in as the attacker. A state is now only accepted when the caller also
|
|
55
|
+
* presents the matching client-held binding value, and only at the provider it
|
|
56
|
+
* was issued for.
|
|
57
|
+
*/
|
|
58
|
+
export default class StateStore {
|
|
59
|
+
pending: Map<string, PendingState>;
|
|
60
|
+
ttl: number;
|
|
61
|
+
constructor(ttl?: number);
|
|
62
|
+
static hash(value: string): string;
|
|
63
|
+
/** Length-independent, content-constant-time comparison of two digests. */
|
|
64
|
+
static digestsMatch(a: string, b: string): boolean;
|
|
65
|
+
issue(provider: string): IssuedState;
|
|
66
|
+
/**
|
|
67
|
+
* Validates and consumes a pending state. Throws on every rejection path.
|
|
68
|
+
*
|
|
69
|
+
* The record is removed as soon as the state is recognised — before the TTL,
|
|
70
|
+
* provider and binding checks — so every state gets exactly one attempt
|
|
71
|
+
* whatever the outcome.
|
|
72
|
+
*
|
|
73
|
+
* That uniformity is the justification, not brute-force resistance:
|
|
74
|
+
* guessing `BINDING_VALUE_BYTES` of CSPRNG output is infeasible whether or
|
|
75
|
+
* not the record survives. What retaining it would buy an attacker is a
|
|
76
|
+
* repeatable, unauthenticated oracle on this endpoint for the state's full
|
|
77
|
+
* lifetime — and the safety of that would then rest entirely on an entropy
|
|
78
|
+
* constant a future change can lower. One attempt per state is a structural
|
|
79
|
+
* property; entropy arithmetic is not.
|
|
80
|
+
*
|
|
81
|
+
* The trade is real: an attacker who already knows a victim's state can burn
|
|
82
|
+
* it, and the victim must restart at `/auth/login/:provider`. That vector is
|
|
83
|
+
* accepted deliberately — it requires the victim's `randomUUID` state, and
|
|
84
|
+
* it is self-healing on retry. `consumed` on the rejection says whether this
|
|
85
|
+
* call actually burned a record, so a caller can distinguish "nothing of the
|
|
86
|
+
* victim's was touched" from "one attempt was spent".
|
|
87
|
+
*
|
|
88
|
+
* `bindingValues` is every value the client presented under the binding
|
|
89
|
+
* cookie's name, not just the first — see `anyCandidateMatches`.
|
|
90
|
+
*/
|
|
91
|
+
consume(stateToken: string | undefined, provider: string, bindingValues: readonly string[]): void;
|
|
92
|
+
/**
|
|
93
|
+
* Whether *any* presented value is the binding value for this record.
|
|
94
|
+
*
|
|
95
|
+
* Every candidate is tried, and the callback is accepted if one matches.
|
|
96
|
+
* Returning on the first value carrying the cookie name instead made a
|
|
97
|
+
* planted cookie a permanent, unauthenticated denial of login: RFC 6265
|
|
98
|
+
* section 5.4 orders the `Cookie` header by path length then creation time,
|
|
99
|
+
* so an attacker with content control on a sibling subdomain sets a
|
|
100
|
+
* same-named cookie once and every subsequent callback for that victim reads
|
|
101
|
+
* theirs, fails the binding check, and burns the state on the way out. The
|
|
102
|
+
* victim cannot recover by retrying.
|
|
103
|
+
*
|
|
104
|
+
* Accepting any match gives an attacker nothing: they would have to present
|
|
105
|
+
* the victim's own binding value, which is the property being checked. The
|
|
106
|
+
* record is consumed on recognition, so a state still gets exactly one
|
|
107
|
+
* attempt however many candidates were presented, and the candidate count is
|
|
108
|
+
* bounded by Node's header size limit rather than by a cap here — a cap
|
|
109
|
+
* truncates the list from the wrong end and reinstates the denial this method
|
|
110
|
+
* exists to close. See `constants.ts`.
|
|
111
|
+
*
|
|
112
|
+
* The loop does not short-circuit, so the work is a function of how many
|
|
113
|
+
* values were presented and not of which one matched.
|
|
114
|
+
*/
|
|
115
|
+
anyCandidateMatches(candidates: readonly string[], record: PendingState): boolean;
|
|
116
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
|
2
|
+
import { BINDING_VALUE_BYTES, STATE_TTL_MS } from './constants.js';
|
|
3
|
+
/**
|
|
4
|
+
* The five reasons a callback is rejected, as fixed strings.
|
|
5
|
+
*
|
|
6
|
+
* Named rather than inlined so that collapsing two of them into one is a
|
|
7
|
+
* visible edit: distinguishing them in the server log is the whole point of
|
|
8
|
+
* logging a reason, and an operator telling an expired state from a
|
|
9
|
+
* cross-provider replay depends on them staying distinct.
|
|
10
|
+
*/
|
|
11
|
+
export const STATE_REJECTION = {
|
|
12
|
+
unknownState: 'Invalid or missing state token',
|
|
13
|
+
expired: 'State token has expired',
|
|
14
|
+
wrongProvider: 'State token was not issued for this provider',
|
|
15
|
+
missingBinding: 'Missing state binding value',
|
|
16
|
+
unboundClient: 'State token is not bound to this client',
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* A callback rejected by `StateStore.consume`.
|
|
20
|
+
*
|
|
21
|
+
* Carries two things the route layer cannot otherwise recover: that the
|
|
22
|
+
* rejection came from state validation rather than from anything downstream of
|
|
23
|
+
* it, and whether a pending record was actually consumed.
|
|
24
|
+
*/
|
|
25
|
+
export class StateRejection extends Error {
|
|
26
|
+
/** True when this attempt recognised a pending record and burned it. */
|
|
27
|
+
consumed;
|
|
28
|
+
constructor(reason, consumed) {
|
|
29
|
+
super(reason);
|
|
30
|
+
this.name = 'StateRejection';
|
|
31
|
+
this.consumed = consumed;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Issues and validates OAuth2 `state` tokens bound to the client that started
|
|
36
|
+
* the flow (#36).
|
|
37
|
+
*
|
|
38
|
+
* Presence-plus-age on a process-global map is replay-window limiting, not the
|
|
39
|
+
* CSRF binding `state` exists to provide (RFC 6749 section 10.12): any state
|
|
40
|
+
* issued to any visitor validated for any callback, so an attacker could
|
|
41
|
+
* harvest their own state and code and deliver them to a victim, logging the
|
|
42
|
+
* victim in as the attacker. A state is now only accepted when the caller also
|
|
43
|
+
* presents the matching client-held binding value, and only at the provider it
|
|
44
|
+
* was issued for.
|
|
45
|
+
*/
|
|
46
|
+
export default class StateStore {
|
|
47
|
+
pending = new Map();
|
|
48
|
+
ttl;
|
|
49
|
+
constructor(ttl = STATE_TTL_MS) {
|
|
50
|
+
this.ttl = ttl;
|
|
51
|
+
}
|
|
52
|
+
static hash(value) {
|
|
53
|
+
return createHash('sha256').update(value).digest('hex');
|
|
54
|
+
}
|
|
55
|
+
/** Length-independent, content-constant-time comparison of two digests. */
|
|
56
|
+
static digestsMatch(a, b) {
|
|
57
|
+
if (a.length !== b.length)
|
|
58
|
+
return false;
|
|
59
|
+
let difference = 0;
|
|
60
|
+
for (let index = 0; index < a.length; index++) {
|
|
61
|
+
difference |= a.charCodeAt(index) ^ b.charCodeAt(index);
|
|
62
|
+
}
|
|
63
|
+
return difference === 0;
|
|
64
|
+
}
|
|
65
|
+
issue(provider) {
|
|
66
|
+
const stateToken = randomUUID();
|
|
67
|
+
const bindingValue = randomBytes(BINDING_VALUE_BYTES).toString('base64url');
|
|
68
|
+
this.pending.set(stateToken, {
|
|
69
|
+
provider,
|
|
70
|
+
bindingHash: StateStore.hash(bindingValue),
|
|
71
|
+
createdAt: Date.now(),
|
|
72
|
+
});
|
|
73
|
+
return { stateToken, bindingValue };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Validates and consumes a pending state. Throws on every rejection path.
|
|
77
|
+
*
|
|
78
|
+
* The record is removed as soon as the state is recognised — before the TTL,
|
|
79
|
+
* provider and binding checks — so every state gets exactly one attempt
|
|
80
|
+
* whatever the outcome.
|
|
81
|
+
*
|
|
82
|
+
* That uniformity is the justification, not brute-force resistance:
|
|
83
|
+
* guessing `BINDING_VALUE_BYTES` of CSPRNG output is infeasible whether or
|
|
84
|
+
* not the record survives. What retaining it would buy an attacker is a
|
|
85
|
+
* repeatable, unauthenticated oracle on this endpoint for the state's full
|
|
86
|
+
* lifetime — and the safety of that would then rest entirely on an entropy
|
|
87
|
+
* constant a future change can lower. One attempt per state is a structural
|
|
88
|
+
* property; entropy arithmetic is not.
|
|
89
|
+
*
|
|
90
|
+
* The trade is real: an attacker who already knows a victim's state can burn
|
|
91
|
+
* it, and the victim must restart at `/auth/login/:provider`. That vector is
|
|
92
|
+
* accepted deliberately — it requires the victim's `randomUUID` state, and
|
|
93
|
+
* it is self-healing on retry. `consumed` on the rejection says whether this
|
|
94
|
+
* call actually burned a record, so a caller can distinguish "nothing of the
|
|
95
|
+
* victim's was touched" from "one attempt was spent".
|
|
96
|
+
*
|
|
97
|
+
* `bindingValues` is every value the client presented under the binding
|
|
98
|
+
* cookie's name, not just the first — see `anyCandidateMatches`.
|
|
99
|
+
*/
|
|
100
|
+
consume(stateToken, provider, bindingValues) {
|
|
101
|
+
if (!stateToken)
|
|
102
|
+
throw new StateRejection(STATE_REJECTION.unknownState, false);
|
|
103
|
+
const record = this.pending.get(stateToken);
|
|
104
|
+
if (!record)
|
|
105
|
+
throw new StateRejection(STATE_REJECTION.unknownState, false);
|
|
106
|
+
this.pending.delete(stateToken);
|
|
107
|
+
if (Date.now() - record.createdAt > this.ttl)
|
|
108
|
+
throw new StateRejection(STATE_REJECTION.expired, true);
|
|
109
|
+
if (record.provider !== provider)
|
|
110
|
+
throw new StateRejection(STATE_REJECTION.wrongProvider, true);
|
|
111
|
+
const candidates = bindingValues.filter(value => value.length > 0);
|
|
112
|
+
if (candidates.length === 0)
|
|
113
|
+
throw new StateRejection(STATE_REJECTION.missingBinding, true);
|
|
114
|
+
if (!this.anyCandidateMatches(candidates, record)) {
|
|
115
|
+
throw new StateRejection(STATE_REJECTION.unboundClient, true);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Whether *any* presented value is the binding value for this record.
|
|
120
|
+
*
|
|
121
|
+
* Every candidate is tried, and the callback is accepted if one matches.
|
|
122
|
+
* Returning on the first value carrying the cookie name instead made a
|
|
123
|
+
* planted cookie a permanent, unauthenticated denial of login: RFC 6265
|
|
124
|
+
* section 5.4 orders the `Cookie` header by path length then creation time,
|
|
125
|
+
* so an attacker with content control on a sibling subdomain sets a
|
|
126
|
+
* same-named cookie once and every subsequent callback for that victim reads
|
|
127
|
+
* theirs, fails the binding check, and burns the state on the way out. The
|
|
128
|
+
* victim cannot recover by retrying.
|
|
129
|
+
*
|
|
130
|
+
* Accepting any match gives an attacker nothing: they would have to present
|
|
131
|
+
* the victim's own binding value, which is the property being checked. The
|
|
132
|
+
* record is consumed on recognition, so a state still gets exactly one
|
|
133
|
+
* attempt however many candidates were presented, and the candidate count is
|
|
134
|
+
* bounded by Node's header size limit rather than by a cap here — a cap
|
|
135
|
+
* truncates the list from the wrong end and reinstates the denial this method
|
|
136
|
+
* exists to close. See `constants.ts`.
|
|
137
|
+
*
|
|
138
|
+
* The loop does not short-circuit, so the work is a function of how many
|
|
139
|
+
* values were presented and not of which one matched.
|
|
140
|
+
*/
|
|
141
|
+
anyCandidateMatches(candidates, record) {
|
|
142
|
+
return candidates.reduce((matched, candidate) => StateStore.digestsMatch(StateStore.hash(candidate), record.bindingHash) || matched, false);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type OAuthFlow from './oauth-flow.js';
|
|
2
|
+
import type { TokenResult } from './oauth-flow.js';
|
|
3
|
+
export interface TokenData extends TokenResult {
|
|
4
|
+
expiresAt: number;
|
|
5
|
+
}
|
|
6
|
+
export default class TokenManager {
|
|
7
|
+
flow: OAuthFlow;
|
|
8
|
+
constructor(flow: OAuthFlow);
|
|
9
|
+
getTokens(code: string): Promise<TokenData>;
|
|
10
|
+
refresh(refreshToken: string): Promise<TokenData>;
|
|
11
|
+
revoke(accessToken: string): Promise<void>;
|
|
12
|
+
isExpired(tokenData: {
|
|
13
|
+
expiresAt?: number;
|
|
14
|
+
} | null | undefined): boolean;
|
|
15
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export default class TokenManager {
|
|
2
|
+
flow;
|
|
3
|
+
constructor(flow) {
|
|
4
|
+
this.flow = flow;
|
|
5
|
+
}
|
|
6
|
+
async getTokens(code) {
|
|
7
|
+
const tokens = await this.flow.exchangeCode(code);
|
|
8
|
+
tokens.expiresAt = Date.now() + (tokens.expiresIn * 1000);
|
|
9
|
+
return tokens;
|
|
10
|
+
}
|
|
11
|
+
async refresh(refreshToken) {
|
|
12
|
+
const tokens = await this.flow.refreshAccessToken(refreshToken);
|
|
13
|
+
tokens.expiresAt = Date.now() + (tokens.expiresIn * 1000);
|
|
14
|
+
return tokens;
|
|
15
|
+
}
|
|
16
|
+
async revoke(accessToken) {
|
|
17
|
+
return this.flow.revokeToken(accessToken);
|
|
18
|
+
}
|
|
19
|
+
isExpired(tokenData) {
|
|
20
|
+
if (!tokenData?.expiresAt)
|
|
21
|
+
return true;
|
|
22
|
+
return Date.now() >= tokenData.expiresAt;
|
|
23
|
+
}
|
|
24
|
+
}
|
package/package.json
CHANGED
|
@@ -4,40 +4,76 @@
|
|
|
4
4
|
"stonyx-async",
|
|
5
5
|
"stonyx-module"
|
|
6
6
|
],
|
|
7
|
-
"version": "0.1.1-alpha.
|
|
7
|
+
"version": "0.1.1-alpha.21",
|
|
8
8
|
"description": "OAuth2 authentication module for the Stonyx framework",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "git+https://github.com/abofs/stonyx-oauth.git"
|
|
12
12
|
},
|
|
13
|
-
"main": "
|
|
13
|
+
"main": "dist/main.js",
|
|
14
14
|
"type": "module",
|
|
15
15
|
"exports": {
|
|
16
|
-
".":
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/main.d.ts",
|
|
18
|
+
"default": "./dist/main.js"
|
|
19
|
+
},
|
|
20
|
+
"./oauth-flow": {
|
|
21
|
+
"types": "./dist/oauth-flow.d.ts",
|
|
22
|
+
"default": "./dist/oauth-flow.js"
|
|
23
|
+
},
|
|
24
|
+
"./auth-request": {
|
|
25
|
+
"types": "./dist/auth-request.d.ts",
|
|
26
|
+
"default": "./dist/auth-request.js"
|
|
27
|
+
},
|
|
28
|
+
"./session-manager": {
|
|
29
|
+
"types": "./dist/session-manager.d.ts",
|
|
30
|
+
"default": "./dist/session-manager.js"
|
|
31
|
+
},
|
|
32
|
+
"./token-manager": {
|
|
33
|
+
"types": "./dist/token-manager.d.ts",
|
|
34
|
+
"default": "./dist/token-manager.js"
|
|
35
|
+
},
|
|
36
|
+
"./providers/discord": {
|
|
37
|
+
"types": "./dist/providers/discord.d.ts",
|
|
38
|
+
"default": "./dist/providers/discord.js"
|
|
39
|
+
}
|
|
17
40
|
},
|
|
18
41
|
"author": "Stone Costa",
|
|
19
42
|
"license": "Apache-2.0",
|
|
20
43
|
"contributors": [
|
|
21
44
|
"Stone Costa <stone.costa@synamicd.com>"
|
|
22
45
|
],
|
|
46
|
+
"files": [
|
|
47
|
+
"dist",
|
|
48
|
+
"src",
|
|
49
|
+
"config",
|
|
50
|
+
"README.md"
|
|
51
|
+
],
|
|
23
52
|
"publishConfig": {
|
|
24
53
|
"access": "public",
|
|
25
54
|
"provenance": true
|
|
26
55
|
},
|
|
27
56
|
"dependencies": {
|
|
28
|
-
"stonyx": "0.
|
|
29
|
-
"
|
|
57
|
+
"@stonyx/events": "0.1.1-beta.52",
|
|
58
|
+
"stonyx": "0.2.3-beta.77"
|
|
30
59
|
},
|
|
31
60
|
"peerDependencies": {
|
|
32
61
|
"@stonyx/rest-server": ">=0.2.1-beta.11"
|
|
33
62
|
},
|
|
34
63
|
"devDependencies": {
|
|
35
|
-
"@stonyx/rest-server": "0.2.1-beta.
|
|
36
|
-
"@stonyx/utils": "0.2.3-beta.
|
|
64
|
+
"@stonyx/rest-server": "0.2.1-beta.83",
|
|
65
|
+
"@stonyx/utils": "0.2.3-beta.26",
|
|
66
|
+
"@stonyx/logs": "1.0.1-beta.19",
|
|
67
|
+
"@types/qunit": "^2.19.13",
|
|
68
|
+
"@types/sinon": "^21.0.1",
|
|
37
69
|
"qunit": "^2.24.1",
|
|
38
|
-
"sinon": "^21.0.0"
|
|
70
|
+
"sinon": "^21.0.0",
|
|
71
|
+
"tsx": "^4.21.0",
|
|
72
|
+
"typescript": "^5.8.3"
|
|
39
73
|
},
|
|
40
74
|
"scripts": {
|
|
41
|
-
"
|
|
75
|
+
"build": "tsc",
|
|
76
|
+
"build:test": "tsc -p tsconfig.test.json",
|
|
77
|
+
"test": "pnpm build && NODE_ENV=test node --import tsx/esm --import ./test/setup.ts node_modules/qunit/bin/qunit.js 'test/**/*-test.ts'"
|
|
42
78
|
}
|
|
43
79
|
}
|