@softure-ai/auth 0.2.17 → 0.2.19
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 +18 -0
- package/README.md +12 -4
- 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/oidc/accounts.d.ts.map +1 -1
- package/dist/server/oidc/accounts.js +25 -2
- package/dist/server/oidc/accounts.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/oidc/accounts.ts +32 -2
- package/src/server/reauthenticate.ts +57 -3
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,24 @@ 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.19
|
|
8
|
+
|
|
9
|
+
- **Provider sign-in ends an unverified password account's access:** registration does not prove an email, so someone
|
|
10
|
+
could register the owner's address first and keep the password and the sessions after the owner's first OpenID
|
|
11
|
+
Connect or GitHub sign-in linked the account. A first provider sign-in that links an account no provider has
|
|
12
|
+
verified yet now replaces its password with an unusable one and deletes its sessions and a pending reset link, in
|
|
13
|
+
the same transaction. An account a provider already verified keeps them when another provider links it.
|
|
14
|
+
- Migrate: nothing. An owner who also used the password sets it again through a reset link.
|
|
15
|
+
|
|
16
|
+
## 0.2.18
|
|
17
|
+
|
|
18
|
+
- **Re-authentication by a provider sign-in:** `findRecentProviderSignIn(ctx, { userId, sessionToken, withinMinutes })`
|
|
19
|
+
(`/server`) answers the provider whose sign-in opened that very session within the window, or null (a session a
|
|
20
|
+
password login opened, an older one, another user's or an expired one). `findLinkedProviders(ctx, userId)` lists the
|
|
21
|
+
providers an account signs in with. An account a provider created has no password its owner knows; these let an
|
|
22
|
+
action that cannot be undone, such as `@softure-ai/privacy`'s account deletion, confirm it is the owner.
|
|
23
|
+
- Migrate: nothing.
|
|
24
|
+
|
|
7
25
|
## 0.2.17
|
|
8
26
|
|
|
9
27
|
- **Open sign-up through a provider:** `openSignUp: true` on an OpenID Connect or GitHub provider lets any verified email
|
package/README.md
CHANGED
|
@@ -174,7 +174,9 @@ the variables the provider names.
|
|
|
174
174
|
`?oidc_error=auth.oidc_not_allowed` and never gets an account.
|
|
175
175
|
- **Accounts.** The provider's subject finds the account (`auth.identities`), so an email change at the provider does
|
|
176
176
|
not lock its owner out. A first sign-in links the account with the same verified email, or creates one (with an
|
|
177
|
-
unusable password) when `createAccounts` is on
|
|
177
|
+
unusable password) when `createAccounts` is on. Registration does not prove an email, so linking an account no
|
|
178
|
+
provider has verified yet ends its password (replaced by an unusable one), its sessions and a pending reset link:
|
|
179
|
+
whoever registered the address first keeps no way in, and the owner sets a password again through a reset link; account creation does not look at `registrationClosed`, the
|
|
178
180
|
allowlist is the gate. An account keeps one identity per provider: a new subject with an email that already has one
|
|
179
181
|
(a recycled address) is refused with `auth.oidc_no_account`. `onRegistered` runs for a created account with
|
|
180
182
|
`consent: null`, `fields: {}` and `oidcProvider: "<id>"`.
|
|
@@ -866,7 +868,12 @@ contributes to `@softure-ai/privacy` (`privacy` flags on):
|
|
|
866
868
|
depend on it, and the app's contributors before all modules.
|
|
867
869
|
|
|
868
870
|
`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.
|
|
871
|
+
that cannot be undone, such as the account deletion; the caller counts the attempt first. An account a
|
|
872
|
+
provider created has no password its owner knows, so it re-authenticates by signing in with the
|
|
873
|
+
provider again: `findRecentProviderSignIn(ctx, { userId, sessionToken, withinMinutes })` answers the
|
|
874
|
+
provider whose sign-in opened that very session within the window (a session a password login
|
|
875
|
+
opened, an older one or another user's is null), and `findLinkedProviders(ctx, userId)` lists the
|
|
876
|
+
providers the account signs in with.
|
|
870
877
|
|
|
871
878
|
Rate limit rows of `security` hold only SHA-256 prefixes of the email and user id
|
|
872
879
|
(`subjectKey`), pruned two windows after they start; they are not exported or deleted.
|
|
@@ -892,7 +899,8 @@ Rate limit rows of `security` hold only SHA-256 prefixes of the email and user i
|
|
|
892
899
|
at most N accounts keeps registration closed.
|
|
893
900
|
- OpenID Connect: no Front-Channel or Back-Channel Logout (a sign-out at the provider leaves the app's session), no
|
|
894
901
|
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
|
|
902
|
+
and `isCurrentPassword` cannot re-check an account that has no password of its own (use
|
|
903
|
+
`findRecentProviderSignIn`). With passwords on and reset configured, the owner of an account created by a sign-in
|
|
904
|
+
can still set a password through a reset link.
|
|
897
905
|
- A registration attempt reveals whether an email has an account (`auth.email_taken`); the
|
|
898
906
|
`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.19",
|
|
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,WAAW,EAAE,MAAM,YAAY,CAAC;AAC1E,OAAO,EACL,kBAAkB,EAClB,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,KAAK,yBAAyB,GAC/B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,eAA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AK,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,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,mBAAmB,EAAE,wBAAwB,EAAE,iBAAiB,EAAkC,MAAM,qBAAqB,CAAC;AACvI,OAAO,EAAE,YAAY,EAAmE,MAAM,eAAe,CAAC;AAC9G,OAAO,EACL,aAAa,EACb,eAAe,EACf,aAAa,EACb,aAAa,EACb,kBAAkB,GAGnB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,0BAA0B,EAAiC,MAAM,yBAAyB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accounts.d.ts","sourceRoot":"","sources":["../../../src/server/oidc/accounts.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"accounts.d.ts","sourceRoot":"","sources":["../../../src/server/oidc/accounts.ts"],"names":[],"mappings":"AASA,OAAO,EAAW,KAAK,GAAG,EAAE,KAAK,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAE9D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAIrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAGlD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,wCAAwC;IACxC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,kGAAkG;IAClG,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CACrC;AAED,MAAM,MAAM,iBAAiB,GAAG,EAAE,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAC,sBAAsB,GAAG,+BAA+B,CAAC,CAAC;AAE7G,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,QAAQ,EAAE,YAAY,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAkErI"}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
// The account an OpenID Connect identity signs in to. The provider's subject finds it (`auth.identities`), so an
|
|
2
2
|
// email change at the provider keeps it; a first sign-in links the account with the same verified email, or creates
|
|
3
3
|
// one with an unusable password when the provider may create accounts. One transaction, so two first sign-ins at
|
|
4
|
-
// once end with one account and one identity.
|
|
4
|
+
// once end with one account and one identity. Linking an account that no provider had verified yet (one registered
|
|
5
|
+
// with a password, whose email nobody proved) cancels its password, its sessions and a pending reset link: whoever
|
|
6
|
+
// registered that email first may not be its owner, and must not keep a way in to the account the owner now uses.
|
|
7
|
+
// An `openSignUp` provider creates none while registration is closed. Every sign-in that carries a display name
|
|
8
|
+
// stores it on the account.
|
|
5
9
|
import { randomBytes } from "node:crypto";
|
|
6
10
|
import { err, ok } from "@softure-ai/core";
|
|
7
11
|
import { and, eq } from "drizzle-orm";
|
|
8
|
-
import { AUTH_USER_COLUMNS, identities, users } from "../../schema.js";
|
|
12
|
+
import { AUTH_USER_COLUMNS, identities, passwordResets, sessions, users } from "../../schema.js";
|
|
9
13
|
import { getAuthOptions } from "../options.js";
|
|
10
14
|
import { hashPassword } from "../password.js";
|
|
11
15
|
import { isRegistrationSwitchOn } from "../switches.js";
|
|
@@ -61,6 +65,7 @@ export async function resolveOidcAccount(ctx, provider, identity) {
|
|
|
61
65
|
if (user === undefined)
|
|
62
66
|
return err("auth.oidc_no_account");
|
|
63
67
|
}
|
|
68
|
+
const isUnverified = !isCreated && (await hasNoIdentity(tx, user.id));
|
|
64
69
|
await tx
|
|
65
70
|
.insert(identities)
|
|
66
71
|
.values({ provider: provider.id, subject: identity.subject, userId: user.id, email: identity.email, createdAt: now, lastLoginAt: now })
|
|
@@ -69,6 +74,8 @@ export async function resolveOidcAccount(ctx, provider, identity) {
|
|
|
69
74
|
const nowLinked = await findLinked();
|
|
70
75
|
if (nowLinked?.id !== user.id)
|
|
71
76
|
return err("auth.oidc_no_account");
|
|
77
|
+
if (isUnverified)
|
|
78
|
+
await revokeUnverifiedAccess(tx, { userId: user.id, now, scrypt: options.password.scrypt });
|
|
72
79
|
const signedIn = await storeDisplayName(tx, user, identity.displayName);
|
|
73
80
|
if (isCreated)
|
|
74
81
|
await options.onRegistered?.({ user: signedIn, consent: null, fields: {}, oidcProvider: provider.id }, txCtx);
|
|
@@ -82,4 +89,20 @@ async function storeDisplayName(db, user, displayName) {
|
|
|
82
89
|
await db.update(users).set({ displayName }).where(eq(users.id, user.id));
|
|
83
90
|
return { ...user, displayName };
|
|
84
91
|
}
|
|
92
|
+
/** Whether no provider identity is linked to the account yet: its email was never proven by a provider. */
|
|
93
|
+
async function hasNoIdentity(db, userId) {
|
|
94
|
+
const [linked] = await db.select({ provider: identities.provider }).from(identities).where(eq(identities.userId, userId)).limit(1);
|
|
95
|
+
return linked === undefined;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Ends every way in that existed before the provider proved the email: the password (replaced by one nobody knows),
|
|
99
|
+
* every session and a pending reset link. The sign-in opens its own session afterwards; the owner gets a password
|
|
100
|
+
* back through a reset link.
|
|
101
|
+
*/
|
|
102
|
+
async function revokeUnverifiedAccess(db, input) {
|
|
103
|
+
const unusablePassword = await hashPassword(randomBytes(32).toString("base64url"), input.scrypt);
|
|
104
|
+
await db.update(users).set({ passwordHash: unusablePassword, passwordChangedAt: input.now }).where(eq(users.id, input.userId));
|
|
105
|
+
await db.delete(sessions).where(eq(sessions.userId, input.userId));
|
|
106
|
+
await db.delete(passwordResets).where(eq(passwordResets.userId, input.userId));
|
|
107
|
+
}
|
|
85
108
|
//# sourceMappingURL=accounts.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accounts.js","sourceRoot":"","sources":["../../../src/server/oidc/accounts.ts"],"names":[],"mappings":"AAAA,iHAAiH;AACjH,oHAAoH;AACpH,iHAAiH;AACjH,
|
|
1
|
+
{"version":3,"file":"accounts.js","sourceRoot":"","sources":["../../../src/server/oidc/accounts.ts"],"names":[],"mappings":"AAAA,iHAAiH;AACjH,oHAAoH;AACpH,iHAAiH;AACjH,mHAAmH;AACnH,mHAAmH;AACnH,kHAAkH;AAClH,gHAAgH;AAChH,4BAA4B;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,GAAG,EAAE,EAAE,EAAqB,MAAM,kBAAkB,CAAC;AAC9D,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAGtC,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACjG,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAYxD,M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,CAAC,EAAqB,EAAE,KAAkC;IAC7F,MAAM,gBAAgB,GAAG,MAAM,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACjG,MAAM,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/H,MAAM,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACnE,MAAM,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,cAAc,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;AACjF,CAAC"}
|
|
@@ -4,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,mBAA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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.19",
|
|
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.19",
|
|
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,13 +1,17 @@
|
|
|
1
1
|
// The account an OpenID Connect identity signs in to. The provider's subject finds it (`auth.identities`), so an
|
|
2
2
|
// email change at the provider keeps it; a first sign-in links the account with the same verified email, or creates
|
|
3
3
|
// one with an unusable password when the provider may create accounts. One transaction, so two first sign-ins at
|
|
4
|
-
// once end with one account and one identity.
|
|
4
|
+
// once end with one account and one identity. Linking an account that no provider had verified yet (one registered
|
|
5
|
+
// with a password, whose email nobody proved) cancels its password, its sessions and a pending reset link: whoever
|
|
6
|
+
// registered that email first may not be its owner, and must not keep a way in to the account the owner now uses.
|
|
7
|
+
// An `openSignUp` provider creates none while registration is closed. Every sign-in that carries a display name
|
|
8
|
+
// stores it on the account.
|
|
5
9
|
import { randomBytes } from "node:crypto";
|
|
6
10
|
import { err, ok, type Err, type Ok } from "@softure-ai/core";
|
|
7
11
|
import { and, eq } from "drizzle-orm";
|
|
8
12
|
import type { AuthUser } from "../../contract.js";
|
|
9
13
|
import type { OidcProvider } from "../../options.js";
|
|
10
|
-
import { AUTH_USER_COLUMNS, identities, users } from "../../schema.js";
|
|
14
|
+
import { AUTH_USER_COLUMNS, identities, passwordResets, sessions, users } from "../../schema.js";
|
|
11
15
|
import { getAuthOptions } from "../options.js";
|
|
12
16
|
import { hashPassword } from "../password.js";
|
|
13
17
|
import type { AuthContext } from "../sessions.js";
|
|
@@ -76,6 +80,7 @@ export async function resolveOidcAccount(ctx: AuthContext, provider: OidcProvide
|
|
|
76
80
|
if (user === undefined) return err("auth.oidc_no_account");
|
|
77
81
|
}
|
|
78
82
|
|
|
83
|
+
const isUnverified = !isCreated && (await hasNoIdentity(tx, user.id));
|
|
79
84
|
await tx
|
|
80
85
|
.insert(identities)
|
|
81
86
|
.values({ provider: provider.id, subject: identity.subject, userId: user.id, email: identity.email, createdAt: now, lastLoginAt: now })
|
|
@@ -83,6 +88,7 @@ export async function resolveOidcAccount(ctx: AuthContext, provider: OidcProvide
|
|
|
83
88
|
// The pair may have been linked meanwhile; an account that already has another subject of this provider is refused.
|
|
84
89
|
const nowLinked = await findLinked();
|
|
85
90
|
if (nowLinked?.id !== user.id) return err("auth.oidc_no_account");
|
|
91
|
+
if (isUnverified) await revokeUnverifiedAccess(tx, { userId: user.id, now, scrypt: options.password.scrypt });
|
|
86
92
|
const signedIn = await storeDisplayName(tx, user, identity.displayName);
|
|
87
93
|
if (isCreated) await options.onRegistered?.({ user: signedIn, consent: null, fields: {}, oidcProvider: provider.id }, txCtx);
|
|
88
94
|
return ok(signedIn);
|
|
@@ -95,3 +101,27 @@ async function storeDisplayName(db: AuthContext["db"], user: AuthUser, displayNa
|
|
|
95
101
|
await db.update(users).set({ displayName }).where(eq(users.id, user.id));
|
|
96
102
|
return { ...user, displayName };
|
|
97
103
|
}
|
|
104
|
+
|
|
105
|
+
/** Whether no provider identity is linked to the account yet: its email was never proven by a provider. */
|
|
106
|
+
async function hasNoIdentity(db: AuthContext["db"], userId: string): Promise<boolean> {
|
|
107
|
+
const [linked] = await db.select({ provider: identities.provider }).from(identities).where(eq(identities.userId, userId)).limit(1);
|
|
108
|
+
return linked === undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
interface RevokeUnverifiedAccessInput {
|
|
112
|
+
readonly userId: string;
|
|
113
|
+
readonly now: Date;
|
|
114
|
+
readonly scrypt: ReturnType<typeof getAuthOptions>["password"]["scrypt"];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Ends every way in that existed before the provider proved the email: the password (replaced by one nobody knows),
|
|
119
|
+
* every session and a pending reset link. The sign-in opens its own session afterwards; the owner gets a password
|
|
120
|
+
* back through a reset link.
|
|
121
|
+
*/
|
|
122
|
+
async function revokeUnverifiedAccess(db: AuthContext["db"], input: RevokeUnverifiedAccessInput): Promise<void> {
|
|
123
|
+
const unusablePassword = await hashPassword(randomBytes(32).toString("base64url"), input.scrypt);
|
|
124
|
+
await db.update(users).set({ passwordHash: unusablePassword, passwordChangedAt: input.now }).where(eq(users.id, input.userId));
|
|
125
|
+
await db.delete(sessions).where(eq(sessions.userId, input.userId));
|
|
126
|
+
await db.delete(passwordResets).where(eq(passwordResets.userId, input.userId));
|
|
127
|
+
}
|
|
@@ -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
|
+
}
|