@udibo/oauth2 0.46.0 → 0.48.0
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/dist/client/direct-client.d.ts +15 -0
- package/dist/client/direct-client.js +92 -34
- package/dist/identity/lockout.d.ts +62 -6
- package/dist/identity/lockout.js +37 -2
- package/dist/identity/service.d.ts +4 -1
- package/dist/identity/service.js +55 -9
- package/dist/identity/token-flow.d.ts +24 -3
- package/dist/identity/token-flow.js +24 -6
- package/dist/react/context.d.ts +4 -1
- package/dist/react/provider.js +8 -2
- package/dist/server/grants/device-authorization.d.ts +6 -0
- package/dist/server/grants/device-authorization.js +25 -4
- package/dist/server/services/device-authorization.d.ts +17 -1
- package/dist/server/services/token.d.ts +17 -0
- package/dist/testing/contract/device-authorization.js +83 -0
- package/dist/testing/contract/device-code-exchange.d.ts +112 -0
- package/dist/testing/contract/device-code-exchange.js +242 -0
- package/dist/testing/contract/lockout-store.d.ts +15 -4
- package/dist/testing/contract/lockout-store.js +77 -7
- package/dist/testing/contract/mod.d.ts +6 -0
- package/dist/testing/contract/mod.js +6 -0
- package/dist/testing/contract/token-flow-store.d.ts +10 -0
- package/dist/testing/contract/token-flow-store.js +47 -0
- package/dist/testing/services.d.ts +26 -6
- package/dist/testing/services.js +55 -14
- package/package.json +1 -1
|
@@ -2,10 +2,14 @@
|
|
|
2
2
|
* Contract test suite for {@link LockoutStore} implementations — the
|
|
3
3
|
* per-account failure counter behind `AccountLockout`.
|
|
4
4
|
*
|
|
5
|
-
* `AccountLockout`
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* `AccountLockout` counts each sign-in attempt with `increment` before its
|
|
6
|
+
* password is checked, and admits the attempt only on the count `increment`
|
|
7
|
+
* returns. A store that lets concurrent attempts share a count admits one extra
|
|
8
|
+
* guess for every attempt sharing it: the brute-force floor the lockout is
|
|
9
|
+
* meant to be moves up by however many attempts an attacker can land at once.
|
|
10
|
+
*
|
|
11
|
+
* The optional `lock` cases run when the store implements it and are skipped,
|
|
12
|
+
* with a note naming the gap, when it does not.
|
|
9
13
|
*
|
|
10
14
|
* @example Verify a database-backed store
|
|
11
15
|
* ```ts
|
|
@@ -22,7 +26,7 @@
|
|
|
22
26
|
*
|
|
23
27
|
* @module
|
|
24
28
|
*/
|
|
25
|
-
import { beforeEach, describe, expect, it } from "vitest";
|
|
29
|
+
import { assert, beforeEach, describe, expect, it } from "vitest";
|
|
26
30
|
/**
|
|
27
31
|
* Runs the {@link LockoutStore} contract suite. Call it from your own test
|
|
28
32
|
* file — it registers `describe` / `it` blocks the test runner picks up.
|
|
@@ -78,10 +82,18 @@ export function runLockoutStoreContractTests(options) {
|
|
|
78
82
|
});
|
|
79
83
|
it("gives every concurrent failure its own count", async () => {
|
|
80
84
|
const results = await Promise.all(Array.from({ length: 5 }, () => store.increment(USER, now)));
|
|
81
|
-
expect(results.map((result) => result.failures).sort((a, b) => a - b), "
|
|
82
|
-
"sharing a count
|
|
85
|
+
expect(results.map((result) => result.failures).sort((a, b) => a - b), "sign-in is admitted on the returned count — concurrent attempts " +
|
|
86
|
+
"sharing a count each get another guess past the threshold").toStrictEqual([1, 2, 3, 4, 5]);
|
|
83
87
|
expect((await store.get(USER))?.failures).toStrictEqual(5);
|
|
84
88
|
});
|
|
89
|
+
it("gives every concurrent attempt against an active lock its own count", async () => {
|
|
90
|
+
const lockedUntil = now + 60_000;
|
|
91
|
+
await store.set(USER, { failures: 3, lockedUntil });
|
|
92
|
+
const results = await Promise.all(Array.from({ length: 4 }, () => store.increment(USER, now)));
|
|
93
|
+
expect(results.map((result) => result.failures).sort((a, b) => a - b)).toStrictEqual([4, 5, 6, 7]);
|
|
94
|
+
expect(results.map((result) => result.lockedUntil), "every attempt counted during the lock must see it, or one is " +
|
|
95
|
+
"admitted to the password check").toStrictEqual([lockedUntil, lockedUntil, lockedUntil, lockedUntil]);
|
|
96
|
+
});
|
|
85
97
|
});
|
|
86
98
|
describe("set", () => {
|
|
87
99
|
it("persists the record as given", async () => {
|
|
@@ -99,6 +111,64 @@ export function runLockoutStoreContractTests(options) {
|
|
|
99
111
|
expect(record?.lockedUntil).toBe(undefined);
|
|
100
112
|
});
|
|
101
113
|
});
|
|
114
|
+
describe("lock (optional)", () => {
|
|
115
|
+
const NOT_IMPLEMENTED = "lock is not implemented — AccountLockout falls back to set, so an " +
|
|
116
|
+
"over-budget attempt can re-lock an account a concurrent sign-in reset";
|
|
117
|
+
if (options.lock) {
|
|
118
|
+
it("is implemented", () => {
|
|
119
|
+
assert(typeof store.lock === "function", `${NOT_IMPLEMENTED}. Implement it, or drop \`lock: true\`.`);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
it("locks the account while its count stands, keeping the count", async ({ skip, }) => {
|
|
123
|
+
if (!store.lock)
|
|
124
|
+
return skip(NOT_IMPLEMENTED);
|
|
125
|
+
for (let i = 0; i < 3; i++)
|
|
126
|
+
await store.increment(USER, now);
|
|
127
|
+
const lockedUntil = now + 60_000;
|
|
128
|
+
expect(await store.lock(USER, { failures: 3, lockedUntil }, now)).toBe(true);
|
|
129
|
+
const record = await store.get(USER);
|
|
130
|
+
expect(record?.failures).toStrictEqual(3);
|
|
131
|
+
expect(record?.lockedUntil).toBe(lockedUntil);
|
|
132
|
+
});
|
|
133
|
+
it("locks when later attempts counted past the lock's own count", async ({ skip, }) => {
|
|
134
|
+
if (!store.lock)
|
|
135
|
+
return skip(NOT_IMPLEMENTED);
|
|
136
|
+
for (let i = 0; i < 5; i++)
|
|
137
|
+
await store.increment(USER, now);
|
|
138
|
+
expect(await store.lock(USER, { failures: 3, lockedUntil: now + 60_000 }, now)).toBe(true);
|
|
139
|
+
expect((await store.get(USER))?.failures).toStrictEqual(5);
|
|
140
|
+
});
|
|
141
|
+
it("refuses once the account was cleared", async ({ skip }) => {
|
|
142
|
+
if (!store.lock)
|
|
143
|
+
return skip(NOT_IMPLEMENTED);
|
|
144
|
+
for (let i = 0; i < 3; i++)
|
|
145
|
+
await store.increment(USER, now);
|
|
146
|
+
await store.clear(USER);
|
|
147
|
+
expect(await store.lock(USER, { failures: 3, lockedUntil: now + 60_000 }, now), "a lock computed before a successful sign-in must not land after it").toBe(false);
|
|
148
|
+
expect(await store.get(USER)).toBe(undefined);
|
|
149
|
+
});
|
|
150
|
+
it("refuses once a fresh count started below the lock's count", async ({ skip, }) => {
|
|
151
|
+
if (!store.lock)
|
|
152
|
+
return skip(NOT_IMPLEMENTED);
|
|
153
|
+
for (let i = 0; i < 3; i++)
|
|
154
|
+
await store.increment(USER, now);
|
|
155
|
+
await store.clear(USER);
|
|
156
|
+
await store.increment(USER, now);
|
|
157
|
+
expect(await store.lock(USER, { failures: 3, lockedUntil: now + 60_000 }, now)).toBe(false);
|
|
158
|
+
const record = await store.get(USER);
|
|
159
|
+
expect(record?.failures).toStrictEqual(1);
|
|
160
|
+
expect(record?.lockedUntil).toBe(undefined);
|
|
161
|
+
});
|
|
162
|
+
it("locks only the account it is given", async ({ skip }) => {
|
|
163
|
+
if (!store.lock)
|
|
164
|
+
return skip(NOT_IMPLEMENTED);
|
|
165
|
+
for (let i = 0; i < 3; i++)
|
|
166
|
+
await store.increment(USER, now);
|
|
167
|
+
await store.increment(OTHER_USER, now);
|
|
168
|
+
await store.lock(USER, { failures: 3, lockedUntil: now + 60_000 }, now);
|
|
169
|
+
expect((await store.get(OTHER_USER))?.lockedUntil).toBe(undefined);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
102
172
|
describe("clear", () => {
|
|
103
173
|
it("drops the account's failures and lock", async () => {
|
|
104
174
|
await store.set(USER, { failures: 10, lockedUntil: now + 60_000 });
|
|
@@ -36,6 +36,11 @@
|
|
|
36
36
|
* and an exchange whose insert fails. A store that awaits between its claim
|
|
37
37
|
* and its insert wires `CodeExchangeBarriers` into `exchangeCode`.
|
|
38
38
|
*
|
|
39
|
+
* `TokenServiceInterface.exchangeDeviceCode` has one as well: concurrent
|
|
40
|
+
* exchanges of one approved device code, an exchange of a claimed
|
|
41
|
+
* authorization, and an exchange whose insert fails, with
|
|
42
|
+
* `DeviceCodeExchangeBarriers` wired the same way.
|
|
43
|
+
*
|
|
39
44
|
* The service runners take a factory plus seed helpers because `add` /
|
|
40
45
|
* `register` methods aren't part of the public service interfaces — the
|
|
41
46
|
* consumer tells the runner how to populate test data.
|
|
@@ -47,6 +52,7 @@ export { type ClientServiceContractOptions, runClientServiceContractTests, } fro
|
|
|
47
52
|
export { runTokenServiceContractTests, type TokenServiceContractOptions, } from "./token.ts";
|
|
48
53
|
export { runTokenRotationContractTests, type TokenRotationBarriers, type TokenRotationContractOptions, type TokenRotationContractServices, } from "./token-rotation.ts";
|
|
49
54
|
export { type CodeExchangeBarriers, type CodeExchangeContractOptions, type CodeExchangeContractServices, runCodeExchangeContractTests, } from "./code-exchange.ts";
|
|
55
|
+
export { type DeviceCodeExchangeBarriers, type DeviceCodeExchangeContractOptions, type DeviceCodeExchangeContractServices, runDeviceCodeExchangeContractTests, } from "./device-code-exchange.ts";
|
|
50
56
|
export { runTokenReaderContractTests, type TokenReaderContractFixture, type TokenReaderContractOptions, } from "./token-reader.ts";
|
|
51
57
|
export { type AuthorizationCodeServiceContractOptions, runAuthorizationCodeServiceContractTests, } from "./authorization-code.ts";
|
|
52
58
|
export { type DeviceAuthorizationServiceContractOptions, runDeviceAuthorizationServiceContractTests, } from "./device-authorization.ts";
|
|
@@ -36,6 +36,11 @@
|
|
|
36
36
|
* and an exchange whose insert fails. A store that awaits between its claim
|
|
37
37
|
* and its insert wires `CodeExchangeBarriers` into `exchangeCode`.
|
|
38
38
|
*
|
|
39
|
+
* `TokenServiceInterface.exchangeDeviceCode` has one as well: concurrent
|
|
40
|
+
* exchanges of one approved device code, an exchange of a claimed
|
|
41
|
+
* authorization, and an exchange whose insert fails, with
|
|
42
|
+
* `DeviceCodeExchangeBarriers` wired the same way.
|
|
43
|
+
*
|
|
39
44
|
* The service runners take a factory plus seed helpers because `add` /
|
|
40
45
|
* `register` methods aren't part of the public service interfaces — the
|
|
41
46
|
* consumer tells the runner how to populate test data.
|
|
@@ -47,6 +52,7 @@ export { runClientServiceContractTests, } from "./client.js";
|
|
|
47
52
|
export { runTokenServiceContractTests, } from "./token.js";
|
|
48
53
|
export { runTokenRotationContractTests, } from "./token-rotation.js";
|
|
49
54
|
export { runCodeExchangeContractTests, } from "./code-exchange.js";
|
|
55
|
+
export { runDeviceCodeExchangeContractTests, } from "./device-code-exchange.js";
|
|
50
56
|
export { runTokenReaderContractTests, } from "./token-reader.js";
|
|
51
57
|
export { runAuthorizationCodeServiceContractTests, } from "./authorization-code.js";
|
|
52
58
|
export { runDeviceAuthorizationServiceContractTests, } from "./device-authorization.js";
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* single-use, and only a conditional write survives concurrent requests
|
|
7
7
|
* redeeming one link. This suite races eight of them on one link.
|
|
8
8
|
*
|
|
9
|
+
* The optional `replacePending` cases run when the store implements it and
|
|
10
|
+
* are skipped, with a note naming the gap, when it does not.
|
|
11
|
+
*
|
|
9
12
|
* @example Verify a database-backed store
|
|
10
13
|
* ```ts
|
|
11
14
|
* import { runTokenFlowStoreContractTests } from "@udibo/oauth2/testing/contract";
|
|
@@ -38,6 +41,13 @@ export interface TokenFlowStoreContractOptions {
|
|
|
38
41
|
* that meant to implement it and does not fails loudly.
|
|
39
42
|
*/
|
|
40
43
|
deleteBySubject?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Set to `true` to require the optional
|
|
46
|
+
* {@link TokenFlowStore.replacePending}: the suite then fails a store that
|
|
47
|
+
* does not implement it. Left unset, the `replacePending` cases run when the
|
|
48
|
+
* store implements it and are skipped with a note when it does not.
|
|
49
|
+
*/
|
|
50
|
+
replacePending?: boolean;
|
|
41
51
|
/**
|
|
42
52
|
* The subject the cases issue tokens for, in your application's id format. A
|
|
43
53
|
* store whose rows reference a users table needs it to name a user that
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* single-use, and only a conditional write survives concurrent requests
|
|
7
7
|
* redeeming one link. This suite races eight of them on one link.
|
|
8
8
|
*
|
|
9
|
+
* The optional `replacePending` cases run when the store implements it and
|
|
10
|
+
* are skipped, with a note naming the gap, when it does not.
|
|
11
|
+
*
|
|
9
12
|
* @example Verify a database-backed store
|
|
10
13
|
* ```ts
|
|
11
14
|
* import { runTokenFlowStoreContractTests } from "@udibo/oauth2/testing/contract";
|
|
@@ -133,6 +136,50 @@ export function runTokenFlowStoreContractTests(options) {
|
|
|
133
136
|
(await store.get(hash("2")))?.consumedAt !== undefined, "neither stamp may be lost to the other's stale read");
|
|
134
137
|
});
|
|
135
138
|
});
|
|
139
|
+
describe("replacePending (optional)", () => {
|
|
140
|
+
const NOT_IMPLEMENTED = "replacePending is not implemented — TokenFlowService.create with " +
|
|
141
|
+
"invalidateExisting deletes then saves, so concurrent requests can " +
|
|
142
|
+
"each leave a live link";
|
|
143
|
+
if (options.replacePending) {
|
|
144
|
+
it("is implemented", () => {
|
|
145
|
+
assert(typeof store.replacePending === "function", `${NOT_IMPLEMENTED}. Implement it, or drop \`replacePending: true\`.`);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
it("replaces the pair's pending records with the new one", async ({ skip, }) => {
|
|
149
|
+
if (!store.replacePending)
|
|
150
|
+
return skip(NOT_IMPLEMENTED);
|
|
151
|
+
await store.save(record({ tokenHash: hash("old") }));
|
|
152
|
+
await store.replacePending(record({ tokenHash: hash("new") }));
|
|
153
|
+
expect(await store.get(hash("old"))).toBe(null);
|
|
154
|
+
expect((await store.get(hash("new")))?.tokenHash).toBe(hash("new"));
|
|
155
|
+
});
|
|
156
|
+
it("keeps consumed records, other subjects and other purposes", async ({ skip, }) => {
|
|
157
|
+
if (!store.replacePending)
|
|
158
|
+
return skip(NOT_IMPLEMENTED);
|
|
159
|
+
await store.save(record({ tokenHash: hash("used") }));
|
|
160
|
+
expect(await store.markConsumed(hash("used"), Date.now())).toBe(true);
|
|
161
|
+
await store.save(record({ tokenHash: hash("other-subject"), subject: OTHER_SUBJECT }));
|
|
162
|
+
await store.save(record({ tokenHash: hash("other-purpose"), purpose: OTHER_PURPOSE }));
|
|
163
|
+
await store.replacePending(record({ tokenHash: hash("new") }));
|
|
164
|
+
assert((await store.get(hash("used")))?.consumedAt !== undefined, "a consumed record must survive so inspect() can answer `consumed`");
|
|
165
|
+
assert((await store.get(hash("other-subject"))) !== null);
|
|
166
|
+
assert((await store.get(hash("other-purpose"))) !== null);
|
|
167
|
+
});
|
|
168
|
+
it(`leaves exactly one of ${CONCURRENT_CALLERS} concurrent replacements pending`, async ({ skip, }) => {
|
|
169
|
+
if (!store.replacePending)
|
|
170
|
+
return skip(NOT_IMPLEMENTED);
|
|
171
|
+
const replace = store.replacePending.bind(store);
|
|
172
|
+
await race((caller) => replace(record({ tokenHash: hash(`racer-${caller}`) })));
|
|
173
|
+
const pending = [];
|
|
174
|
+
for (let caller = 0; caller < CONCURRENT_CALLERS; caller++) {
|
|
175
|
+
const stored = await store.get(hash(`racer-${caller}`));
|
|
176
|
+
if (stored && stored.consumedAt === undefined)
|
|
177
|
+
pending.push(stored);
|
|
178
|
+
}
|
|
179
|
+
expect(pending.length, "each replacement invalidates the others — interleaved deletes and " +
|
|
180
|
+
"inserts leave several live links to one mailbox").toBe(1);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
136
183
|
if (expectDeleteBySubject) {
|
|
137
184
|
describe("deleteBySubject", () => {
|
|
138
185
|
it("is implemented", () => {
|
|
@@ -157,6 +157,15 @@ export interface MemoryTokenServiceOptions<C extends ClientInterface, U extends
|
|
|
157
157
|
* map, so a subclass that overrides the code store's `revoke` is bypassed.
|
|
158
158
|
*/
|
|
159
159
|
authorizationCodeService?: MemoryAuthorizationCodeService<C, U, S>;
|
|
160
|
+
/**
|
|
161
|
+
* The device-authorization store the device grant claims from. Pass it to
|
|
162
|
+
* give the token service {@linkcode MemoryTokenService.exchangeDeviceCode},
|
|
163
|
+
* so the grant claims an approved device code and saves its token in one
|
|
164
|
+
* step; without it the grant claims and saves in two.
|
|
165
|
+
* `exchangeDeviceCode` claims from the store's own maps, so a subclass that
|
|
166
|
+
* overrides the store's `revoke` is bypassed.
|
|
167
|
+
*/
|
|
168
|
+
deviceAuthorizationService?: MemoryDeviceAuthorizationService<C, U, S>;
|
|
160
169
|
}
|
|
161
170
|
/**
|
|
162
171
|
* In-memory `TokenServiceInterface` implementation. Tokens are hashed on
|
|
@@ -187,11 +196,20 @@ export declare class MemoryTokenService<C extends ClientInterface, U extends Mem
|
|
|
187
196
|
* two calls for a token service that has no code store to claim from.
|
|
188
197
|
*/
|
|
189
198
|
readonly exchangeCode?: (authorizationCode: AuthorizationCode<C, U, S>, token: Token<C, U, S>) => Promise<Token<C, U, S> | undefined>;
|
|
199
|
+
/**
|
|
200
|
+
* Claims an approved authorization from the paired
|
|
201
|
+
* `deviceAuthorizationService` and saves the token issued from it in one
|
|
202
|
+
* step, or does neither, resolving `undefined` when the authorization was
|
|
203
|
+
* already claimed. Present only when the service was constructed with
|
|
204
|
+
* `deviceAuthorizationService`.
|
|
205
|
+
*/
|
|
206
|
+
readonly exchangeDeviceCode?: (authorization: DeviceAuthorization<C, U, S>, token: Token<C, U, S>) => Promise<Token<C, U, S> | undefined>;
|
|
190
207
|
/**
|
|
191
208
|
* Creates an empty token store.
|
|
192
209
|
*
|
|
193
210
|
* @param options Backing client/user services, optional default token
|
|
194
|
-
* lifetimes, and the code
|
|
211
|
+
* lifetimes, and the code and device stores that enable `exchangeCode` and
|
|
212
|
+
* `exchangeDeviceCode`.
|
|
195
213
|
*/
|
|
196
214
|
constructor(options: MemoryTokenServiceOptions<C, U, S>);
|
|
197
215
|
/**
|
|
@@ -393,14 +411,16 @@ export declare class MemoryDeviceAuthorizationService<C extends ClientInterface,
|
|
|
393
411
|
*/
|
|
394
412
|
getByUserCode(userCode: string): Promise<DeviceAuthorization<C, U, S> | undefined>;
|
|
395
413
|
/**
|
|
396
|
-
* Marks the authorization granted by `user`, optionally narrowing
|
|
397
|
-
* so a subsequent device-code poll can exchange it for a token.
|
|
398
|
-
* updated record. Throws when the authorization is not stored
|
|
414
|
+
* Marks the pending authorization granted by `user`, optionally narrowing
|
|
415
|
+
* `scope`, so a subsequent device-code poll can exchange it for a token.
|
|
416
|
+
* Returns the updated record. Throws when the authorization is not stored,
|
|
417
|
+
* has expired, or was already approved or denied.
|
|
399
418
|
*/
|
|
400
419
|
approve(authorization: DeviceAuthorization<C, U, S>, user: U, scope?: S | null): Promise<DeviceAuthorization<C, U, S>>;
|
|
401
420
|
/**
|
|
402
|
-
* Marks the authorization denied so a subsequent poll is rejected.
|
|
403
|
-
* the updated record. Throws when the authorization is not stored
|
|
421
|
+
* Marks the pending authorization denied so a subsequent poll is rejected.
|
|
422
|
+
* Returns the updated record. Throws when the authorization is not stored,
|
|
423
|
+
* has expired, or was already approved or denied.
|
|
404
424
|
*/
|
|
405
425
|
deny(authorization: DeviceAuthorization<C, U, S>): Promise<DeviceAuthorization<C, U, S>>;
|
|
406
426
|
/**
|
package/dist/testing/services.js
CHANGED
|
@@ -216,11 +216,20 @@ export class MemoryTokenService {
|
|
|
216
216
|
* two calls for a token service that has no code store to claim from.
|
|
217
217
|
*/
|
|
218
218
|
exchangeCode;
|
|
219
|
+
/**
|
|
220
|
+
* Claims an approved authorization from the paired
|
|
221
|
+
* `deviceAuthorizationService` and saves the token issued from it in one
|
|
222
|
+
* step, or does neither, resolving `undefined` when the authorization was
|
|
223
|
+
* already claimed. Present only when the service was constructed with
|
|
224
|
+
* `deviceAuthorizationService`.
|
|
225
|
+
*/
|
|
226
|
+
exchangeDeviceCode;
|
|
219
227
|
/**
|
|
220
228
|
* Creates an empty token store.
|
|
221
229
|
*
|
|
222
230
|
* @param options Backing client/user services, optional default token
|
|
223
|
-
* lifetimes, and the code
|
|
231
|
+
* lifetimes, and the code and device stores that enable `exchangeCode` and
|
|
232
|
+
* `exchangeDeviceCode`.
|
|
224
233
|
*/
|
|
225
234
|
constructor(options) {
|
|
226
235
|
this.#clientService = options.clientService;
|
|
@@ -233,6 +242,18 @@ export class MemoryTokenService {
|
|
|
233
242
|
if (codes) {
|
|
234
243
|
this.exchangeCode = (authorizationCode, token) => this.#exchangeCode(codes, authorizationCode, token);
|
|
235
244
|
}
|
|
245
|
+
const devices = options.deviceAuthorizationService;
|
|
246
|
+
if (devices) {
|
|
247
|
+
this.exchangeDeviceCode = (authorization, token) => this.#exchangeDeviceCode(devices, authorization, token);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
async #exchangeDeviceCode(devices, authorization, token) {
|
|
251
|
+
const deviceCodeHash = await sha256Hash(authorization.deviceCode);
|
|
252
|
+
const stored = await this.#prepare(token);
|
|
253
|
+
if (!claimStoredDeviceCode(devices, deviceCodeHash))
|
|
254
|
+
return undefined;
|
|
255
|
+
this.#store(stored);
|
|
256
|
+
return token;
|
|
236
257
|
}
|
|
237
258
|
async #exchangeCode(codes, authorizationCode, token) {
|
|
238
259
|
const codeHash = await sha256Hash(authorizationCode.code);
|
|
@@ -639,6 +660,7 @@ export class MemoryAuthorizationCodeService {
|
|
|
639
660
|
return this.#codes.delete(await sha256Hash(code));
|
|
640
661
|
}
|
|
641
662
|
}
|
|
663
|
+
let claimStoredDeviceCode;
|
|
642
664
|
/** In-memory `DeviceAuthorizationServiceInterface` implementation. */
|
|
643
665
|
export class MemoryDeviceAuthorizationService {
|
|
644
666
|
/** Device-authorization lifetime in seconds applied by {@linkcode expiresAt}. */
|
|
@@ -649,6 +671,16 @@ export class MemoryDeviceAuthorizationService {
|
|
|
649
671
|
#userService;
|
|
650
672
|
#byDeviceCode = new Map();
|
|
651
673
|
#byUserCode = new Map();
|
|
674
|
+
static {
|
|
675
|
+
claimStoredDeviceCode = (service, deviceCodeHash) => {
|
|
676
|
+
const stored = service.#byDeviceCode.get(deviceCodeHash);
|
|
677
|
+
if (!stored)
|
|
678
|
+
return false;
|
|
679
|
+
service.#byDeviceCode.delete(deviceCodeHash);
|
|
680
|
+
service.#byUserCode.delete(stored.userCodeHash);
|
|
681
|
+
return true;
|
|
682
|
+
};
|
|
683
|
+
}
|
|
652
684
|
/**
|
|
653
685
|
* Creates an empty device-authorization store.
|
|
654
686
|
*
|
|
@@ -763,16 +795,27 @@ export class MemoryDeviceAuthorizationService {
|
|
|
763
795
|
}
|
|
764
796
|
return undefined;
|
|
765
797
|
}
|
|
766
|
-
|
|
767
|
-
* Marks the authorization granted by `user`, optionally narrowing `scope`,
|
|
768
|
-
* so a subsequent device-code poll can exchange it for a token. Returns the
|
|
769
|
-
* updated record. Throws when the authorization is not stored.
|
|
770
|
-
*/
|
|
771
|
-
async approve(authorization, user, scope) {
|
|
798
|
+
async #findPending(authorization, method) {
|
|
772
799
|
const stored = await this.#findStored(authorization);
|
|
773
800
|
if (!stored) {
|
|
774
|
-
throw new Error(
|
|
801
|
+
throw new Error(`${method}: device authorization not found`);
|
|
802
|
+
}
|
|
803
|
+
if (stored.authorized || stored.denied) {
|
|
804
|
+
throw new Error(`${method}: device authorization already decided`);
|
|
775
805
|
}
|
|
806
|
+
if (stored.expiresAt.getTime() <= Date.now()) {
|
|
807
|
+
throw new Error(`${method}: device authorization expired`);
|
|
808
|
+
}
|
|
809
|
+
return stored;
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Marks the pending authorization granted by `user`, optionally narrowing
|
|
813
|
+
* `scope`, so a subsequent device-code poll can exchange it for a token.
|
|
814
|
+
* Returns the updated record. Throws when the authorization is not stored,
|
|
815
|
+
* has expired, or was already approved or denied.
|
|
816
|
+
*/
|
|
817
|
+
async approve(authorization, user, scope) {
|
|
818
|
+
const stored = await this.#findPending(authorization, "approve");
|
|
776
819
|
stored.authorized = true;
|
|
777
820
|
stored.userId = user.id;
|
|
778
821
|
if (scope !== undefined)
|
|
@@ -785,14 +828,12 @@ export class MemoryDeviceAuthorizationService {
|
|
|
785
828
|
};
|
|
786
829
|
}
|
|
787
830
|
/**
|
|
788
|
-
* Marks the authorization denied so a subsequent poll is rejected.
|
|
789
|
-
* the updated record. Throws when the authorization is not stored
|
|
831
|
+
* Marks the pending authorization denied so a subsequent poll is rejected.
|
|
832
|
+
* Returns the updated record. Throws when the authorization is not stored,
|
|
833
|
+
* has expired, or was already approved or denied.
|
|
790
834
|
*/
|
|
791
835
|
async deny(authorization) {
|
|
792
|
-
const stored = await this.#
|
|
793
|
-
if (!stored) {
|
|
794
|
-
throw new Error("deny: device authorization not found");
|
|
795
|
-
}
|
|
836
|
+
const stored = await this.#findPending(authorization, "deny");
|
|
796
837
|
stored.denied = true;
|
|
797
838
|
return { ...authorization, denied: true };
|
|
798
839
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@udibo/oauth2",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.48.0",
|
|
4
4
|
"description": "A flexible OAuth2 toolkit: framework-agnostic authorization & resource servers, a typed client, Hono adapters (including a backend-for-frontend), Express adapters, and React bindings.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"authorization-server",
|