@softure-ai/auth 0.2.17 → 0.2.18
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/CHANGELOG.md +9 -0
- package/README.md +9 -3
- package/dist/index.js +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/reauthenticate.d.ts +17 -0
- package/dist/server/reauthenticate.d.ts.map +1 -1
- package/dist/server/reauthenticate.js +40 -3
- package/dist/server/reauthenticate.js.map +1 -1
- package/module.json +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/server/index.ts +1 -1
- package/src/server/reauthenticate.ts +57 -3
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,15 @@ Newest first. Each version lists what changed for an app that uses `@softure-ai/
|
|
|
4
4
|
production, the version gets a line `verified in: <app>@<commit>` ([docs/05](../../docs/05-adoption-playbook.md),
|
|
5
5
|
"Definition of done"). Versions before the first one below are described in their GitHub Releases (`auth@x.y.z`).
|
|
6
6
|
|
|
7
|
+
## 0.2.18
|
|
8
|
+
|
|
9
|
+
- **Re-authentication by a provider sign-in:** `findRecentProviderSignIn(ctx, { userId, sessionToken, withinMinutes })`
|
|
10
|
+
(`/server`) answers the provider whose sign-in opened that very session within the window, or null (a session a
|
|
11
|
+
password login opened, an older one, another user's or an expired one). `findLinkedProviders(ctx, userId)` lists the
|
|
12
|
+
providers an account signs in with. An account a provider created has no password its owner knows; these let an
|
|
13
|
+
action that cannot be undone, such as `@softure-ai/privacy`'s account deletion, confirm it is the owner.
|
|
14
|
+
- Migrate: nothing.
|
|
15
|
+
|
|
7
16
|
## 0.2.17
|
|
8
17
|
|
|
9
18
|
- **Open sign-up through a provider:** `openSignUp: true` on an OpenID Connect or GitHub provider lets any verified email
|
package/README.md
CHANGED
|
@@ -866,7 +866,12 @@ contributes to `@softure-ai/privacy` (`privacy` flags on):
|
|
|
866
866
|
depend on it, and the app's contributors before all modules.
|
|
867
867
|
|
|
868
868
|
`isCurrentPassword(ctx, userId, password)` (`/server`) checks the password again before an action
|
|
869
|
-
that cannot be undone, such as the account deletion; the caller counts the attempt first.
|
|
869
|
+
that cannot be undone, such as the account deletion; the caller counts the attempt first. An account a
|
|
870
|
+
provider created has no password its owner knows, so it re-authenticates by signing in with the
|
|
871
|
+
provider again: `findRecentProviderSignIn(ctx, { userId, sessionToken, withinMinutes })` answers the
|
|
872
|
+
provider whose sign-in opened that very session within the window (a session a password login
|
|
873
|
+
opened, an older one or another user's is null), and `findLinkedProviders(ctx, userId)` lists the
|
|
874
|
+
providers the account signs in with.
|
|
870
875
|
|
|
871
876
|
Rate limit rows of `security` hold only SHA-256 prefixes of the email and user id
|
|
872
877
|
(`subjectKey`), pruned two windows after they start; they are not exported or deleted.
|
|
@@ -892,7 +897,8 @@ Rate limit rows of `security` hold only SHA-256 prefixes of the email and user i
|
|
|
892
897
|
at most N accounts keeps registration closed.
|
|
893
898
|
- OpenID Connect: no Front-Channel or Back-Channel Logout (a sign-out at the provider leaves the app's session), no
|
|
894
899
|
refresh of the account's email from the provider (`users.email` keeps the first one; `identities.email` follows),
|
|
895
|
-
and `isCurrentPassword` cannot re-check an account that has no password of its own
|
|
896
|
-
configured, the owner of an account created by a sign-in
|
|
900
|
+
and `isCurrentPassword` cannot re-check an account that has no password of its own (use
|
|
901
|
+
`findRecentProviderSignIn`). With passwords on and reset configured, the owner of an account created by a sign-in
|
|
902
|
+
can still set a password through a reset link.
|
|
897
903
|
- A registration attempt reveals whether an email has an account (`auth.email_taken`); the
|
|
898
904
|
`register` rate limit bounds how fast anyone can ask.
|
package/dist/index.js
CHANGED
|
@@ -40,7 +40,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
|
|
|
40
40
|
export const auth = defineModule({
|
|
41
41
|
manifest: {
|
|
42
42
|
id: MODULE_ID,
|
|
43
|
-
version: "0.2.
|
|
43
|
+
version: "0.2.18",
|
|
44
44
|
runtimes: ["node", "workers"],
|
|
45
45
|
dependsOn: { security: "^0.2.0", mailing: "^0.2.0?", ops: "^0.2.0?", jobs: "^0.1.1?" },
|
|
46
46
|
dbSchema: "auth",
|
package/dist/server/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needs
|
|
|
7
7
|
export { deliverPasswordReset, findPasswordResetUser, getPasswordResetLink, isPasswordResetEnabled, issuePasswordReset, prunePasswordResets, requestPasswordReset, resetPassword, RESET_TOKEN_PARAM, sendPasswordReset, type PasswordResetRequestInput, type PasswordResetRequestResult, type ResetPasswordErrorCode, type ResetPasswordInput, type ResetPasswordResult, } from "./password-reset.js";
|
|
8
8
|
export { assertDeclaredRole, findUserRoles, getDeclaredRoles, grantRole, isConfiguredAdmin, isDeclaredRole, revokeRole, type RoleChange, type RoleErrorCode, } from "./roles.js";
|
|
9
9
|
export { authPrivacyContributor, deleteAuthUserData, exportAuthUserData, type AuthUserData } from "./privacy.js";
|
|
10
|
-
export { isCurrentPassword } from "./reauthenticate.js";
|
|
10
|
+
export { findLinkedProviders, findRecentProviderSignIn, isCurrentPassword, type RecentProviderSignInInput } from "./reauthenticate.js";
|
|
11
11
|
export { registerUser, type RegisterErrorCode, type RegisterInput, type RegisterResult } from "./register.js";
|
|
12
12
|
export { createSession, findSessionUser, logoutSession, pruneSessions, revokeUserSessions, type AuthContext, type RevokeUserSessionsOptions, } from "./sessions.js";
|
|
13
13
|
export { isRegistrationClosed } from "./switches.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,KAAK,uBAAuB,EAAE,KAAK,mBAAmB,EAAE,KAAK,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACzI,OAAO,EAAE,SAAS,EAAE,KAAK,UAAU,EAAE,KAAK,WAA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AAO,EACL,kBAAkB,EAClB,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,iBAAiB,EACjB,cAAc,EACd,UAAU,EACV,KAAK,UAAU,EACf,KAAK,aAAa,GACnB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AACjH,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,KAAK,yBAAyB,EAAE,MAAM,qBAAqB,CAAC;AACvI,OAAO,EAAE,YAAY,EAAE,KAAK,iBAAiB,EAAE,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;AAC9G,OAAO,EACL,aAAa,EACb,eAAe,EACf,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,KAAK,WAAW,EAChB,KAAK,yBAAyB,GAC/B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,KAAK,wBAAwB,EAAE,MAAM,yBAAyB,CAAC"}
|
package/dist/server/index.js
CHANGED
|
@@ -9,7 +9,7 @@ export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needs
|
|
|
9
9
|
export { deliverPasswordReset, findPasswordResetUser, getPasswordResetLink, isPasswordResetEnabled, issuePasswordReset, prunePasswordResets, requestPasswordReset, resetPassword, RESET_TOKEN_PARAM, sendPasswordReset, } from "./password-reset.js";
|
|
10
10
|
export { assertDeclaredRole, findUserRoles, getDeclaredRoles, grantRole, isConfiguredAdmin, isDeclaredRole, revokeRole, } from "./roles.js";
|
|
11
11
|
export { authPrivacyContributor, deleteAuthUserData, exportAuthUserData } from "./privacy.js";
|
|
12
|
-
export { isCurrentPassword } from "./reauthenticate.js";
|
|
12
|
+
export { findLinkedProviders, findRecentProviderSignIn, isCurrentPassword } from "./reauthenticate.js";
|
|
13
13
|
export { registerUser } from "./register.js";
|
|
14
14
|
export { createSession, findSessionUser, logoutSession, pruneSessions, revokeUserSessions, } from "./sessions.js";
|
|
15
15
|
export { isRegistrationClosed } from "./switches.js";
|
package/dist/server/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,qGAAqG;AACrG,OAAO,EAAE,cAAc,EAAqF,MAAM,sBAAsB,CAAC;AACzI,OAAO,EAAE,SAAS,EAAqC,MAAM,YAAY,CAAC;AAC1E,OAAO,EACL,kBAAkB,GAKnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,eAAe,EACf,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,GAShB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,cAAc,EAAE,aAAa,EAAmB,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,cAAc,EAAsB,MAAM,eAAe,CAAC;AAClJ,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,aAAa,EACb,iBAAiB,EACjB,iBAAiB,GAMlB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,iBAAiB,EACjB,cAAc,EACd,UAAU,GAGX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,kBAAkB,EAAqB,MAAM,cAAc,CAAC;AACjH,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,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4 +4,21 @@ import type { AuthContext } from "./sessions.js";
|
|
|
4
4
|
* caller counts the attempt in a rate limit bucket first: this is a password check like a login.
|
|
5
5
|
*/
|
|
6
6
|
export declare function isCurrentPassword(ctx: AuthContext, userId: string, password: string): Promise<boolean>;
|
|
7
|
+
/** The ids of the providers the account signs in with (its `auth.identities`), sorted. */
|
|
8
|
+
export declare function findLinkedProviders(ctx: AuthContext, userId: string): Promise<readonly string[]>;
|
|
9
|
+
export interface RecentProviderSignInInput {
|
|
10
|
+
/** The signed-in user, from the session. */
|
|
11
|
+
readonly userId: string;
|
|
12
|
+
/** The request's session token: only the session this sign-in opened counts. */
|
|
13
|
+
readonly sessionToken: string;
|
|
14
|
+
/** How recent the sign-in must be, in minutes. */
|
|
15
|
+
readonly withinMinutes: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The provider whose sign-in opened this very session within the last `withinMinutes`, or null:
|
|
19
|
+
* an account without a password it knows (one created by a provider) re-authenticates by signing in
|
|
20
|
+
* with its provider again. A session opened by a password login, an older session, another user's
|
|
21
|
+
* session or an expired one is null; so is a provider sign-in that opened a session elsewhere.
|
|
22
|
+
*/
|
|
23
|
+
export declare function findRecentProviderSignIn(ctx: AuthContext, input: RecentProviderSignInInput): Promise<string | null>;
|
|
7
24
|
//# sourceMappingURL=reauthenticate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reauthenticate.d.ts","sourceRoot":"","sources":["../../src/server/reauthenticate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"reauthenticate.d.ts","sourceRoot":"","sources":["../../src/server/reauthenticate.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjD;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAI5G;AAED,0FAA0F;AAC1F,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,MAAM,EAAE,CAAC,CAGtG;AAKD,MAAM,WAAW,yBAAyB;IACxC,4CAA4C;IAC5C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,gFAAgF;IAChF,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,kDAAkD;IAClD,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;CAChC;AAED;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,yBAAyB,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAyBzH"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
// Re-authentication before an action that cannot be undone, such as deleting the account: the
|
|
2
|
-
// signed-in user types their password again,
|
|
3
|
-
|
|
4
|
-
import {
|
|
2
|
+
// signed-in user types their password again, or signs in with their provider again, so a session
|
|
3
|
+
// cookie alone is not enough.
|
|
4
|
+
import { and, eq, gt, gte } from "drizzle-orm";
|
|
5
|
+
import { identities, sessions, users } from "../schema.js";
|
|
5
6
|
import { verifyPassword } from "./password.js";
|
|
7
|
+
import { hashSessionToken, isSessionTokenShape } from "./session-token.js";
|
|
8
|
+
import { getLegacyTokenPattern } from "./options.js";
|
|
6
9
|
/**
|
|
7
10
|
* Whether `password` is the current password of the account. False for an unknown account. The
|
|
8
11
|
* caller counts the attempt in a rate limit bucket first: this is a password check like a login.
|
|
@@ -13,4 +16,38 @@ export async function isCurrentPassword(ctx, userId, password) {
|
|
|
13
16
|
return false;
|
|
14
17
|
return verifyPassword(password, stored.passwordHash);
|
|
15
18
|
}
|
|
19
|
+
/** The ids of the providers the account signs in with (its `auth.identities`), sorted. */
|
|
20
|
+
export async function findLinkedProviders(ctx, userId) {
|
|
21
|
+
const rows = await ctx.db.select({ provider: identities.provider }).from(identities).where(eq(identities.userId, userId));
|
|
22
|
+
return rows.map((row) => row.provider).sort();
|
|
23
|
+
}
|
|
24
|
+
/** How long after a provider's sign-in its session may still be opened (the callback opens it right after). */
|
|
25
|
+
const SIGN_IN_TO_SESSION_MS = 60 * 1000;
|
|
26
|
+
/**
|
|
27
|
+
* The provider whose sign-in opened this very session within the last `withinMinutes`, or null:
|
|
28
|
+
* an account without a password it knows (one created by a provider) re-authenticates by signing in
|
|
29
|
+
* with its provider again. A session opened by a password login, an older session, another user's
|
|
30
|
+
* session or an expired one is null; so is a provider sign-in that opened a session elsewhere.
|
|
31
|
+
*/
|
|
32
|
+
export async function findRecentProviderSignIn(ctx, input) {
|
|
33
|
+
if (!isSessionTokenShape(input.sessionToken, getLegacyTokenPattern(ctx.config)))
|
|
34
|
+
return null;
|
|
35
|
+
const now = ctx.clock.now();
|
|
36
|
+
const since = new Date(now.getTime() - input.withinMinutes * 60 * 1000);
|
|
37
|
+
const [session] = await ctx.db
|
|
38
|
+
.select({ createdAt: sessions.createdAt })
|
|
39
|
+
.from(sessions)
|
|
40
|
+
.where(and(eq(sessions.tokenHash, hashSessionToken(input.sessionToken)), eq(sessions.userId, input.userId), gt(sessions.expiresAt, now), gte(sessions.createdAt, since)))
|
|
41
|
+
.limit(1);
|
|
42
|
+
if (session === undefined)
|
|
43
|
+
return null;
|
|
44
|
+
const linked = await ctx.db
|
|
45
|
+
.select({ provider: identities.provider, lastLoginAt: identities.lastLoginAt })
|
|
46
|
+
.from(identities)
|
|
47
|
+
.where(and(eq(identities.userId, input.userId), gte(identities.lastLoginAt, since)));
|
|
48
|
+
const opened = session.createdAt.getTime();
|
|
49
|
+
// The callback stamps the identity, then opens the session: the session is the sign-in's own when it follows the stamp closely.
|
|
50
|
+
const match = linked.find((row) => row.lastLoginAt.getTime() <= opened && opened - row.lastLoginAt.getTime() <= SIGN_IN_TO_SESSION_MS);
|
|
51
|
+
return match?.provider ?? null;
|
|
52
|
+
}
|
|
16
53
|
//# sourceMappingURL=reauthenticate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reauthenticate.js","sourceRoot":"","sources":["../../src/server/reauthenticate.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,
|
|
1
|
+
{"version":3,"file":"reauthenticate.js","sourceRoot":"","sources":["../../src/server/reauthenticate.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,iGAAiG;AACjG,8BAA8B;AAC9B,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAGrD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,GAAgB,EAAE,MAAc,EAAE,QAAgB;IACxF,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,YAAY,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5H,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,KAAK,CAAC;IACvC,OAAO,cAAc,CAAC,QAAQ,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC;AACvD,CAAC;AAED,0FAA0F;AAC1F,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,GAAgB,EAAE,MAAc;IACxE,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,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A,KAAK,CAAC,CAAC,CAAC,CAAC;IACZ,IAAI,OAAO,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACvC,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,EAAE;SACxB,MAAM,CAAC,EAAE,QAAQ,EAAE,UAAU,CAAC,QAAQ,EAAE,WAAW,EAAE,UAAU,CAAC,WAAW,EAAE,CAAC;SAC9E,IAAI,CAAC,UAAU,CAAC;SAChB,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,UAAU,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,UAAU,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IACvF,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;IAC3C,gIAAgI;IAChI,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,OAAO,EAAE,IAAI,MAAM,IAAI,MAAM,GAAG,GAAG,CAAC,WAAW,CAAC,OAAO,EAAE,IAAI,qBAAqB,CAAC,CAAC;IACvI,OAAO,KAAK,EAAE,QAAQ,IAAI,IAAI,CAAC;AACjC,CAAC"}
|
package/module.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softure-ai/auth",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.18",
|
|
4
4
|
"description": "SOFTURE AI auth module: users, scrypt passwords, database sessions, register with consent, login, logout, password change, roles, a proxy route guard, pages and forms for Next.js, and a password reset sender over @softure-ai/mailing, sent in the request or by a background job through @softure-ai/jobs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
package/src/index.ts
CHANGED
|
@@ -46,7 +46,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
|
|
|
46
46
|
export const auth = defineModule({
|
|
47
47
|
manifest: {
|
|
48
48
|
id: MODULE_ID,
|
|
49
|
-
version: "0.2.
|
|
49
|
+
version: "0.2.18",
|
|
50
50
|
runtimes: ["node", "workers"],
|
|
51
51
|
dependsOn: { security: "^0.2.0", mailing: "^0.2.0?", ops: "^0.2.0?", jobs: "^0.1.1?" },
|
|
52
52
|
dbSchema: "auth",
|
package/src/server/index.ts
CHANGED
|
@@ -57,7 +57,7 @@ export {
|
|
|
57
57
|
type RoleErrorCode,
|
|
58
58
|
} from "./roles.js";
|
|
59
59
|
export { authPrivacyContributor, deleteAuthUserData, exportAuthUserData, type AuthUserData } from "./privacy.js";
|
|
60
|
-
export { isCurrentPassword } from "./reauthenticate.js";
|
|
60
|
+
export { findLinkedProviders, findRecentProviderSignIn, isCurrentPassword, type RecentProviderSignInInput } from "./reauthenticate.js";
|
|
61
61
|
export { registerUser, type RegisterErrorCode, type RegisterInput, type RegisterResult } from "./register.js";
|
|
62
62
|
export {
|
|
63
63
|
createSession,
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
// Re-authentication before an action that cannot be undone, such as deleting the account: the
|
|
2
|
-
// signed-in user types their password again,
|
|
3
|
-
|
|
4
|
-
import {
|
|
2
|
+
// signed-in user types their password again, or signs in with their provider again, so a session
|
|
3
|
+
// cookie alone is not enough.
|
|
4
|
+
import { and, eq, gt, gte } from "drizzle-orm";
|
|
5
|
+
import { identities, sessions, users } from "../schema.js";
|
|
5
6
|
import { verifyPassword } from "./password.js";
|
|
7
|
+
import { hashSessionToken, isSessionTokenShape } from "./session-token.js";
|
|
8
|
+
import { getLegacyTokenPattern } from "./options.js";
|
|
6
9
|
import type { AuthContext } from "./sessions.js";
|
|
7
10
|
|
|
8
11
|
/**
|
|
@@ -14,3 +17,54 @@ export async function isCurrentPassword(ctx: AuthContext, userId: string, passwo
|
|
|
14
17
|
if (stored === undefined) return false;
|
|
15
18
|
return verifyPassword(password, stored.passwordHash);
|
|
16
19
|
}
|
|
20
|
+
|
|
21
|
+
/** The ids of the providers the account signs in with (its `auth.identities`), sorted. */
|
|
22
|
+
export async function findLinkedProviders(ctx: AuthContext, userId: string): Promise<readonly string[]> {
|
|
23
|
+
const rows = await ctx.db.select({ provider: identities.provider }).from(identities).where(eq(identities.userId, userId));
|
|
24
|
+
return rows.map((row) => row.provider).sort();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** How long after a provider's sign-in its session may still be opened (the callback opens it right after). */
|
|
28
|
+
const SIGN_IN_TO_SESSION_MS = 60 * 1000;
|
|
29
|
+
|
|
30
|
+
export interface RecentProviderSignInInput {
|
|
31
|
+
/** The signed-in user, from the session. */
|
|
32
|
+
readonly userId: string;
|
|
33
|
+
/** The request's session token: only the session this sign-in opened counts. */
|
|
34
|
+
readonly sessionToken: string;
|
|
35
|
+
/** How recent the sign-in must be, in minutes. */
|
|
36
|
+
readonly withinMinutes: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The provider whose sign-in opened this very session within the last `withinMinutes`, or null:
|
|
41
|
+
* an account without a password it knows (one created by a provider) re-authenticates by signing in
|
|
42
|
+
* with its provider again. A session opened by a password login, an older session, another user's
|
|
43
|
+
* session or an expired one is null; so is a provider sign-in that opened a session elsewhere.
|
|
44
|
+
*/
|
|
45
|
+
export async function findRecentProviderSignIn(ctx: AuthContext, input: RecentProviderSignInInput): Promise<string | null> {
|
|
46
|
+
if (!isSessionTokenShape(input.sessionToken, getLegacyTokenPattern(ctx.config))) return null;
|
|
47
|
+
const now = ctx.clock.now();
|
|
48
|
+
const since = new Date(now.getTime() - input.withinMinutes * 60 * 1000);
|
|
49
|
+
const [session] = await ctx.db
|
|
50
|
+
.select({ createdAt: sessions.createdAt })
|
|
51
|
+
.from(sessions)
|
|
52
|
+
.where(
|
|
53
|
+
and(
|
|
54
|
+
eq(sessions.tokenHash, hashSessionToken(input.sessionToken)),
|
|
55
|
+
eq(sessions.userId, input.userId),
|
|
56
|
+
gt(sessions.expiresAt, now),
|
|
57
|
+
gte(sessions.createdAt, since),
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
.limit(1);
|
|
61
|
+
if (session === undefined) return null;
|
|
62
|
+
const linked = await ctx.db
|
|
63
|
+
.select({ provider: identities.provider, lastLoginAt: identities.lastLoginAt })
|
|
64
|
+
.from(identities)
|
|
65
|
+
.where(and(eq(identities.userId, input.userId), gte(identities.lastLoginAt, since)));
|
|
66
|
+
const opened = session.createdAt.getTime();
|
|
67
|
+
// The callback stamps the identity, then opens the session: the session is the sign-in's own when it follows the stamp closely.
|
|
68
|
+
const match = linked.find((row) => row.lastLoginAt.getTime() <= opened && opened - row.lastLoginAt.getTime() <= SIGN_IN_TO_SESSION_MS);
|
|
69
|
+
return match?.provider ?? null;
|
|
70
|
+
}
|