@udibo/oauth2 0.52.0 → 0.53.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/identity/service.d.ts +85 -0
- package/dist/identity/service.js +54 -7
- package/package.json +1 -1
|
@@ -211,6 +211,87 @@ export interface IdentityServiceOptions<User extends IdentityUser> {
|
|
|
211
211
|
* Successful sign-ins are never delayed.
|
|
212
212
|
*/
|
|
213
213
|
failedSignInFloorMs?: number;
|
|
214
|
+
/**
|
|
215
|
+
* Resolve a sign-in whose identifier matches no account, typically by
|
|
216
|
+
* checking the password against the system your users are moving from and
|
|
217
|
+
* creating the account on a match. {@link IdentityService.signIn} calls it
|
|
218
|
+
* only after the sign-in throttle admits the attempt and
|
|
219
|
+
* {@link IdentityUserStore.findByIdentifier} finds no account. It receives
|
|
220
|
+
* the trimmed identifier and the password exactly as submitted.
|
|
221
|
+
*
|
|
222
|
+
* Return the user to sign them in: typically the account you just created
|
|
223
|
+
* for this identifier with this password. The service signs it in only when
|
|
224
|
+
* {@link IdentityUserStore.findByIdentifier} now finds that same account for
|
|
225
|
+
* the identifier and its stored credential accepts the submitted password, so
|
|
226
|
+
* a resolver can never sign in an account the identifier does not reach (a
|
|
227
|
+
* casing variant, an account your store hides from sign-in) or one whose own
|
|
228
|
+
* password differs. On a uniqueness conflict, return `null` or throw rather
|
|
229
|
+
* than the existing row. The service then refuses a locked account under
|
|
230
|
+
* `"enforce"`, as for any other sign-in, and otherwise resets the throttle
|
|
231
|
+
* and the lockout count and emits `sign_in.succeeded`. Return `null` to
|
|
232
|
+
* refuse; an account that fails the check above is refused the same way.
|
|
233
|
+
* A throw
|
|
234
|
+
* also refuses: the service logs that the resolver threw, never the error
|
|
235
|
+
* itself, which may carry the password. A refusal returns the same `null`,
|
|
236
|
+
* runs the same equalizing password hash, emits the same `sign_in.failed`
|
|
237
|
+
* event with reason `unknown_identifier`, and is held to the same
|
|
238
|
+
* {@link failedSignInFloorMs} as an unknown identifier with no resolver.
|
|
239
|
+
*
|
|
240
|
+
* The resolver's own running time adds to the refusal's work, so set
|
|
241
|
+
* {@link failedSignInFloorMs} above its slowest refusal plus one password
|
|
242
|
+
* hash. A refusal after the other system accepted the password (the create
|
|
243
|
+
* failed) is usually the slowest: your request timeout, the hash and write
|
|
244
|
+
* of the create, then the service's hash. Below that, response time tells an
|
|
245
|
+
* unauthenticated caller which identifiers had no local account, or that a
|
|
246
|
+
* guessed password works on the other system. Only the
|
|
247
|
+
* per-identifier throttle bounds how often it runs, and only when a
|
|
248
|
+
* sign-in rate limiter (`rateLimiter` or `rateLimiters.signIn`) is
|
|
249
|
+
* configured: there is no account to lock, the throttle
|
|
250
|
+
* never stops one password tried across many identifiers, and under
|
|
251
|
+
* `protectionMode: "log-only"` it reports without stopping anything. Limit
|
|
252
|
+
* calls inside the resolver when it reaches another system. Compare the
|
|
253
|
+
* other system's answer against the identifier in the same normalized form
|
|
254
|
+
* your store resolves accounts by.
|
|
255
|
+
*
|
|
256
|
+
* @example Create an account from a password check against another system
|
|
257
|
+
* ```ts
|
|
258
|
+
* import {
|
|
259
|
+
* IdentityService,
|
|
260
|
+
* type IdentityUser,
|
|
261
|
+
* type IdentityUserStore,
|
|
262
|
+
* PasswordIdentityService,
|
|
263
|
+
* } from "@udibo/oauth2/identity";
|
|
264
|
+
*
|
|
265
|
+
* interface AppUser extends IdentityUser {
|
|
266
|
+
* email: string;
|
|
267
|
+
* }
|
|
268
|
+
*
|
|
269
|
+
* declare const users: IdentityUserStore<AppUser>;
|
|
270
|
+
* declare function checkPreviousSystem(
|
|
271
|
+
* email: string,
|
|
272
|
+
* password: string,
|
|
273
|
+
* ): Promise<{ email: string } | null>;
|
|
274
|
+
*
|
|
275
|
+
* const passwords = new PasswordIdentityService();
|
|
276
|
+
* const identity = new IdentityService<AppUser>({
|
|
277
|
+
* users,
|
|
278
|
+
* passwords,
|
|
279
|
+
* failedSignInFloorMs: 3_000,
|
|
280
|
+
* resolveUnknownIdentifier: async ({ identifier, password }) => {
|
|
281
|
+
* const profile = await checkPreviousSystem(identifier, password);
|
|
282
|
+
* if (profile?.email !== identifier) return null;
|
|
283
|
+
* return await users.create(
|
|
284
|
+
* { email: profile.email },
|
|
285
|
+
* await passwords.hash(password),
|
|
286
|
+
* );
|
|
287
|
+
* },
|
|
288
|
+
* });
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
291
|
+
resolveUnknownIdentifier?: (input: {
|
|
292
|
+
identifier: string;
|
|
293
|
+
password: string;
|
|
294
|
+
}) => Promise<User | null>;
|
|
214
295
|
/**
|
|
215
296
|
* Session revocation, called on a successful password reset. Optional. When
|
|
216
297
|
* the user store implements {@link IdentityUserStore.resetCredential}, the
|
|
@@ -493,6 +574,10 @@ export declare class IdentityService<User extends IdentityUser> {
|
|
|
493
574
|
* longer (typically a foreign KDF) stays visible by its excess. A success
|
|
494
575
|
* returns as soon as it is done.
|
|
495
576
|
*
|
|
577
|
+
* With {@link IdentityServiceOptions.resolveUnknownIdentifier} set, an
|
|
578
|
+
* identifier that finds no account is offered to it first; a user it returns
|
|
579
|
+
* signs in, and a refusal is the same `null` an unknown identifier gets.
|
|
580
|
+
*
|
|
496
581
|
* @throws {IdentityError} `rate_limited` (with `retryAfterMs`) when a
|
|
497
582
|
* `rateLimiter` is configured, the identifier's limit is hit, and
|
|
498
583
|
* `protectionMode` is `"enforce"`. Rate-limit rejections throw before the
|
package/dist/identity/service.js
CHANGED
|
@@ -70,6 +70,7 @@ export class IdentityService {
|
|
|
70
70
|
#onEvent;
|
|
71
71
|
#legacyVerifiers;
|
|
72
72
|
#failedSignInFloorMs;
|
|
73
|
+
#resolveUnknownIdentifier;
|
|
73
74
|
/** Wires the user store and optional collaborators; applies the TTL and hasher defaults. */
|
|
74
75
|
constructor(options) {
|
|
75
76
|
this.#users = options.users;
|
|
@@ -112,6 +113,7 @@ export class IdentityService {
|
|
|
112
113
|
Number.isFinite(floor) && floor >= 0
|
|
113
114
|
? floor
|
|
114
115
|
: DEFAULT_FAILED_SIGN_IN_FLOOR_MS;
|
|
116
|
+
this.#resolveUnknownIdentifier = options.resolveUnknownIdentifier;
|
|
115
117
|
}
|
|
116
118
|
/**
|
|
117
119
|
* Create a user with a hashed password. `profile` is your app's own fields.
|
|
@@ -148,6 +150,10 @@ export class IdentityService {
|
|
|
148
150
|
* longer (typically a foreign KDF) stays visible by its excess. A success
|
|
149
151
|
* returns as soon as it is done.
|
|
150
152
|
*
|
|
153
|
+
* With {@link IdentityServiceOptions.resolveUnknownIdentifier} set, an
|
|
154
|
+
* identifier that finds no account is offered to it first; a user it returns
|
|
155
|
+
* signs in, and a refusal is the same `null` an unknown identifier gets.
|
|
156
|
+
*
|
|
151
157
|
* @throws {IdentityError} `rate_limited` (with `retryAfterMs`) when a
|
|
152
158
|
* `rateLimiter` is configured, the identifier's limit is hit, and
|
|
153
159
|
* `protectionMode` is `"enforce"`. Rate-limit rejections throw before the
|
|
@@ -163,13 +169,7 @@ export class IdentityService {
|
|
|
163
169
|
});
|
|
164
170
|
const user = await this.#users.findByIdentifier(identifier);
|
|
165
171
|
if (!user) {
|
|
166
|
-
await this.#
|
|
167
|
-
await this.#emit({
|
|
168
|
-
type: "sign_in.failed",
|
|
169
|
-
identifier,
|
|
170
|
-
reason: "unknown_identifier",
|
|
171
|
-
});
|
|
172
|
-
return await this.#rejectSignIn(startedAt);
|
|
172
|
+
return await this.#signInUnknownIdentifier(identifier, input.password, startedAt);
|
|
173
173
|
}
|
|
174
174
|
const lockReported = await this.#reportLock(user.id, identifier);
|
|
175
175
|
if (lockReported && this.#enforce) {
|
|
@@ -198,6 +198,53 @@ export class IdentityService {
|
|
|
198
198
|
await attempt.fail();
|
|
199
199
|
return await this.#rejectSignIn(startedAt);
|
|
200
200
|
}
|
|
201
|
+
return await this.#completeSignIn(user, identifier);
|
|
202
|
+
}
|
|
203
|
+
async #signInUnknownIdentifier(identifier, password, startedAt) {
|
|
204
|
+
const resolved = await this.#tryResolveUnknownIdentifier(identifier, password);
|
|
205
|
+
const user = resolved &&
|
|
206
|
+
(await this.#bindResolvedUser(resolved, identifier, password));
|
|
207
|
+
if (user) {
|
|
208
|
+
if ((await this.#reportLock(user.id, identifier)) && this.#enforce) {
|
|
209
|
+
await this.#equalizeTiming(password);
|
|
210
|
+
return await this.#rejectSignIn(startedAt);
|
|
211
|
+
}
|
|
212
|
+
return await this.#completeSignIn(user, identifier);
|
|
213
|
+
}
|
|
214
|
+
await this.#equalizeTiming(password);
|
|
215
|
+
await this.#emit({
|
|
216
|
+
type: "sign_in.failed",
|
|
217
|
+
identifier,
|
|
218
|
+
reason: "unknown_identifier",
|
|
219
|
+
});
|
|
220
|
+
return await this.#rejectSignIn(startedAt);
|
|
221
|
+
}
|
|
222
|
+
async #tryResolveUnknownIdentifier(identifier, password) {
|
|
223
|
+
if (!this.#resolveUnknownIdentifier)
|
|
224
|
+
return null;
|
|
225
|
+
try {
|
|
226
|
+
return ((await this.#resolveUnknownIdentifier({ identifier, password })) ?? null);
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
// Never log the error object: it may echo the submitted password.
|
|
230
|
+
console.error("[@udibo/oauth2] resolveUnknownIdentifier threw");
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async #bindResolvedUser(resolved, identifier, password) {
|
|
235
|
+
const found = await this.#users.findByIdentifier(identifier);
|
|
236
|
+
const credential = found?.id === resolved.id
|
|
237
|
+
? await this.#users.getCredential(found.id)
|
|
238
|
+
: undefined;
|
|
239
|
+
if (found &&
|
|
240
|
+
credential &&
|
|
241
|
+
(await this.#passwords.verify(password, credential))) {
|
|
242
|
+
return found;
|
|
243
|
+
}
|
|
244
|
+
console.error("[@udibo/oauth2] resolveUnknownIdentifier returned an account the identifier and password do not sign in to; refused");
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
async #completeSignIn(user, identifier) {
|
|
201
248
|
await this.#resetThrottle("signIn", identifier);
|
|
202
249
|
if (this.#lockout)
|
|
203
250
|
await this.#lockout.reset(user.id);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@udibo/oauth2",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.53.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",
|