@sharadox/account-web-library 0.1.0-development.24 → 0.1.0-development.27
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/README.md +95 -20
- package/distribution/account-activity/index.d.ts +16 -0
- package/distribution/account-activity/index.d.ts.map +1 -0
- package/distribution/account-activity/index.js +18 -0
- package/distribution/account-activity/index.js.map +1 -0
- package/distribution/account-activity/type.d.ts +15 -0
- package/distribution/account-activity/type.d.ts.map +1 -0
- package/distribution/account-activity/type.js.map +1 -0
- package/distribution/account-database/type.d.ts +57 -4
- package/distribution/account-database/type.d.ts.map +1 -1
- package/distribution/account-deletion/index.d.ts +18 -0
- package/distribution/account-deletion/index.d.ts.map +1 -0
- package/distribution/account-deletion/index.js +28 -0
- package/distribution/account-deletion/index.js.map +1 -0
- package/distribution/account-export/index.d.ts +17 -0
- package/distribution/account-export/index.d.ts.map +1 -0
- package/distribution/account-export/index.js +87 -0
- package/distribution/account-export/index.js.map +1 -0
- package/distribution/account-export/type.d.ts +40 -0
- package/distribution/account-export/type.d.ts.map +1 -0
- package/distribution/{username-change → account-export}/type.js.map +1 -1
- package/distribution/account-migration/index.d.ts.map +1 -1
- package/distribution/account-migration/index.js +110 -0
- package/distribution/account-migration/index.js.map +1 -1
- package/distribution/account-name/index.d.ts +13 -0
- package/distribution/account-name/index.d.ts.map +1 -0
- package/distribution/account-name/index.js +22 -0
- package/distribution/account-name/index.js.map +1 -0
- package/distribution/account-name/type.d.ts +9 -0
- package/distribution/account-name/type.d.ts.map +1 -0
- package/distribution/account-name/type.js +2 -0
- package/distribution/account-name/type.js.map +1 -0
- package/distribution/account-name-change/index.d.ts +15 -0
- package/distribution/account-name-change/index.d.ts.map +1 -0
- package/distribution/account-name-change/index.js +26 -0
- package/distribution/account-name-change/index.js.map +1 -0
- package/distribution/account-name-change/type.d.ts +7 -0
- package/distribution/account-name-change/type.d.ts.map +1 -0
- package/distribution/account-name-change/type.js +2 -0
- package/distribution/account-name-change/type.js.map +1 -0
- package/distribution/account-picture-change/index.d.ts +30 -0
- package/distribution/account-picture-change/index.d.ts.map +1 -0
- package/distribution/account-picture-change/index.js +70 -0
- package/distribution/account-picture-change/index.js.map +1 -0
- package/distribution/account-picture-change/type.d.ts +11 -0
- package/distribution/account-picture-change/type.d.ts.map +1 -0
- package/distribution/account-picture-change/type.js +2 -0
- package/distribution/account-picture-change/type.js.map +1 -0
- package/distribution/account-server/index.d.ts +11 -1
- package/distribution/account-server/index.d.ts.map +1 -1
- package/distribution/account-server/index.js +12 -1
- package/distribution/account-server/index.js.map +1 -1
- package/distribution/account-server/type.d.ts +2 -0
- package/distribution/account-server/type.d.ts.map +1 -1
- package/distribution/account-session-end/index.d.ts +15 -0
- package/distribution/account-session-end/index.d.ts.map +1 -0
- package/distribution/account-session-end/index.js +21 -0
- package/distribution/account-session-end/index.js.map +1 -0
- package/distribution/account-session-list/index.d.ts +16 -0
- package/distribution/account-session-list/index.d.ts.map +1 -0
- package/distribution/account-session-list/index.js +33 -0
- package/distribution/account-session-list/index.js.map +1 -0
- package/distribution/account-session-list/type.d.ts +13 -0
- package/distribution/account-session-list/type.d.ts.map +1 -0
- package/distribution/account-session-list/type.js +2 -0
- package/distribution/account-session-list/type.js.map +1 -0
- package/distribution/account-sign-in/index.d.ts +1 -1
- package/distribution/account-sign-in/index.d.ts.map +1 -1
- package/distribution/account-sign-in/index.js +4 -2
- package/distribution/account-sign-in/index.js.map +1 -1
- package/distribution/account-sign-in/type.d.ts +2 -0
- package/distribution/account-sign-in/type.d.ts.map +1 -1
- package/distribution/audit-event/index.d.ts.map +1 -1
- package/distribution/audit-event/index.js +12 -1
- package/distribution/audit-event/index.js.map +1 -1
- package/distribution/audit-event/type.d.ts +1 -0
- package/distribution/audit-event/type.d.ts.map +1 -1
- package/distribution/browser-session-read/index.d.ts.map +1 -1
- package/distribution/browser-session-read/index.js +5 -1
- package/distribution/browser-session-read/index.js.map +1 -1
- package/distribution/browser-session-read/type.d.ts +5 -1
- package/distribution/browser-session-read/type.d.ts.map +1 -1
- package/distribution/code-request/index.js +1 -1
- package/distribution/code-request/type.d.ts +1 -1
- package/distribution/code-sending/index.d.ts +14 -0
- package/distribution/code-sending/index.d.ts.map +1 -0
- package/distribution/code-sending/index.js +65 -0
- package/distribution/code-sending/index.js.map +1 -0
- package/distribution/code-sending/type.d.ts +31 -0
- package/distribution/code-sending/type.d.ts.map +1 -0
- package/distribution/code-sending/type.js +2 -0
- package/distribution/code-sending/type.js.map +1 -0
- package/distribution/contact-addition-flow/index.d.ts +31 -0
- package/distribution/contact-addition-flow/index.d.ts.map +1 -0
- package/distribution/contact-addition-flow/index.js +85 -0
- package/distribution/contact-addition-flow/index.js.map +1 -0
- package/distribution/contact-addition-flow/type.d.ts +22 -0
- package/distribution/contact-addition-flow/type.d.ts.map +1 -0
- package/distribution/contact-addition-flow/type.js +2 -0
- package/distribution/contact-addition-flow/type.js.map +1 -0
- package/distribution/database-error/index.d.ts +1 -1
- package/distribution/database-error/index.js +1 -1
- package/distribution/email-channel/index.js +1 -1
- package/distribution/identifier/index.d.ts +23 -0
- package/distribution/identifier/index.d.ts.map +1 -0
- package/distribution/identifier/index.js +32 -0
- package/distribution/identifier/index.js.map +1 -0
- package/distribution/identifier/type.d.ts +10 -0
- package/distribution/identifier/type.d.ts.map +1 -0
- package/distribution/identifier/type.js +2 -0
- package/distribution/identifier/type.js.map +1 -0
- package/distribution/identifier-availability/index.d.ts +11 -0
- package/distribution/identifier-availability/index.d.ts.map +1 -0
- package/distribution/identifier-availability/index.js +21 -0
- package/distribution/identifier-availability/index.js.map +1 -0
- package/distribution/identifier-change/index.d.ts +15 -0
- package/distribution/identifier-change/index.d.ts.map +1 -0
- package/distribution/identifier-change/index.js +43 -0
- package/distribution/identifier-change/index.js.map +1 -0
- package/distribution/identifier-change/type.d.ts +7 -0
- package/distribution/identifier-change/type.d.ts.map +1 -0
- package/distribution/identifier-change/type.js +2 -0
- package/distribution/identifier-change/type.js.map +1 -0
- package/distribution/memory-picture-storage/index.d.ts +15 -0
- package/distribution/memory-picture-storage/index.d.ts.map +1 -0
- package/distribution/memory-picture-storage/index.js +16 -0
- package/distribution/memory-picture-storage/index.js.map +1 -0
- package/distribution/one-time-code/index.d.ts +2 -2
- package/distribution/one-time-code/index.js +3 -3
- package/distribution/person-account/index.d.ts +7 -3
- package/distribution/person-account/index.d.ts.map +1 -1
- package/distribution/person-account/index.js +21 -9
- package/distribution/person-account/index.js.map +1 -1
- package/distribution/person-account/type.d.ts +5 -2
- package/distribution/person-account/type.d.ts.map +1 -1
- package/distribution/picture-key/index.d.ts +8 -0
- package/distribution/picture-key/index.d.ts.map +1 -0
- package/distribution/picture-key/index.js +10 -0
- package/distribution/picture-key/index.js.map +1 -0
- package/distribution/picture-preparation/index.d.ts +18 -0
- package/distribution/picture-preparation/index.d.ts.map +1 -0
- package/distribution/picture-preparation/index.js +86 -0
- package/distribution/picture-preparation/index.js.map +1 -0
- package/distribution/picture-preparation/type.d.ts +18 -0
- package/distribution/picture-preparation/type.d.ts.map +1 -0
- package/distribution/picture-preparation/type.js +2 -0
- package/distribution/picture-preparation/type.js.map +1 -0
- package/distribution/picture-storage/type.d.ts +11 -0
- package/distribution/picture-storage/type.d.ts.map +1 -0
- package/distribution/picture-storage/type.js +2 -0
- package/distribution/picture-storage/type.js.map +1 -0
- package/distribution/recovery-code-regeneration/index.d.ts +19 -0
- package/distribution/recovery-code-regeneration/index.d.ts.map +1 -0
- package/distribution/recovery-code-regeneration/index.js +34 -0
- package/distribution/recovery-code-regeneration/index.js.map +1 -0
- package/distribution/recovery-code-store/index.d.ts +25 -0
- package/distribution/recovery-code-store/index.d.ts.map +1 -0
- package/distribution/recovery-code-store/index.js +34 -0
- package/distribution/recovery-code-store/index.js.map +1 -0
- package/distribution/second-factor-finish/index.d.ts +12 -0
- package/distribution/second-factor-finish/index.d.ts.map +1 -0
- package/distribution/second-factor-finish/index.js +78 -0
- package/distribution/second-factor-finish/index.js.map +1 -0
- package/distribution/second-factor-finish/type.d.ts +27 -0
- package/distribution/second-factor-finish/type.d.ts.map +1 -0
- package/distribution/second-factor-finish/type.js +2 -0
- package/distribution/second-factor-finish/type.js.map +1 -0
- package/distribution/secret-encryption/index.d.ts +17 -0
- package/distribution/secret-encryption/index.d.ts.map +1 -0
- package/distribution/secret-encryption/index.js +37 -0
- package/distribution/secret-encryption/index.js.map +1 -0
- package/distribution/sign-in-finish/index.d.ts +3 -3
- package/distribution/sign-in-finish/index.d.ts.map +1 -1
- package/distribution/sign-in-finish/index.js +22 -5
- package/distribution/sign-in-finish/index.js.map +1 -1
- package/distribution/sign-in-finish/type.d.ts +8 -1
- package/distribution/sign-in-finish/type.d.ts.map +1 -1
- package/distribution/sign-in-start/index.d.ts +4 -6
- package/distribution/sign-in-start/index.d.ts.map +1 -1
- package/distribution/sign-in-start/index.js +14 -62
- package/distribution/sign-in-start/index.js.map +1 -1
- package/distribution/sign-in-start/type.d.ts +7 -0
- package/distribution/sign-in-start/type.d.ts.map +1 -1
- package/distribution/sign-up-finish/index.d.ts +4 -4
- package/distribution/sign-up-finish/index.d.ts.map +1 -1
- package/distribution/sign-up-finish/index.js +11 -6
- package/distribution/sign-up-finish/index.js.map +1 -1
- package/distribution/sign-up-finish/type.d.ts +6 -1
- package/distribution/sign-up-finish/type.d.ts.map +1 -1
- package/distribution/signed-ticket/index.d.ts +1 -1
- package/distribution/signed-ticket/index.js +1 -1
- package/distribution/step-up/index.d.ts +48 -0
- package/distribution/step-up/index.d.ts.map +1 -0
- package/distribution/step-up/index.js +83 -0
- package/distribution/step-up/index.js.map +1 -0
- package/distribution/step-up/type.d.ts +15 -0
- package/distribution/step-up/type.d.ts.map +1 -0
- package/distribution/step-up/type.js +2 -0
- package/distribution/{username → step-up}/type.js.map +1 -1
- package/distribution/test-person/index.d.ts +14 -0
- package/distribution/test-person/index.d.ts.map +1 -0
- package/distribution/test-person/index.js +20 -0
- package/distribution/test-person/index.js.map +1 -0
- package/distribution/totp/type.d.ts +1 -1
- package/distribution/totp/type.d.ts.map +1 -1
- package/distribution/totp-setup/index.d.ts +50 -0
- package/distribution/totp-setup/index.d.ts.map +1 -0
- package/distribution/totp-setup/index.js +136 -0
- package/distribution/totp-setup/index.js.map +1 -0
- package/distribution/totp-setup/type.d.ts +24 -0
- package/distribution/totp-setup/type.d.ts.map +1 -0
- package/distribution/totp-setup/type.js +2 -0
- package/distribution/totp-setup/type.js.map +1 -0
- package/package.json +4 -3
- package/distribution/username/index.d.ts +0 -23
- package/distribution/username/index.d.ts.map +0 -1
- package/distribution/username/index.js +0 -32
- package/distribution/username/index.js.map +0 -1
- package/distribution/username/type.d.ts +0 -10
- package/distribution/username/type.d.ts.map +0 -1
- package/distribution/username-availability/index.d.ts +0 -11
- package/distribution/username-availability/index.d.ts.map +0 -1
- package/distribution/username-availability/index.js +0 -21
- package/distribution/username-availability/index.js.map +0 -1
- package/distribution/username-change/index.d.ts +0 -15
- package/distribution/username-change/index.d.ts.map +0 -1
- package/distribution/username-change/index.js +0 -39
- package/distribution/username-change/index.js.map +0 -1
- package/distribution/username-change/type.d.ts +0 -7
- package/distribution/username-change/type.d.ts.map +0 -1
- /package/distribution/{username-change → account-activity}/type.js +0 -0
- /package/distribution/{username → account-export}/type.js +0 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { accountTables } from "@sharadox/account-web-library/account-server";
|
|
2
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
3
|
+
import { replaceRecoveryCodes } from "@sharadox/account-web-library/recovery-code-store";
|
|
4
|
+
import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
|
|
5
|
+
/**
|
|
6
|
+
* Makes 10 new recovery codes, voiding the old ones, for an account with two-factor on. Show them once. Ask for a
|
|
7
|
+
* fresh code first (step-up).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const result = await regenerateRecoveryCodes(server, { accountId });
|
|
11
|
+
* if (result.status === "made") show(result.recoveryCodes);
|
|
12
|
+
*/
|
|
13
|
+
export async function regenerateRecoveryCodes(server, { accountId, now = Date.now() }) {
|
|
14
|
+
return accountTables(server)
|
|
15
|
+
.transaction()
|
|
16
|
+
.execute(async (transaction) => {
|
|
17
|
+
const on = await transaction
|
|
18
|
+
.selectFrom("totp_credential")
|
|
19
|
+
.select("id")
|
|
20
|
+
.where("account_id", "=", accountId)
|
|
21
|
+
.where("confirmed_at", "is not", null)
|
|
22
|
+
.executeTakeFirst();
|
|
23
|
+
if (!on)
|
|
24
|
+
return { status: "second-factor-off" };
|
|
25
|
+
const recoveryCodes = await replaceRecoveryCodes(transaction, {
|
|
26
|
+
accountId,
|
|
27
|
+
pepper: deriveSecret(server.secret, "recovery-code"),
|
|
28
|
+
now,
|
|
29
|
+
});
|
|
30
|
+
await recordAuditEvent(transaction, { accountId, action: "second-factor.recovery-codes-made", now });
|
|
31
|
+
return { status: "made", recoveryCodes };
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/recovery-code-regeneration/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,mDAAmD,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,MAAqB,EACrB,EAAE,SAAS,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAuC;IAEpE,OAAO,aAAa,CAAC,MAAM,CAAC;SACzB,WAAW,EAAE;SACb,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QAC7B,MAAM,EAAE,GAAG,MAAM,WAAW;aACzB,UAAU,CAAC,iBAAiB,CAAC;aAC7B,MAAM,CAAC,IAAI,CAAC;aACZ,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;aACnC,KAAK,CAAC,cAAc,EAAE,QAAQ,EAAE,IAAI,CAAC;aACrC,gBAAgB,EAAE,CAAC;QACtB,IAAI,CAAC,EAAE;YAAE,OAAO,EAAE,MAAM,EAAE,mBAA4B,EAAE,CAAC;QACzD,MAAM,aAAa,GAAG,MAAM,oBAAoB,CAAC,WAAW,EAAE;YAC5D,SAAS;YACT,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC;YACpD,GAAG;SACJ,CAAC,CAAC;QACH,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,mCAAmC,EAAE,GAAG,EAAE,CAAC,CAAC;QACrG,OAAO,EAAE,MAAM,EAAE,MAAe,EAAE,aAAa,EAAE,CAAC;IACpD,CAAC,CAAC,CAAC;AACP,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* The stored form of a recovery code: an HMAC bound to its account, never the code.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const hash = recoveryCodeHash(typed, { accountId, pepper });
|
|
8
|
+
*/
|
|
9
|
+
export declare function recoveryCodeHash(code: string, { accountId, pepper }: {
|
|
10
|
+
accountId: string;
|
|
11
|
+
pepper: string;
|
|
12
|
+
}): string;
|
|
13
|
+
/**
|
|
14
|
+
* Replaces an account's recovery codes with 10 new ones (inside the caller's transaction) and returns them, to show
|
|
15
|
+
* once. Old codes stop working.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const codes = await replaceRecoveryCodes(transaction, { accountId, pepper });
|
|
19
|
+
*/
|
|
20
|
+
export declare function replaceRecoveryCodes(database: Kysely<AccountDatabase>, { accountId, pepper, now }: {
|
|
21
|
+
accountId: string;
|
|
22
|
+
pepper: string;
|
|
23
|
+
now?: number;
|
|
24
|
+
}): Promise<string[]>;
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/recovery-code-store/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAEnH;AAED;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,MAAM,EAAE,GAAgB,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GAC3F,OAAO,CAAC,MAAM,EAAE,CAAC,CAenB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
|
|
2
|
+
import { hashOneTimeCode } from "@sharadox/account-web-library/one-time-code";
|
|
3
|
+
import { createRecoveryCodes, normalizeRecoveryCode } from "@sharadox/account-web-library/recovery-code";
|
|
4
|
+
/**
|
|
5
|
+
* The stored form of a recovery code: an HMAC bound to its account, never the code.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const hash = recoveryCodeHash(typed, { accountId, pepper });
|
|
9
|
+
*/
|
|
10
|
+
export function recoveryCodeHash(code, { accountId, pepper }) {
|
|
11
|
+
return hashOneTimeCode(normalizeRecoveryCode(code), { pepper, purpose: `recovery:${accountId}` });
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Replaces an account's recovery codes with 10 new ones (inside the caller's transaction) and returns them, to show
|
|
15
|
+
* once. Old codes stop working.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const codes = await replaceRecoveryCodes(transaction, { accountId, pepper });
|
|
19
|
+
*/
|
|
20
|
+
export async function replaceRecoveryCodes(database, { accountId, pepper, now = Date.now() }) {
|
|
21
|
+
const codes = createRecoveryCodes();
|
|
22
|
+
await database.deleteFrom("recovery_code").where("account_id", "=", accountId).execute();
|
|
23
|
+
await database
|
|
24
|
+
.insertInto("recovery_code")
|
|
25
|
+
.values(codes.map((code) => ({
|
|
26
|
+
id: uuidVersion7({ now }),
|
|
27
|
+
account_id: accountId,
|
|
28
|
+
code_hash: recoveryCodeHash(code, { accountId, pepper }),
|
|
29
|
+
created_at: new Date(now),
|
|
30
|
+
})))
|
|
31
|
+
.execute();
|
|
32
|
+
return codes;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/recovery-code-store/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E,OAAO,EAAE,eAAe,EAAE,MAAM,6CAA6C,CAAC;AAC9E,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,6CAA6C,CAAC;AAEzG;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,EAAE,SAAS,EAAE,MAAM,EAAyC;IACzG,OAAO,eAAe,CAAC,qBAAqB,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,SAAS,EAAE,EAAE,CAAC,CAAC;AACpG,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,QAAiC,EACjC,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAuD;IAE5F,MAAM,KAAK,GAAG,mBAAmB,EAAE,CAAC;IACpC,MAAM,QAAQ,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC;IACzF,MAAM,QAAQ;SACX,UAAU,CAAC,eAAe,CAAC;SAC3B,MAAM,CACL,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACnB,EAAE,EAAE,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC;QACzB,UAAU,EAAE,SAAS;QACrB,SAAS,EAAE,gBAAgB,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC;QACxD,UAAU,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC;KAC1B,CAAC,CAAC,CACJ;SACA,OAAO,EAAE,CAAC;IACb,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
|
|
2
|
+
import type { SecondFactorFinish, SecondFactorFinishResult } from "@sharadox/account-web-library/second-factor-finish/type";
|
|
3
|
+
/**
|
|
4
|
+
* The two-factor step of signing in: the authenticator app's code (never the same step twice) or a single-use
|
|
5
|
+
* recovery code, after the emailed code passed (`finishSignIn` gave the ticket). Guesses are limited per account and
|
|
6
|
+
* per address group. The session is marked as having passed two-factor.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const result = await finishSecondFactor(server, { ticket, code, address, token: cookie });
|
|
10
|
+
*/
|
|
11
|
+
export declare function finishSecondFactor(server: AccountServer, { ticket, code, address, token, userAgent, now }: SecondFactorFinish): Promise<SecondFactorFinishResult>;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/second-factor-finish/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAMvF,OAAO,KAAK,EACV,kBAAkB,EAClB,wBAAwB,EACzB,MAAM,yDAAyD,CAAC;AAIjE;;;;;;;GAOG;AACH,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,aAAa,EACrB,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,SAAgB,EAAE,GAAgB,EAAE,EAAE,kBAAkB,GACvF,OAAO,CAAC,wBAAwB,CAAC,CA6DnC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { addressGroup } from "@sharadox/protection-web-library/address-group";
|
|
2
|
+
import { limitRate } from "@sharadox/protection-web-library/rate-limit";
|
|
3
|
+
import { accountTables, signInLimits } from "@sharadox/account-web-library/account-server";
|
|
4
|
+
import { signIn } from "@sharadox/account-web-library/account-sign-in";
|
|
5
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
6
|
+
import { recoveryCodeHash } from "@sharadox/account-web-library/recovery-code-store";
|
|
7
|
+
import { decryptSecret } from "@sharadox/account-web-library/secret-encryption";
|
|
8
|
+
import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
|
|
9
|
+
import { readTicket } from "@sharadox/account-web-library/signed-ticket";
|
|
10
|
+
import { verifyTotp } from "@sharadox/account-web-library/totp";
|
|
11
|
+
/**
|
|
12
|
+
* The two-factor step of signing in: the authenticator app's code (never the same step twice) or a single-use
|
|
13
|
+
* recovery code, after the emailed code passed (`finishSignIn` gave the ticket). Guesses are limited per account and
|
|
14
|
+
* per address group. The session is marked as having passed two-factor.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const result = await finishSecondFactor(server, { ticket, code, address, token: cookie });
|
|
18
|
+
*/
|
|
19
|
+
export async function finishSecondFactor(server, { ticket, code, address, token, userAgent = null, now = Date.now() }) {
|
|
20
|
+
const reading = readTicket(ticket, {
|
|
21
|
+
secret: deriveSecret(server.secret, "second-factor-ticket"),
|
|
22
|
+
purpose: "second-factor",
|
|
23
|
+
now,
|
|
24
|
+
});
|
|
25
|
+
if (!reading.valid)
|
|
26
|
+
return { status: "refused", reason: "ticket" };
|
|
27
|
+
const { accountId } = reading.data;
|
|
28
|
+
const limits = signInLimits(server);
|
|
29
|
+
const rate = await limitRate(server.database, [
|
|
30
|
+
{ key: `second-factor:${accountId}`, rule: limits.secondFactorPerAccount },
|
|
31
|
+
{ key: `verification:${addressGroup(address)}`, rule: limits.verificationsPerAddress },
|
|
32
|
+
], { secret: deriveSecret(server.secret, "rate-limit"), now });
|
|
33
|
+
if (!rate.allowed)
|
|
34
|
+
return { status: "refused", reason: "rate-limit", retryAfterSeconds: rate.retryAfterSeconds };
|
|
35
|
+
const passed = await accountTables(server)
|
|
36
|
+
.transaction()
|
|
37
|
+
.execute(async (transaction) => {
|
|
38
|
+
const credential = await transaction
|
|
39
|
+
.selectFrom("totp_credential")
|
|
40
|
+
.select(["id", "secret_ciphertext", "last_used_step"])
|
|
41
|
+
.where("account_id", "=", accountId)
|
|
42
|
+
.where("confirmed_at", "is not", null)
|
|
43
|
+
.forUpdate()
|
|
44
|
+
.executeTakeFirst();
|
|
45
|
+
// Two-factor was turned off since the emailed code passed: that code is the proof.
|
|
46
|
+
if (!credential)
|
|
47
|
+
return true;
|
|
48
|
+
const typed = code.replace(/\s/g, "");
|
|
49
|
+
const secret = decryptSecret(credential.secret_ciphertext, deriveSecret(server.secret, "totp-encryption"));
|
|
50
|
+
// Confirming the setup stored a step, so a confirmed credential always has one.
|
|
51
|
+
const lastUsedStep = Number(credential.last_used_step);
|
|
52
|
+
const verification = verifyTotp(secret, typed, now, { lastUsedStep });
|
|
53
|
+
if (verification.valid) {
|
|
54
|
+
await transaction
|
|
55
|
+
.updateTable("totp_credential")
|
|
56
|
+
.set({ last_used_step: verification.step })
|
|
57
|
+
.where("id", "=", credential.id)
|
|
58
|
+
.execute();
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
const used = await transaction
|
|
62
|
+
.updateTable("recovery_code")
|
|
63
|
+
.set({ used_at: new Date(now) })
|
|
64
|
+
.where("account_id", "=", accountId)
|
|
65
|
+
.where("code_hash", "=", recoveryCodeHash(code, { accountId, pepper: deriveSecret(server.secret, "recovery-code") }))
|
|
66
|
+
.where("used_at", "is", null)
|
|
67
|
+
.executeTakeFirst();
|
|
68
|
+
if (used.numUpdatedRows === 0n)
|
|
69
|
+
return false;
|
|
70
|
+
await recordAuditEvent(transaction, { accountId, action: "second-factor.recovery-code-used", now });
|
|
71
|
+
return true;
|
|
72
|
+
});
|
|
73
|
+
if (!passed)
|
|
74
|
+
return { status: "refused", reason: "wrong" };
|
|
75
|
+
const session = await signIn(server.database, { accountId, token, secondFactor: true, userAgent, now });
|
|
76
|
+
return { status: "signed-in", accountId, token: session.token };
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/second-factor-finish/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gDAAgD,CAAC;AAC9E,OAAO,EAAE,SAAS,EAAE,MAAM,6CAA6C,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,8CAA8C,CAAC;AAE3F,OAAO,EAAE,MAAM,EAAE,MAAM,+CAA+C,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,mDAAmD,CAAC;AACrF,OAAO,EAAE,aAAa,EAAE,MAAM,iDAAiD,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE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B,GAAG,CAAC,EAAE,OAAO,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;aAC/B,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;aACnC,KAAK,CACJ,WAAW,EACX,GAAG,EACH,gBAAgB,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,EAAE,CAAC,CAC5F;aACA,KAAK,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC;aAC5B,gBAAgB,EAAE,CAAC;QACtB,IAAI,IAAI,CAAC,cAAc,KAAK,EAAE;YAAE,OAAO,KAAK,CAAC;QAC7C,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,kCAAkC,EAAE,GAAG,EAAE,CAAC,CAAC;QACpG,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;IACL,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IAC3D,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,CAAC;IACxG,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface SecondFactorFinish {
|
|
2
|
+
/** From `finishSignIn`'s `second-factor` answer. */
|
|
3
|
+
ticket: string;
|
|
4
|
+
/** The authenticator app's code, or a recovery code. */
|
|
5
|
+
code: string;
|
|
6
|
+
/** The client's IP address. */
|
|
7
|
+
address: string;
|
|
8
|
+
/** The browser's current session cookie, if any. */
|
|
9
|
+
token: string | undefined;
|
|
10
|
+
/** The browser's User-Agent, for the list of devices. */
|
|
11
|
+
userAgent?: string | null;
|
|
12
|
+
/** Milliseconds since 1970 (now by default). */
|
|
13
|
+
now?: number;
|
|
14
|
+
}
|
|
15
|
+
export type SecondFactorFinishResult = {
|
|
16
|
+
status: "signed-in";
|
|
17
|
+
accountId: string;
|
|
18
|
+
token: string;
|
|
19
|
+
} | {
|
|
20
|
+
status: "refused";
|
|
21
|
+
reason: "ticket" | "wrong";
|
|
22
|
+
} | {
|
|
23
|
+
status: "refused";
|
|
24
|
+
reason: "rate-limit";
|
|
25
|
+
retryAfterSeconds: number;
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/second-factor-finish/type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC,oDAAoD;IACpD,MAAM,EAAE,MAAM,CAAC;IACf,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,oDAAoD;IACpD,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,wBAAwB,GAChC;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACzD;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,QAAQ,GAAG,OAAO,CAAA;CAAE,GACjD;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,YAAY,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/second-factor-finish/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encrypts a secret for storing in the database (a TOTP secret) with AES-256-GCM under a key kept outside it
|
|
3
|
+
* (`deriveSecret(ACCOUNT_SECRET, "totp-encryption")`), so a leaked database dump reveals nothing. A fresh random IV
|
|
4
|
+
* every time; the result is text: `v1.<iv>.<ciphertext and tag>`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const sealed = encryptSecret(secret, deriveSecret(server.secret, "totp-encryption"));
|
|
8
|
+
*/
|
|
9
|
+
export declare function encryptSecret(secret: Uint8Array, key: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Decrypts what `encryptSecret` made; throws for a wrong key or anything changed.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const secret = decryptSecret(row.secret_ciphertext, key);
|
|
15
|
+
*/
|
|
16
|
+
export declare function decryptSecret(sealed: string, key: string): Uint8Array;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/secret-encryption/index.ts"],"names":[],"mappings":"AAQA;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAKrE;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,UAAU,CAOrE"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes } from "node:crypto";
|
|
2
|
+
const VERSION = "v1";
|
|
3
|
+
const keyBytes = (key) => {
|
|
4
|
+
if (key.length < 32)
|
|
5
|
+
throw new Error("the encryption key must have at least 32 characters");
|
|
6
|
+
return createHash("sha256").update(key).digest();
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Encrypts a secret for storing in the database (a TOTP secret) with AES-256-GCM under a key kept outside it
|
|
10
|
+
* (`deriveSecret(ACCOUNT_SECRET, "totp-encryption")`), so a leaked database dump reveals nothing. A fresh random IV
|
|
11
|
+
* every time; the result is text: `v1.<iv>.<ciphertext and tag>`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const sealed = encryptSecret(secret, deriveSecret(server.secret, "totp-encryption"));
|
|
15
|
+
*/
|
|
16
|
+
export function encryptSecret(secret, key) {
|
|
17
|
+
const iv = randomBytes(12);
|
|
18
|
+
const cipher = createCipheriv("aes-256-gcm", keyBytes(key), iv);
|
|
19
|
+
const body = Buffer.concat([cipher.update(secret), cipher.final(), cipher.getAuthTag()]);
|
|
20
|
+
return `${VERSION}.${iv.toString("base64url")}.${body.toString("base64url")}`;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Decrypts what `encryptSecret` made; throws for a wrong key or anything changed.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const secret = decryptSecret(row.secret_ciphertext, key);
|
|
27
|
+
*/
|
|
28
|
+
export function decryptSecret(sealed, key) {
|
|
29
|
+
const [version, iv, body] = sealed.split(".");
|
|
30
|
+
if (version !== VERSION || !iv || !body)
|
|
31
|
+
throw new Error("unknown encrypted secret format");
|
|
32
|
+
const bytes = Buffer.from(body, "base64url");
|
|
33
|
+
const decipher = createDecipheriv("aes-256-gcm", keyBytes(key), Buffer.from(iv, "base64url"));
|
|
34
|
+
decipher.setAuthTag(bytes.subarray(-16));
|
|
35
|
+
return new Uint8Array(Buffer.concat([decipher.update(bytes.subarray(0, -16)), decipher.final()]));
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/secret-encryption/index.ts"],"names":[],"mappings":"A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IAAI,EAAE,WAAW,CAAC,CAAC;IAC7C,MAAM,QAAQ,GAAG,gBAAgB,CAAC,aAAa,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC,CAAC;IAC9F,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACzC,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;AACpG,CAAC"}
|
|
@@ -2,11 +2,11 @@ import type { AccountServer } from "@sharadox/account-web-library/account-server
|
|
|
2
2
|
import type { SignInFinish, SignInFinishResult } from "@sharadox/account-web-library/sign-in-finish/type";
|
|
3
3
|
/**
|
|
4
4
|
* The second step: the typed code. Guesses are limited per address group (and per request by `verifyCode`). With the
|
|
5
|
-
* right code, an account with that contact is signed in on the browser (the new token goes in its cookie); a contact
|
|
6
|
-
* with no account gets a sign-up ticket for choosing
|
|
5
|
+
* right code, an account with that contact is signed in (or, with two-factor on, gets a ticket for that step) on the browser (the new token goes in its cookie); a contact
|
|
6
|
+
* with no account gets a sign-up ticket for choosing an identifier.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* const result = await finishSignIn(server, { requestId, code, address, token: cookie });
|
|
10
10
|
*/
|
|
11
|
-
export declare function finishSignIn(server: AccountServer, { requestId, code, address, token, now }: SignInFinish): Promise<SignInFinishResult>;
|
|
11
|
+
export declare function finishSignIn(server: AccountServer, { requestId, code, address, token, userAgent, now }: SignInFinish): Promise<SignInFinishResult>;
|
|
12
12
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/sign-in-finish/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAKvF,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,mDAAmD,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/sign-in-finish/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAKvF,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,mDAAmD,CAAC;AAO1G;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,aAAa,EACrB,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,SAAgB,EAAE,GAAgB,EAAE,EAAE,YAAY,GACpF,OAAO,CAAC,kBAAkB,CAAC,CAiD7B"}
|
|
@@ -6,17 +6,19 @@ import { verifyCode } from "@sharadox/account-web-library/code-verification";
|
|
|
6
6
|
import { findAccountByContact } from "@sharadox/account-web-library/contact-lookup";
|
|
7
7
|
import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
|
|
8
8
|
import { createTicket } from "@sharadox/account-web-library/signed-ticket";
|
|
9
|
-
/** How long a sign-up ticket lasts: time to choose
|
|
9
|
+
/** How long a sign-up ticket lasts: time to choose an identifier. */
|
|
10
10
|
const SIGN_UP_SECONDS = 900;
|
|
11
|
+
/** How long the two-factor step may take once the emailed code passed. */
|
|
12
|
+
const SECOND_FACTOR_SECONDS = 600;
|
|
11
13
|
/**
|
|
12
14
|
* The second step: the typed code. Guesses are limited per address group (and per request by `verifyCode`). With the
|
|
13
|
-
* right code, an account with that contact is signed in on the browser (the new token goes in its cookie); a contact
|
|
14
|
-
* with no account gets a sign-up ticket for choosing
|
|
15
|
+
* right code, an account with that contact is signed in (or, with two-factor on, gets a ticket for that step) on the browser (the new token goes in its cookie); a contact
|
|
16
|
+
* with no account gets a sign-up ticket for choosing an identifier.
|
|
15
17
|
*
|
|
16
18
|
* @example
|
|
17
19
|
* const result = await finishSignIn(server, { requestId, code, address, token: cookie });
|
|
18
20
|
*/
|
|
19
|
-
export async function finishSignIn(server, { requestId, code, address, token, now = Date.now() }) {
|
|
21
|
+
export async function finishSignIn(server, { requestId, code, address, token, userAgent = null, now = Date.now() }) {
|
|
20
22
|
const rate = await limitRate(server.database, [{ key: `verification:${addressGroup(address)}`, rule: signInLimits(server).verificationsPerAddress }], { secret: deriveSecret(server.secret, "rate-limit"), now });
|
|
21
23
|
if (!rate.allowed)
|
|
22
24
|
return { status: "refused", reason: "rate-limit", retryAfterSeconds: rate.retryAfterSeconds };
|
|
@@ -41,7 +43,22 @@ export async function finishSignIn(server, { requestId, code, address, token, no
|
|
|
41
43
|
}
|
|
42
44
|
if (!found.signIn)
|
|
43
45
|
return { status: "refused", reason: "unknown" };
|
|
44
|
-
const
|
|
46
|
+
const secondFactor = await server.database
|
|
47
|
+
.selectFrom("totp_credential")
|
|
48
|
+
.select("id")
|
|
49
|
+
.where("account_id", "=", found.accountId)
|
|
50
|
+
.where("confirmed_at", "is not", null)
|
|
51
|
+
.executeTakeFirst();
|
|
52
|
+
if (secondFactor) {
|
|
53
|
+
const ticket = createTicket({ accountId: found.accountId }, {
|
|
54
|
+
secret: deriveSecret(server.secret, "second-factor-ticket"),
|
|
55
|
+
purpose: "second-factor",
|
|
56
|
+
lifetimeSeconds: SECOND_FACTOR_SECONDS,
|
|
57
|
+
now,
|
|
58
|
+
});
|
|
59
|
+
return { status: "second-factor", ticket };
|
|
60
|
+
}
|
|
61
|
+
const session = await signIn(server.database, { accountId: found.accountId, token, userAgent, now });
|
|
45
62
|
return { status: "signed-in", accountId: found.accountId, token: session.token };
|
|
46
63
|
}
|
|
47
64
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/sign-in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AAC,KAAK,CAAC,MAAM;QAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IACnE,MAAM,YAAY,GAAG,MAAM,MAAM,CAAC,QAAQ;SACvC,UAAU,CAAC,iBAAiB,CAAC;SAC7B,MAAM,CAAC,IAAI,CAAC;SACZ,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,KAAK,CAAC,SAAS,CAAC;SACzC,KAAK,CAAC,cAAc,EAAE,QAAQ,EAAE,IAAI,CAAC;SACrC,gBAAgB,EAAE,CAAC;IACtB,IAAI,YAAY,EAAE,CAAC;QACjB,MAAM,MAAM,GAAG,YAAY,CACzB,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,EAC9B;YACE,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC;YAC3D,OAAO,EAAE,eAAe;YACxB,eAAe,EAAE,qBAAqB;YACtC,GAAG;SACJ,CACF,CAAC;QACF,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,CAAC;IAC7C,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,CAAC;IACrG,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC;AACnF,CAAC"}
|
|
@@ -7,6 +7,8 @@ export interface SignInFinish {
|
|
|
7
7
|
address: string;
|
|
8
8
|
/** The browser's current session cookie, if any: the account joins its accounts. */
|
|
9
9
|
token: string | undefined;
|
|
10
|
+
/** The browser's User-Agent, for the list of devices. */
|
|
11
|
+
userAgent?: string | null;
|
|
10
12
|
/** Milliseconds since 1970 (now by default). */
|
|
11
13
|
now?: number;
|
|
12
14
|
}
|
|
@@ -18,7 +20,12 @@ export type SignInFinishResult = {
|
|
|
18
20
|
accountId: string;
|
|
19
21
|
token: string;
|
|
20
22
|
}
|
|
21
|
-
/**
|
|
23
|
+
/** The account has two-factor on: ask for its authenticator's code (or a recovery code), then `finishSecondFactor`. */
|
|
24
|
+
| {
|
|
25
|
+
status: "second-factor";
|
|
26
|
+
ticket: string;
|
|
27
|
+
}
|
|
28
|
+
/** No account has this contact yet: ask for an identifier, then `finishSignUp` with the ticket. */
|
|
22
29
|
| {
|
|
23
30
|
status: "sign-up";
|
|
24
31
|
ticket: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/sign-in-finish/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sDAAsD,CAAC;AAEnG,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,oFAAoF;IACpF,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,KAAK,OAAO,GAAG,OAAO,CAAC,sBAAsB,EAAE;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC,CAAC,QAAQ,CAAC,CAAC;AAE3E,MAAM,MAAM,kBAAkB,GAC1B;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE;AAC3D,
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/sign-in-finish/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sDAAsD,CAAC;AAEnG,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,oFAAoF;IACpF,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,KAAK,OAAO,GAAG,OAAO,CAAC,sBAAsB,EAAE;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC,CAAC,QAAQ,CAAC,CAAC;AAE3E,MAAM,MAAM,kBAAkB,GAC1B;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE;AAC3D,uHAAuH;GACrH;IAAE,MAAM,EAAE,eAAe,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE;AAC7C,mGAAmG;GACjG;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACrC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GACtC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,YAAY,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
|
@@ -5,15 +5,13 @@ import type { SignInStart, SignInStartResult } from "@sharadox/account-web-libra
|
|
|
5
5
|
* cheaper than what it protects:
|
|
6
6
|
* 1. a single-use proof of work;
|
|
7
7
|
* 2. a valid contact with a channel;
|
|
8
|
-
* 3. rate limits
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* (`code.sent`, or `code.delivery-failed` with the error, never the code; a failure is thrown).
|
|
12
|
-
* The answer never depends on whether an account exists, so nobody can probe for accounts.
|
|
8
|
+
* 3. `sendCode`: rate limits, the channel's spending cap, then a new code sent by `deliver` after answering.
|
|
9
|
+
* A browser already signed in to the account with that verified contact gets `signed-in` instead, and nothing is sent.
|
|
10
|
+
* Otherwise the answer never depends on whether an account exists, so nobody can probe for accounts.
|
|
13
11
|
*
|
|
14
12
|
* @example
|
|
15
13
|
* const result = await startSignIn(server, { contact, solution, address: clientAddress(request), language });
|
|
16
14
|
* if (result.status === "sent") after(result.deliver);
|
|
17
15
|
*/
|
|
18
|
-
export declare function startSignIn(server: AccountServer, { contact, solution, address, language, now }: SignInStart): Promise<SignInStartResult>;
|
|
16
|
+
export declare function startSignIn(server: AccountServer, { contact, solution, address, language, token, now }: SignInStart): Promise<SignInStartResult>;
|
|
19
17
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/sign-in-start/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/sign-in-start/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAOvF,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,kDAAkD,CAAC;AAEvG;;;;;;;;;;;;GAYG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE,aAAa,EACrB,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAgB,EAAE,EAAE,WAAW,GAC7E,OAAO,CAAC,iBAAiB,CAAC,CAqB5B"}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import { addressGroup } from "@sharadox/protection-web-library/address-group";
|
|
2
|
-
import { limitRate } from "@sharadox/protection-web-library/rate-limit";
|
|
3
1
|
import { redeemSolution } from "@sharadox/protection-web-library/solution-redemption";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { requestCode } from "@sharadox/account-web-library/code-request";
|
|
2
|
+
import { readBrowserSession } from "@sharadox/account-web-library/browser-session-read";
|
|
3
|
+
import { sendCode } from "@sharadox/account-web-library/code-sending";
|
|
4
|
+
import { findAccountByContact } from "@sharadox/account-web-library/contact-lookup";
|
|
8
5
|
import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
|
|
9
6
|
import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
|
|
10
7
|
/**
|
|
@@ -12,18 +9,15 @@ import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
|
|
|
12
9
|
* cheaper than what it protects:
|
|
13
10
|
* 1. a single-use proof of work;
|
|
14
11
|
* 2. a valid contact with a channel;
|
|
15
|
-
* 3. rate limits
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* (`code.sent`, or `code.delivery-failed` with the error, never the code; a failure is thrown).
|
|
19
|
-
* The answer never depends on whether an account exists, so nobody can probe for accounts.
|
|
12
|
+
* 3. `sendCode`: rate limits, the channel's spending cap, then a new code sent by `deliver` after answering.
|
|
13
|
+
* A browser already signed in to the account with that verified contact gets `signed-in` instead, and nothing is sent.
|
|
14
|
+
* Otherwise the answer never depends on whether an account exists, so nobody can probe for accounts.
|
|
20
15
|
*
|
|
21
16
|
* @example
|
|
22
17
|
* const result = await startSignIn(server, { contact, solution, address: clientAddress(request), language });
|
|
23
18
|
* if (result.status === "sent") after(result.deliver);
|
|
24
19
|
*/
|
|
25
|
-
export async function startSignIn(server, { contact, solution, address, language, now = Date.now() }) {
|
|
26
|
-
const limits = signInLimits(server);
|
|
20
|
+
export async function startSignIn(server, { contact, solution, address, language, token, now = Date.now() }) {
|
|
27
21
|
const proof = await redeemSolution(server.database, solution, {
|
|
28
22
|
secret: deriveSecret(server.secret, "proof-of-work"),
|
|
29
23
|
now,
|
|
@@ -40,54 +34,12 @@ export async function startSignIn(server, { contact, solution, address, language
|
|
|
40
34
|
const channel = server.channels.find(({ kind }) => kind === normalized.kind);
|
|
41
35
|
if (!channel)
|
|
42
36
|
return { status: "refused", reason: "contact" };
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
{
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const cap = await spendFromCap(server.database, { name: `${normalized.kind}-send`, limit: limits.dailySendsPerChannel, period: "day" }, { now });
|
|
51
|
-
if (!cap.allowed)
|
|
52
|
-
return { status: "refused", reason: "unavailable" };
|
|
53
|
-
const request = await requestCode(server.database, {
|
|
54
|
-
purpose: "sign-in",
|
|
55
|
-
contact: normalized,
|
|
56
|
-
pepper: deriveSecret(server.secret, "one-time-code"),
|
|
57
|
-
now,
|
|
58
|
-
});
|
|
59
|
-
return {
|
|
60
|
-
status: "sent",
|
|
61
|
-
requestId: request.requestId,
|
|
62
|
-
deliver: async () => {
|
|
63
|
-
try {
|
|
64
|
-
await channel.send({
|
|
65
|
-
contact: request.contact,
|
|
66
|
-
code: request.code,
|
|
67
|
-
purpose: "sign-in",
|
|
68
|
-
language,
|
|
69
|
-
expiresAt: request.expiresAt,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
// Nobody is waiting for the answer any more: the audit log is where a failed send shows (never the code).
|
|
74
|
-
const { name, message } = error;
|
|
75
|
-
await recordAuditEvent(server.database, {
|
|
76
|
-
action: "code.delivery-failed",
|
|
77
|
-
detail: {
|
|
78
|
-
kind: request.contact.kind,
|
|
79
|
-
purpose: "sign-in",
|
|
80
|
-
requestId: request.requestId,
|
|
81
|
-
error: `${name}: ${message}`.slice(0, 500),
|
|
82
|
-
},
|
|
83
|
-
});
|
|
84
|
-
throw error;
|
|
85
|
-
}
|
|
86
|
-
await recordAuditEvent(server.database, {
|
|
87
|
-
action: "code.sent",
|
|
88
|
-
detail: { kind: request.contact.kind, purpose: "sign-in", requestId: request.requestId },
|
|
89
|
-
});
|
|
90
|
-
},
|
|
91
|
-
};
|
|
37
|
+
if (token) {
|
|
38
|
+
const owner = await findAccountByContact(server.database, normalized);
|
|
39
|
+
const browser = owner && (await readBrowserSession(server.database, { token, now }));
|
|
40
|
+
if (owner && browser?.accounts.some(({ accountId }) => accountId === owner.accountId))
|
|
41
|
+
return { status: "signed-in", accountId: owner.accountId };
|
|
42
|
+
}
|
|
43
|
+
return sendCode(server, { purpose: "sign-in", contact: normalized, channel, address, language, now });
|
|
92
44
|
}
|
|
93
45
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/sign-in-start/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/sign-in-start/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAC;AAEtF,OAAO,EAAE,kBAAkB,EAAE,MAAM,oDAAoD,CAAC;AACxF,OAAO,EAAE,QAAQ,EAAE,MAAM,4CAA4C,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,8CAA8C,CAAC;AACpF,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAE9E,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAG/E;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,MAAqB,EACrB,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAe;IAE9E,MAAM,KAAK,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE;QAC5D,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC;QACpD,GAAG;KACJ,CAAC,CAAC;IACH,IAAI,CAAC,KAAK,CAAC,KAAK;QAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,OA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** The browser's session cookie, if any: a contact of an account already signed in there gets no code. */
|
|
12
|
+
token?: string | undefined;
|
|
11
13
|
/** Milliseconds since 1970 (now by default). */
|
|
12
14
|
now?: number;
|
|
13
15
|
}
|
|
@@ -16,6 +18,11 @@ export type SignInStartResult = {
|
|
|
16
18
|
requestId: string;
|
|
17
19
|
/** Sends the code: call it after answering (Next.js `after()`), so the answer's timing reveals nothing. */
|
|
18
20
|
deliver: () => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
/** The browser is already signed in to the account with this verified contact: nothing was sent. */
|
|
23
|
+
| {
|
|
24
|
+
status: "signed-in";
|
|
25
|
+
accountId: string;
|
|
19
26
|
} | {
|
|
20
27
|
status: "refused";
|
|
21
28
|
reason: "proof" | "contact" | "unavailable";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/sign-in-start/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qDAAqD,CAAC;AACpF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,QAAQ,EAAE,QAAQ,CAAC;IACnB,+DAA+D;IAC/D,OAAO,EAAE,MAAM,CAAC;IAChB,8BAA8B;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,iBAAiB,GACzB;IACE,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,2GAA2G;IAC3G,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B,
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/sign-in-start/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MA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