@softure-ai/auth 0.2.18 → 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 +9 -0
- package/README.md +3 -1
- package/dist/index.js +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/module.json +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/server/oidc/accounts.ts +32 -2
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.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
|
+
|
|
7
16
|
## 0.2.18
|
|
8
17
|
|
|
9
18
|
- **Re-authentication by a provider sign-in:** `findRecentProviderSignIn(ctx, { userId, sessionToken, withinMinutes })`
|
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>"`.
|
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",
|
|
@@ -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,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,GAAgB,EAAE,QAAsB,EAAE,QAAsB;IACvG,MAAM,OAAO,GAAG,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC3C,MAAM,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;IAC5B,kHAAkH;IAClH,2EAA2E;IAC3E,MAAM,cAAc,GAAG,QAAQ,CAAC,UAAU,IAAI,CAAC,MAAM,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAC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AC,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"}
|
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",
|
|
@@ -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
|
+
}
|