Compare commits

..

1 commit

Author SHA1 Message Date
unlinkability efe4789e83
Merge 735878fe6a into b59d1b24ff 2024-06-18 13:11:13 -05:00
3 changed files with 12 additions and 32 deletions

View file

@ -4,14 +4,12 @@ import crypto from "node:crypto"
export type Intent = "verifyEmail" | "recoverAccount" | "identityProof" export type Intent = "verifyEmail" | "recoverAccount" | "identityProof"
export const Intents = { export const Intents = {
verifyEmail: { verifyEmail: {},
limit: 2
},
recoverAccount: {}, recoverAccount: {},
identityProof: { identityProof: {
codeGenerator: crypto.randomUUID codeGenerator: crypto.randomUUID
} }
} as Record<Intent, {codeGenerator?: () => string, limit?: number}> } as Record<Intent, {codeGenerator?: () => string}>
export function isIntent(intent: string): intent is Intent { export function isIntent(intent: string): intent is Intent {
return intent in Intents return intent in Intents
@ -74,15 +72,4 @@ export class Code {
check(forUser: string) { check(forUser: string) {
return forUser === this.for return forUser === this.for
} }
}
export function code(...params: ConstructorParameters<typeof Code>): { success: true, code: Code } | { success: false, error: string } {
const [intent, forUser] = params
const {limit = 100} = Intents[intent]
const {length: codeCount} = codes[intent].byUser.get(forUser) || [];
if (codeCount >= limit)
return { success: false, error: `Too many active codes for intent ${intent} (${limit})` }
else
return { success: true, code: new Code(...params) }
} }

View file

@ -111,12 +111,12 @@ const validators: {
// send verification email // send verification email
const tryCode = CodeMgr.code("verifyEmail", target.id, params.email) if (
(CodeMgr.codes.verifyEmail.byUser.get(target.id)?.length || 0) >= 2
)
return [429, "you have too many active codes"]
if (!tryCode.success) let code = new CodeMgr.Code("verifyEmail", target.id, params.email)
return [429, tryCode.error]
const { code } = tryCode
sendMail( sendMail(
params.email, params.email,
@ -264,7 +264,7 @@ export default function (files: Files) {
password: AccountSchemas.StringPassword, password: AccountSchemas.StringPassword,
invite: z.string().max(6) invite: z.string().max(6)
}).omit( }).omit(
!Configuration.accounts.requiredForUpload Configuration.accounts.requiredForUpload
? { invite: true } ? { invite: true }
: {} : {}
)), async (ctx) => { )), async (ctx) => {
@ -302,9 +302,7 @@ export default function (files: Files) {
router.patch( router.patch(
"/:user", "/:user",
scheme( scheme(UserUpdateScheme),
UserUpdateScheme
),
assertAPI( assertAPI(
ctx => ctx =>
Object.keys(ctx.get("parsedScheme")) Object.keys(ctx.get("parsedScheme"))

View file

@ -49,7 +49,7 @@ export default function () {
return ctx.json(["none"]) // if we add 2fa in the future, return available 2fa methods return ctx.json(["none"]) // if we add 2fa in the future, return available 2fa methods
}) })
router.post("/", requiresAccount, scheme( router.post("/", scheme(
ProofCreationSchema ProofCreationSchema
), async (ctx) => { ), async (ctx) => {
@ -68,17 +68,12 @@ export default function () {
// if actor does have 2fa in an else block here // if actor does have 2fa in an else block here
const tryCode = CodeMgr.code( return ctx.text(new CodeMgr.Code(
"identityProof", "identityProof",
target.id, target.id,
Boolean(actor), // so that you can only log in with proofs created when logged out Boolean(actor), // so that you can only log in with proofs created when logged out
5 * 60 * 1000 5 * 60 * 1000
) ).id)
if (!tryCode.success)
return ServeError(ctx, 429, tryCode.error)
return ctx.text(tryCode.code.id)
}) })
return router return router