import type { AuthenticationResponseJSON, GenerateAuthenticationOptionsOpts, GenerateRegistrationOptionsOpts, RegistrationResponseJSON, VerifiedAuthenticationResponse, VerifiedRegistrationResponse, VerifyAuthenticationResponseOpts, VerifyRegistrationResponseOpts, } from '@simplewebauthn/server' import type { APIGatewayProxyEvent, Context as LambdaContext } from 'aws-lambda' import md5 from 'md5' import { v4 as uuidv4 } from 'uuid' import type { CorsConfig, CorsContext, PartialRequest } from '@cedarjs/api' import { createCorsContext, isFetchApiRequest, normalizeRequest, } from '@cedarjs/api' import * as DbAuthError from './errors.js' import { isProxiedRequest, isRequestOriginTrusted, requiresOriginValidation, resolveRequestHost, resolveRequestProtocol, } from './originValidation.js' import { buildCookieAttributes, createAuthProviderCookieString, createCsrfToken, createExpiresAtDate, createLoginResponse, createSessionCookieString, DEFAULT_ALLOWED_USER_FIELDS, sanitizeUser, } from './session.js' import type { DbAuthCookieConfig } from './session.js' import { decryptSession, extractCookie, extractHashingOptions, generateCookieName, getDbAuthResponseBuilder, getSession, hashPassword, hashToken, isLegacySession, legacyHashPassword, webAuthnSession, } from './shared.js' interface SignupFlowOptions> { /** * Allow users to sign up. Defaults to true. * Needs to be explicitly set to false to disable the flow */ enabled?: boolean /** * Whatever you want to happen to your data on new user signup. Redwood will * check for duplicate usernames before calling this handler. At a minimum * you need to save the `username`, `hashedPassword` and `salt` to your * user table. `userAttributes` contains any additional object members that * were included in the object given to the `signUp()` function you got * from `useAuth()` */ handler: (signupHandlerOptions: SignupHandlerOptions) => any /** * Validate the user-supplied password with whatever logic you want. Return * `true` if valid, throw `PasswordValidationError` if not. */ passwordValidation?: (password: string) => boolean /** * Object containing error strings */ errors?: { fieldMissing?: string usernameTaken?: string flowNotEnabled?: string } /** * Allows the user to define if the UserCheck for their selected db provider should use case insensitive */ usernameMatch?: string } interface ForgotPasswordFlowOptions { /** * Allow users to request a new password via a call to forgotPassword. Defaults to true. * Needs to be explicitly set to false to disable the flow */ enabled?: boolean handler: (user: TUser, token: string) => any errors?: { usernameNotFound?: string usernameRequired?: string flowNotEnabled?: string } expires: number } interface LoginFlowOptions { /** * Allow users to login. Defaults to true. * Needs to be explicitly set to false to disable the flow */ enabled?: boolean /** * Anything you want to happen before logging the user in. This can include * throwing an error to prevent login. If you do want to allow login, this * function must return an object representing the user you want to be logged * in, containing at least an `id` field (whatever named field was provided * for `authFields.id`). For example: `return { id: user.id }` */ handler: (user: TUser) => any /** * Object containing error strings */ errors?: { usernameOrPasswordMissing?: string usernameNotFound?: string incorrectPassword?: string flowNotEnabled?: string } /** * How long a user will remain logged in, in seconds */ expires: number /** * Allows the user to define if the UserCheck for their selected db provider should use case insensitive */ usernameMatch?: string } interface ResetPasswordFlowOptions { /** * Allow users to reset their password via a code from a call to forgotPassword. Defaults to true. * Needs to be explicitly set to false to disable the flow */ enabled?: boolean handler: (user: TUser) => boolean | Promise allowReusedPassword: boolean errors?: { resetTokenExpired?: string resetTokenInvalid?: string resetTokenRequired?: string reusedPassword?: string flowNotEnabled?: string } } interface ChangePasswordFlowOptions { /** * Allow logged in users to change their password by providing their current * one. Defaults to true. * Needs to be explicitly set to false to disable the flow */ enabled?: boolean /** * Invoked after the new password has been saved. This is where you can * notify the user that their password was changed. Returning anything * truthy keeps the user logged in. Returning anything falsy logs them out. */ handler: (user: TUser) => boolean | Promise /** * If `false` (the default) the new password MUST be different from the * current one */ allowReusedPassword?: boolean errors?: { currentPasswordRequired?: string newPasswordRequired?: string incorrectCurrentPassword?: string passwordNotSet?: string reusedPassword?: string flowNotEnabled?: string } } interface WebAuthnFlowOptions { enabled: boolean expires: number name: string domain: string origin: string timeout?: number type: 'any' | 'platform' | 'cross-platform' credentialFields: { id: string userId: string publicKey: string transports: string counter: string } } export type UserType = Record export type DbAuthResponse = Promise<{ headers: { [x: string]: string | string[] } body?: string | undefined statusCode: number }> type AuthMethodOutput = [ string | Record | boolean | undefined, // body Headers?, { statusCode: number }?, ] export interface DbAuthHandlerOptions< TUser = UserType, TUserAttributes = Record, TDb extends object = Record, > { /** * Provide prisma db client * * Typed generically so that `authModelAccessor` and `credentialModelAccessor` * below are constrained to the model names that actually exist in the user's * schema, without importing @prisma/client. A real PrismaClient instance * always satisfies `Record`. */ db: TDb /** * The name of the property you'd call on `db` to access your user table. * ie. if your Prisma model is named `User` this value would be `user`, as in `db.user` */ authModelAccessor: keyof TDb /** * The name of the property you'd call on `db` to access your user credentials table. * ie. if your Prisma model is named `UserCredential` this value would be `userCredential`, as in `db.userCredential` */ credentialModelAccessor?: keyof TDb /** * The fields that are allowed to be returned from the user table when * invoking handlers that return a user object (like forgotPassword and signup) * Defaults to `id` and `email` if not set at all. */ allowedUserFields?: string[] /** * A map of what dbAuth calls a field to what your database calls it. * `id` is whatever column you use to uniquely identify a user (probably * something like `id` or `userId` or even `email`) */ authFields: { id: string username: string hashedPassword: string salt: string resetToken: string resetTokenExpiresAt: string challenge?: string } /** * Object containing change password options */ changePassword: ChangePasswordFlowOptions | { enabled: false } /** * Object containing cookie config options */ cookie?: DbAuthCookieConfig /** * Object containing forgot password options */ forgotPassword: ForgotPasswordFlowOptions | { enabled: false } /** * Object containing login options */ login: LoginFlowOptions | { enabled: false } /** * Object containing reset password options */ resetPassword: ResetPasswordFlowOptions | { enabled: false } /** * Object containing login options */ signup: SignupFlowOptions | { enabled: false } /** * Object containing WebAuthn options */ webAuthn?: WebAuthnFlowOptions | { enabled: false } /** * CORS settings, same as in createGraphqlHandler */ cors?: CorsConfig /** * Extra origins that are trusted for state-changing (non-GET) requests, * on top of the request's own host and any origins already listed in * `cors.origin`. * * Needed when the web side and API are on different origins and * `cors.origin` isn't set to those origins, or when `cors.origin` is set * to `true` -- reflecting any request origin is never treated as trust, * so that combination requires listing trusted origins here explicitly. */ trustedOrigins?: string | string[] } export interface SignupHandlerOptions { username: string hashedPassword: string salt: string userAttributes?: TUserAttributes } export type AuthMethodNames = | 'changePassword' | 'forgotPassword' | 'getToken' | 'login' | 'logout' | 'resetPassword' | 'signup' | 'webAuthnAuthenticate' | 'webAuthnAuthOptions' | 'webAuthnRegOptions' | 'webAuthnRegister' | 'validateResetToken' type Params = AuthenticationResponseJSON & RegistrationResponseJSON & { username?: string password?: string resetToken?: string currentPassword?: string newPassword?: string method: AuthMethodNames [key: string]: any } & { transports?: string // used by webAuthN for something } type DbAuthSession = Record type CorsHeaders = Record export class DbAuthHandler< TUser extends UserType, TUserAttributes = Record, TDb extends object = Record, > { event: Request | APIGatewayProxyEvent _normalizedRequest: PartialRequest | undefined httpMethod: string options: DbAuthHandlerOptions cookie: string db: TDb dbAccessor: any dbCredentialAccessor: any allowedUserFields: string[] hasInvalidSession: boolean session: DbAuthSession | undefined sessionCsrfToken: string | undefined corsContext: CorsContext | undefined sessionExpiresDate: string webAuthnExpiresDate: string encryptedSession: string | null = null createResponse: ( response: { body?: string statusCode: number headers?: Headers }, corsHeaders: CorsHeaders, ) => { headers: Record body?: string | undefined statusCode: number } public get normalizedRequest() { if (!this._normalizedRequest) { // This is a dev time error, no need to throw a specialized error throw new Error( 'dbAuthHandler has not been initialized. Either await ' + 'dbAuthHandler.invoke() or call await dbAuth.init()', ) } return this._normalizedRequest } // class constant: list of auth methods that are supported static get METHODS(): AuthMethodNames[] { return [ 'changePassword', 'forgotPassword', 'getToken', 'login', 'logout', 'resetPassword', 'signup', 'validateResetToken', 'webAuthnRegOptions', 'webAuthnRegister', 'webAuthnAuthOptions', 'webAuthnAuthenticate', ] } // class constant: maps the auth functions to their required HTTP verb for access static get VERBS() { return { changePassword: 'POST', forgotPassword: 'POST', getToken: 'GET', login: 'POST', logout: 'POST', resetPassword: 'POST', signup: 'POST', validateResetToken: 'POST', webAuthnRegOptions: 'GET', webAuthnRegister: 'POST', webAuthnAuthOptions: 'GET', webAuthnAuthenticate: 'POST', } } // default to epoch when we want to expire static get PAST_EXPIRES_DATE() { return new Date('1970-01-01T00:00:00.000+00:00').toUTCString() } // generate a new token (standard UUID) static get CSRF_TOKEN() { return createCsrfToken() } static get AVAILABLE_WEBAUTHN_TRANSPORTS() { return ['usb', 'ble', 'nfc', 'internal'] } /** * Returns the set-cookie header to mark the cookie as expired ("deletes" the session) * * The header keys are case insensitive, but Fastify prefers these to be lowercase. * Therefore, we want to ensure that the headers are always lowercase and unique * for compliance with HTTP/2. * * @see: https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2 */ get _deleteSessionHeader(): Headers { const deleteHeaders = new Headers() deleteHeaders.append( 'set-cookie', [ `${generateCookieName(this.options.cookie?.name)}=`, ...this._cookieAttributes({ expires: 'now' }), ].join(';'), ) deleteHeaders.append( 'set-cookie', [`auth-provider=`, ...this._cookieAttributes({ expires: 'now' })].join( ';', ), ) return deleteHeaders } constructor( event: APIGatewayProxyEvent | Request, _context: LambdaContext, // @TODO: options: DbAuthHandlerOptions, ) { this.options = options this.event = event this.httpMethod = isFetchApiRequest(event) ? event.method : event.httpMethod this.cookie = extractCookie(event) || '' this.createResponse = getDbAuthResponseBuilder(event) this._validateOptions() this.db = this.options.db this.dbAccessor = this.db[this.options.authModelAccessor] this.dbCredentialAccessor = this.options.credentialModelAccessor ? this.db[this.options.credentialModelAccessor] : null this.hasInvalidSession = false this.allowedUserFields = this.options.allowedUserFields || DEFAULT_ALLOWED_USER_FIELDS this.sessionExpiresDate = createExpiresAtDate( (this.options.login as LoginFlowOptions).expires, ) const webAuthnExpiresAt = new Date() webAuthnExpiresAt.setSeconds( webAuthnExpiresAt.getSeconds() + ((this.options?.webAuthn as WebAuthnFlowOptions)?.expires || 0), ) this.webAuthnExpiresDate = webAuthnExpiresAt.toUTCString() // Note that we handle these headers differently in functions/graphql.ts // because it's handled by graphql-yoga, so we map the cors config to yoga config // See packages/graphql-server/src/__tests__/mapRwCorsToYoga.test.ts if (options.cors) { this.corsContext = createCorsContext(options.cors) } try { this.encryptedSession = getSession(this.cookie, this.options.cookie?.name) const [session, csrfToken] = decryptSession(this.encryptedSession) this.session = session this.sessionCsrfToken = csrfToken } catch (e) { // if session can't be decrypted, keep track so we can log them out when // the auth method is called if (e instanceof DbAuthError.SessionDecryptionError) { this.hasInvalidSession = true } else { throw e } } } // Initialize the request object. This is async now, because body in Fetch Request // is parsed async async init() { if (!this._normalizedRequest) { this._normalizedRequest = (await normalizeRequest( this.event, )) as PartialRequest } } // Actual function that triggers everything else to happen: `login`, `signup`, // etc. is called from here, after some checks to make sure the request is good async invoke() { let corsHeaders = {} await this.init() if (this.corsContext) { corsHeaders = this.corsContext.getRequestHeaders(this.normalizedRequest) // Return CORS headers for OPTIONS requests if (this.corsContext.shouldHandleCors(this.normalizedRequest)) { return this.createResponse({ body: '', statusCode: 200 }, corsHeaders) } } // if there was a problem decryption the session, just return the logout // response immediately if (this.hasInvalidSession) { return this.createResponse( this._ok(...this._logoutResponse()), corsHeaders, ) } try { const method = await this._getAuthMethod() // get the auth method the incoming request is trying to call if (!DbAuthHandler.METHODS.includes(method)) { return this.createResponse(this._notFound(), corsHeaders) } // make sure it's using the correct verb, GET vs POST if (this.httpMethod !== DbAuthHandler.VERBS[method]) { return this.createResponse(this._notFound(), corsHeaders) } // state-changing (non-GET) requests must come from a trusted Origin. // `SameSite` cookies alone don't protect apps that relax that // attribute, or bodies delivered via a content type that doesn't // trigger a CORS preflight if ( requiresOriginValidation(this.httpMethod) && !isRequestOriginTrusted( { method: this.httpMethod, headers: this.normalizedRequest.headers, host: this._requestHost(), protocol: this._requestProtocol(), proxied: isProxiedRequest(this.normalizedRequest.headers), }, { trustedOrigins: this.options.trustedOrigins, corsOrigin: this.options.cors?.origin, }, ) ) { throw new DbAuthError.UntrustedOriginError() } // call whatever auth method was requested and return the body and headers const [body, headers, options = { statusCode: 200 }] = await this[method]() return this.createResponse(this._ok(body, headers, options), corsHeaders) } catch (e: any) { if (e instanceof DbAuthError.WrongVerbError) { return this.createResponse(this._notFound(), corsHeaders) } else if (e instanceof DbAuthError.UntrustedOriginError) { return this.createResponse(this._forbidden(e.message), corsHeaders) } else { return this.createResponse( this._badRequest(e.message || e), corsHeaders, ) } } } // best-effort host the request was sent to, used by origin validation to // allow same-origin requests through with no extra configuration. Prefers // `X-Forwarded-Host` over the connection-level host/URL -- see // `resolveRequestHost` for why that's safe behind a proxy _requestHost(): string | null { return resolveRequestHost( this.normalizedRequest.headers, isFetchApiRequest(this.event) ? this.event.url : undefined, ) } // best-effort scheme the request arrived over, used alongside // `_requestHost` for origin validation. See `resolveRequestProtocol` for // when it can and can't be determined reliably _requestProtocol(): string | null { return resolveRequestProtocol( this.normalizedRequest.headers, isFetchApiRequest(this.event) ? this.event.url : undefined, ) } async changePassword(): Promise { // Read as the non-generic options shape, like the other flows do, because // the handler is called with a sanitized user rather than a full `TUser`. // The `{ enabled: false }` variant is handled by the check right below, and // so is a missing option, which only JavaScript apps can end up with const options = this.options.changePassword as ChangePasswordFlowOptions | undefined if (!options || options.enabled === false) { throw new DbAuthError.FlowNotEnabledError( options?.errors?.flowNotEnabled || `Change password flow is not enabled`, ) } const sessionUserId = this.session?.[this.options.authFields.id] if (!sessionUserId) { throw new DbAuthError.NotLoggedInError() } const { currentPassword, newPassword } = this.normalizedRequest.jsonBody || {} if (currentPassword == null || String(currentPassword).trim() === '') { throw new DbAuthError.PasswordRequiredError( options.errors?.currentPasswordRequired || 'Current password is required', ) } if (newPassword == null || String(newPassword).trim() === '') { throw new DbAuthError.PasswordRequiredError( options.errors?.newPasswordRequired || 'New password is required', ) } // check if the new password is valid using signup criteria ;(this.options.signup as SignupFlowOptions).passwordValidation?.( newPassword, ) let user try { user = await this.dbAccessor.findUnique({ where: { [this.options.authFields.id]: sessionUserId }, }) } catch { throw new DbAuthError.GenericError() } if (!user) { throw new DbAuthError.UserNotFoundError() } // Accounts created through OAuth or WebAuthn can exist without a password if (!user[this.options.authFields.hashedPassword]) { throw new DbAuthError.PasswordNotSetError(options.errors?.passwordNotSet) } try { await this._verifyPassword(user, currentPassword) } catch (e) { if (e instanceof DbAuthError.IncorrectPasswordError) { throw new DbAuthError.IncorrectPasswordError( user[this.options.authFields.username], options.errors?.incorrectCurrentPassword || 'Current password is incorrect', ) } throw e } if ( !options.allowReusedPassword && this._passwordMatches(user, newPassword) ) { throw new DbAuthError.ReusedPasswordError(options.errors?.reusedPassword) } const [hashedPassword, salt] = hashPassword(newPassword) try { // A reset token that is still outstanding would let whoever holds it // replace the password that was just chosen, so it is cleared here too user = await this.dbAccessor.update({ where: { [this.options.authFields.id]: user[this.options.authFields.id], }, data: { [this.options.authFields.hashedPassword]: hashedPassword, [this.options.authFields.salt]: salt, [this.options.authFields.resetToken]: null, [this.options.authFields.resetTokenExpiresAt]: null, }, }) } catch { throw new DbAuthError.GenericError() } // call the user-defined handler so they can decide what to do with this user const response = await options.handler(this._sanitizeUser(user)) // returning anything truthy from the handler keeps the user logged in, with // a freshly issued session cookie if (response) { return this._loginResponse(user) } else { return this._logoutResponse({}) } } async forgotPassword(): Promise { const { enabled = true } = this.options.forgotPassword if (!enabled) { throw new DbAuthError.FlowNotEnabledError( (this.options.forgotPassword as ForgotPasswordFlowOptions)?.errors ?.flowNotEnabled || `Forgot password flow is not enabled`, ) } const { username } = this.normalizedRequest.jsonBody || {} // was the username sent in at all? if (!username || username.trim() === '') { throw new DbAuthError.UsernameRequiredError( (this.options.forgotPassword as ForgotPasswordFlowOptions)?.errors ?.usernameRequired || `Username is required`, ) } let user try { user = await this.dbAccessor.findUnique({ where: { [this.options.authFields.username]: username }, }) } catch { throw new DbAuthError.GenericError() } if (user) { const tokenExpires = new Date() tokenExpires.setSeconds( tokenExpires.getSeconds() + (this.options.forgotPassword as ForgotPasswordFlowOptions).expires, ) // generate a token let token = md5(uuidv4()) const buffer = Buffer.from(token) token = buffer.toString('base64').replace('=', '').substring(0, 16) // Store the token hash in the database so we can verify it later const tokenHash = hashToken(token) try { // set token and expires time user = await this.dbAccessor.update({ where: { [this.options.authFields.id]: user[this.options.authFields.id], }, data: { [this.options.authFields.resetToken]: tokenHash, [this.options.authFields.resetTokenExpiresAt]: tokenExpires, }, }) } catch { throw new DbAuthError.GenericError() } // call user-defined handler in their functions/auth.js const response = await ( this.options.forgotPassword as ForgotPasswordFlowOptions ).handler(this._sanitizeUser(user), token) return [ response ? JSON.stringify(response) : '', this._deleteSessionHeader, ] } else { throw new DbAuthError.UsernameNotFoundError( (this.options.forgotPassword as ForgotPasswordFlowOptions)?.errors ?.usernameNotFound || `Username '${username} not found`, ) } } async getToken(): Promise { try { const user = await this._getCurrentUser() let headers = new Headers() // if the session was encrypted with the old algorithm, re-encrypt it // with the new one if (isLegacySession(this.cookie)) { headers = this._loginResponse(user)[1] } return [user[this.options.authFields.id], headers] } catch (e: any) { if (e instanceof DbAuthError.NotLoggedInError) { return this._logoutResponse() } else { return this._logoutResponse({ error: e.message }) } } } async login(): Promise { const { enabled = true } = this.options.login if (!enabled) { throw new DbAuthError.FlowNotEnabledError( (this.options.login as LoginFlowOptions)?.errors?.flowNotEnabled || `Login flow is not enabled`, ) } const { username, password } = this.normalizedRequest.jsonBody || {} const dbUser = await this._verifyUser(username, password) const handlerUser = await (this.options.login as LoginFlowOptions).handler( dbUser, ) if (handlerUser?.[this.options.authFields.id] == null) { throw new DbAuthError.NoUserIdError() } return this._loginResponse(handlerUser) } logout(): AuthMethodOutput { return this._logoutResponse() } async resetPassword(): Promise { const { enabled = true } = this.options.resetPassword if (!enabled) { throw new DbAuthError.FlowNotEnabledError( (this.options.resetPassword as ResetPasswordFlowOptions)?.errors ?.flowNotEnabled || `Reset password flow is not enabled`, ) } const { password, resetToken } = this.normalizedRequest.jsonBody || {} // is the resetToken present? if (resetToken == null || String(resetToken).trim() === '') { throw new DbAuthError.ResetTokenRequiredError( (this.options.resetPassword as ResetPasswordFlowOptions)?.errors ?.resetTokenRequired, ) } // is password present? if (password == null || String(password).trim() === '') { throw new DbAuthError.PasswordRequiredError() } // check if password is valid using signup criteria ;(this.options.signup as SignupFlowOptions).passwordValidation?.(password) let user = await this._findUserByToken(resetToken) const [hashedPassword] = hashPassword(password, { salt: user.salt, }) const [legacyHashedPassword] = legacyHashPassword(password, user.salt) if ( (!(this.options.resetPassword as ResetPasswordFlowOptions) .allowReusedPassword && user.hashedPassword === hashedPassword) || user.hashedPassword === legacyHashedPassword ) { throw new DbAuthError.ReusedPasswordError( (this.options.resetPassword as ResetPasswordFlowOptions)?.errors ?.reusedPassword, ) } try { // if we got here then we can update the password in the database user = await this.dbAccessor.update({ where: { [this.options.authFields.id]: user[this.options.authFields.id], }, data: { [this.options.authFields.hashedPassword]: hashedPassword, }, }) } catch { throw new DbAuthError.GenericError() } await this._clearResetToken(user) // call the user-defined handler so they can decide what to do with this user const response = await ( this.options.resetPassword as ResetPasswordFlowOptions ).handler(this._sanitizeUser(user)) // returning the user from the handler means to log them in automatically if (response) { return this._loginResponse(user) } else { return this._logoutResponse({}) } } async signup(): Promise { const { enabled = true } = this.options.signup if (!enabled) { throw new DbAuthError.FlowNotEnabledError( (this.options.signup as SignupFlowOptions)?.errors?.flowNotEnabled || `Signup flow is not enabled`, ) } // check if password is valid const { password } = this.normalizedRequest.jsonBody || {} ;(this.options.signup as SignupFlowOptions).passwordValidation?.( password as string, ) const userOrMessage = await this._createUser() // at this point `user` is either an actual user, in which case log the // user in automatically, or it's a string, which is a message to show // the user (something like "please verify your email") if (typeof userOrMessage === 'object') { const user = userOrMessage return this._loginResponse(user, 201) } else { const message = userOrMessage return [JSON.stringify({ message }), new Headers(), { statusCode: 201 }] } } async validateResetToken(): Promise { const { resetToken } = this.normalizedRequest.jsonBody || {} // is token present at all? if (!resetToken || String(resetToken).trim() === '') { throw new DbAuthError.ResetTokenRequiredError( (this.options.resetPassword as ResetPasswordFlowOptions)?.errors ?.resetTokenRequired, ) } const user = await this._findUserByToken(resetToken) return [JSON.stringify(this._sanitizeUser(user)), this._deleteSessionHeader] } // browser submits WebAuthn credentials async webAuthnAuthenticate(): Promise { const { verifyAuthenticationResponse } = await import('@simplewebauthn/server') const webAuthnOptions = this.options.webAuthn const { rawId } = this.normalizedRequest.jsonBody || {} if (!rawId) { throw new DbAuthError.WebAuthnError('Missing Id in request') } if (!webAuthnOptions?.enabled) { throw new DbAuthError.WebAuthnError('WebAuthn is not enabled') } const credential = await this.dbCredentialAccessor.findFirst({ where: { id: rawId }, }) if (!credential) { throw new DbAuthError.WebAuthnError('Credentials not found') } const user = await this.dbAccessor.findFirst({ where: { [this.options.authFields.id]: credential[webAuthnOptions.credentialFields.userId], }, }) let verification: VerifiedAuthenticationResponse try { const opts: VerifyAuthenticationResponseOpts = { response: this.normalizedRequest?.jsonBody, // by this point jsonBody has been validated expectedChallenge: user[this.options.authFields.challenge as string], expectedOrigin: webAuthnOptions.origin, expectedRPID: webAuthnOptions.domain, credential: { id: credential[webAuthnOptions.credentialFields.id], publicKey: credential[webAuthnOptions.credentialFields.publicKey], counter: credential[webAuthnOptions.credentialFields.counter], transports: credential[webAuthnOptions.credentialFields.transports] ? JSON.parse( credential[webAuthnOptions.credentialFields.transports], ) : DbAuthHandler.AVAILABLE_WEBAUTHN_TRANSPORTS, }, requireUserVerification: true, } verification = await verifyAuthenticationResponse(opts) } catch (e: any) { throw new DbAuthError.WebAuthnError(e.message) } finally { // whether it worked or errored, clear the challenge in the user record // and user can get a new one next time they try to authenticate await this._saveChallenge(user[this.options.authFields.id], null) } const { verified, authenticationInfo } = verification // `verifyAuthenticationResponse()` throws for most failure modes, but a // response whose signature simply does not match the stored public key // comes back as `verified: false` with no error. Bail out before issuing // any cookies — otherwise an unverified assertion is answered with a // valid session for the credential's owner. if (!verified) { throw new DbAuthError.WebAuthnError('Authentication failed') } // update counter in credentials await this.dbCredentialAccessor.update({ where: { [webAuthnOptions.credentialFields.id]: credential[webAuthnOptions.credentialFields.id], }, data: { [webAuthnOptions.credentialFields.counter]: authenticationInfo.newCounter, }, }) // get the regular `login` cookies const [, headers] = this._loginResponse(user) // Now add the webAuthN cookies headers.append( 'set-cookie', this._webAuthnCookie(rawId, this.webAuthnExpiresDate), ) return [verified, headers] } // get options for a WebAuthn authentication async webAuthnAuthOptions(): Promise { const { generateAuthenticationOptions } = await import('@simplewebauthn/server') if (!this.options.webAuthn?.enabled) { throw new DbAuthError.WebAuthnError('WebAuthn is not enabled') } const webAuthnOptions = this.options.webAuthn const credentialId = webAuthnSession(this.event) let user if (credentialId) { const credential = await this.dbCredentialAccessor.findUnique({ where: { [webAuthnOptions.credentialFields.id]: credentialId }, include: { [this.options.authModelAccessor]: true }, }) user = credential[this.options.authModelAccessor] } else { // webauthn session not present, fallback to getting user from regular // session cookie user = await this._getCurrentUser() } // webauthn cookie has been tampered with or UserCredential has been deleted // from the DB, remove their cookie so it doesn't happen again if (!user) { return [ { error: 'Log in with username and password to enable WebAuthn' }, new Headers([['set-cookie', this._webAuthnCookie('', 'now')]]), { statusCode: 400 }, ] } const credentials = await this.dbCredentialAccessor.findMany({ where: { [webAuthnOptions.credentialFields.userId]: user[this.options.authFields.id], }, }) const someOptions: GenerateAuthenticationOptionsOpts = { timeout: webAuthnOptions.timeout || 60000, allowCredentials: credentials.map((cred: Record) => ({ id: cred[webAuthnOptions.credentialFields.id], transports: cred[webAuthnOptions.credentialFields.transports] ? JSON.parse(cred[webAuthnOptions.credentialFields.transports]) : DbAuthHandler.AVAILABLE_WEBAUTHN_TRANSPORTS, })), userVerification: 'required', rpID: webAuthnOptions.domain, } const authOptions = await generateAuthenticationOptions(someOptions) await this._saveChallenge( user[this.options.authFields.id], authOptions.challenge, ) return [authOptions] } // get options for WebAuthn registration async webAuthnRegOptions(): Promise { const { generateRegistrationOptions } = await import('@simplewebauthn/server') const { isoUint8Array } = await import('@simplewebauthn/server/helpers') if (!this.options?.webAuthn?.enabled) { throw new DbAuthError.WebAuthnError('WebAuthn is not enabled') } const webAuthnOptions = this.options.webAuthn const user = await this._getCurrentUser() const options: GenerateRegistrationOptionsOpts = { rpName: webAuthnOptions.name, rpID: webAuthnOptions.domain, userID: isoUint8Array.fromUTF8String( String(user[this.options.authFields.id]), ), userName: user[this.options.authFields.username], userDisplayName: user[this.options.authFields.username], timeout: webAuthnOptions?.timeout || 60000, excludeCredentials: [], authenticatorSelection: { userVerification: 'required', }, // Support the two most common algorithms: ES256, and RS256 supportedAlgorithmIDs: [-7, -257], } // if a type is specified other than `any` assign it (the default behavior // of this prop if `undefined` means to allow any authenticator) if (webAuthnOptions.type && webAuthnOptions.type !== 'any') { options.authenticatorSelection = Object.assign( options.authenticatorSelection || {}, { authenticatorAttachment: webAuthnOptions.type }, ) } const regOptions = await generateRegistrationOptions(options) await this._saveChallenge( user[this.options.authFields.id], regOptions.challenge, ) return [regOptions] } // browser submits WebAuthn credentials for the first time on a new device async webAuthnRegister(): Promise { const { verifyRegistrationResponse } = await import('@simplewebauthn/server') if (!this.options.webAuthn?.enabled) { throw new DbAuthError.WebAuthnError('WebAuthn is not enabled') } const user = await this._getCurrentUser() let verification: VerifiedRegistrationResponse try { const options: VerifyRegistrationResponseOpts = { response: this.normalizedRequest.jsonBody, // by this point jsonBody has been validated expectedChallenge: user[this.options.authFields.challenge as string], expectedOrigin: this.options.webAuthn.origin, expectedRPID: this.options.webAuthn.domain, requireUserVerification: true, } verification = await verifyRegistrationResponse(options) } catch (e: any) { throw new DbAuthError.WebAuthnError(e.message) } const { verified, registrationInfo } = verification if (!verified || !registrationInfo) { throw new DbAuthError.WebAuthnError('Registration failed') } const { id: credentialID, publicKey: credentialPublicKey, counter, } = registrationInfo.credential const existingDevice = await this.dbCredentialAccessor.findFirst({ where: { [this.options.webAuthn.credentialFields.id]: credentialID, [this.options.webAuthn.credentialFields.userId]: user[this.options.authFields.id], }, }) if (!existingDevice) { const { transports } = this.normalizedRequest.jsonBody || {} await this.dbCredentialAccessor.create({ data: { [this.options.webAuthn.credentialFields.id]: credentialID, [this.options.webAuthn.credentialFields.userId]: user[this.options.authFields.id], [this.options.webAuthn.credentialFields.publicKey]: credentialPublicKey, [this.options.webAuthn.credentialFields.transports]: transports ? JSON.stringify(transports) : null, [this.options.webAuthn.credentialFields.counter]: counter, }, }) } // clear challenge await this._saveChallenge(user[this.options.authFields.id], null) const headers = new Headers([ [ 'set-cookie', this._webAuthnCookie(credentialID, this.webAuthnExpiresDate), ], ]) return [verified, headers] } // validates that we have all the ENV and options we need to login/signup _validateOptions() { // must have a SESSION_SECRET so we can encrypt/decrypt the cookie if (!process.env.SESSION_SECRET) { throw new DbAuthError.NoSessionSecretError() } // must have an expiration time set for the session cookie if ( this.options?.login?.enabled !== false && !this.options?.login?.expires ) { throw new DbAuthError.NoSessionExpirationError() } // must have a login handler to actually log a user in if ( this.options?.login?.enabled !== false && !this.options?.login?.handler ) { throw new DbAuthError.NoLoginHandlerError() } // must have a signup handler to define how to create a new user if ( this.options?.signup?.enabled !== false && !this.options?.signup?.handler ) { throw new DbAuthError.NoSignupHandlerError() } // must have a forgot password handler to define how to notify user of reset token if ( this.options?.forgotPassword?.enabled !== false && !this.options?.forgotPassword?.handler ) { throw new DbAuthError.NoForgotPasswordHandlerError() } // must have a reset password handler to define what to do with user once password changed if ( this.options?.resetPassword?.enabled !== false && !this.options?.resetPassword?.handler ) { throw new DbAuthError.NoResetPasswordHandlerError() } // must have a change password handler to define what to do with user once // password changed if ( this.options?.changePassword && this.options.changePassword.enabled !== false && !this.options.changePassword.handler ) { throw new DbAuthError.NoChangePasswordHandlerError() } // must have webAuthn config if credentialModelAccessor present and vice versa if ( (this.options?.credentialModelAccessor && !this.options?.webAuthn) || (this.options?.webAuthn && !this.options?.credentialModelAccessor) ) { throw new DbAuthError.NoWebAuthnConfigError() } if ( this.options?.webAuthn?.enabled && (!this.options?.webAuthn?.name || !this.options?.webAuthn?.domain || !this.options?.webAuthn?.origin || !this.options?.webAuthn?.credentialFields) ) { throw new DbAuthError.MissingWebAuthnConfigError() } } // Save challenge string for WebAuthn async _saveChallenge(userId: string | number, value: string | null) { await this.dbAccessor.update({ where: { [this.options.authFields.id]: userId, }, data: { [this.options.authFields.challenge as string]: value, }, }) } // returns the string for the webAuthn set-cookie header _webAuthnCookie(id: string, expires: string) { return [ `webAuthn=${id}`, ...this._cookieAttributes({ expires, options: { HttpOnly: false }, }), ].join(';') } // removes any fields not explicitly allowed to be sent to the client before // sending a response over the wire _sanitizeUser(user: Record) { return sanitizeUser(user, this.allowedUserFields) } // Converts LambdaEvent or FetchRequest to _decodeEvent() {} // returns all the cookie attributes in an array with the proper expiration date // // pass the argument `expires` set to "now" to get the attributes needed to expire // the session, or "future" (or left out completely) to set to `futureExpiresDate` _cookieAttributes({ expires = 'now', options = {}, }: { expires?: 'now' | string options?: DbAuthHandlerOptions['cookie'] }) { return buildCookieAttributes({ cookieConfig: this.options.cookie, expires, overrideAttributes: options, }) } _createAuthProviderCookieString(): string { return createAuthProviderCookieString({ cookieConfig: this.options.cookie, expiresAt: this.sessionExpiresDate, }) } // returns the set-cookie header to be returned in the request (effectively // creates the session) _createSessionCookieString( data: DbAuthSession, csrfToken: string, ): string { return createSessionCookieString({ data, csrfToken, cookieConfig: this.options.cookie, expiresAt: this.sessionExpiresDate, }) } async _findUserByToken(token: string) { const tokenExpires = new Date() tokenExpires.setSeconds( tokenExpires.getSeconds() - (this.options.forgotPassword as ForgotPasswordFlowOptions).expires, ) const tokenHash = hashToken(token) const user = await this.dbAccessor.findFirst({ where: { [this.options.authFields.resetToken]: tokenHash, }, }) // user not found with the given token if (!user) { throw new DbAuthError.ResetTokenInvalidError( (this.options.resetPassword as ResetPasswordFlowOptions)?.errors ?.resetTokenInvalid, ) } // token has expired if (user[this.options.authFields.resetTokenExpiresAt] < tokenExpires) { await this._clearResetToken(user) throw new DbAuthError.ResetTokenExpiredError( (this.options.resetPassword as ResetPasswordFlowOptions)?.errors ?.resetTokenExpired, ) } return user } // removes the resetToken from the database async _clearResetToken(user: Record) { try { await this.dbAccessor.update({ where: { [this.options.authFields.id]: user[this.options.authFields.id], }, data: { [this.options.authFields.resetToken]: null, [this.options.authFields.resetTokenExpiresAt]: null, }, }) } catch { throw new DbAuthError.GenericError() } } // verifies that a username and password are correct, and returns the user if so async _verifyUser( username: string | undefined, password: string | undefined, ) { // do we have all the query params we need to check the user? if ( !username || username.toString().trim() === '' || !password || password.toString().trim() === '' ) { throw new DbAuthError.UsernameAndPasswordRequiredError( (this.options.login as LoginFlowOptions)?.errors ?.usernameOrPasswordMissing, ) } const usernameMatchFlowOption = (this.options.login as LoginFlowOptions) ?.usernameMatch const findUniqueUserMatchCriteriaOptions = this._getUserMatchCriteriaOptions(username, usernameMatchFlowOption) let user try { // does user exist? user = await this.dbAccessor.findFirst({ where: findUniqueUserMatchCriteriaOptions, }) } catch { throw new DbAuthError.GenericError() } if (!user) { throw new DbAuthError.UserNotFoundError( username, (this.options.login as LoginFlowOptions)?.errors?.usernameNotFound, ) } await this._verifyPassword(user, password) return user } // extracts scrypt strength options from hashed password (if present) and // compares the hashed plain text password just submitted using those options // with the one in the database. Falls back to the legacy CryptoJS algorihtm // if no options are present. async _verifyPassword(user: Record, password: string) { const options = extractHashingOptions( user[this.options.authFields.hashedPassword] as string, ) if (Object.keys(options).length) { // hashed using the node:crypto algorithm const [hashedPassword] = hashPassword(password, { salt: user[this.options.authFields.salt] as string, options, }) if (hashedPassword === user[this.options.authFields.hashedPassword]) { return user } } else { // fallback to old CryptoJS hashing const [legacyHashedPassword] = legacyHashPassword( password, user[this.options.authFields.salt] as string, ) if ( legacyHashedPassword === user[this.options.authFields.hashedPassword] ) { const [newHashedPassword] = hashPassword(password, { salt: user[this.options.authFields.salt] as string, }) // update user's hash to the new algorithm await this.dbAccessor.update({ where: { id: user.id }, data: { [this.options.authFields.hashedPassword]: newHashedPassword }, }) return user } } throw new DbAuthError.IncorrectPasswordError( user[this.options.authFields.username] as string, (this.options.login as LoginFlowOptions)?.errors?.incorrectPassword, ) } // compares a plain text password with the user's stored hash, using the // same algorithm and options the stored hash was created with. Unlike // `_verifyPassword` this has no side effects and does not throw _passwordMatches(user: Record, password: string) { const storedHash = user[this.options.authFields.hashedPassword] as string const salt = user[this.options.authFields.salt] as string const options = extractHashingOptions(storedHash) const [hashedPassword] = Object.keys(options).length ? hashPassword(password, { salt, options }) : legacyHashPassword(password, salt) return hashedPassword === storedHash } // gets the user from the database and returns only its ID async _getCurrentUser() { if (!this.session?.[this.options.authFields.id]) { throw new DbAuthError.NotLoggedInError() } const select = { [this.options.authFields.id]: true, [this.options.authFields.username]: true, } if (this.options.webAuthn?.enabled && this.options.authFields.challenge) { select[this.options.authFields.challenge] = true } let user try { user = await this.dbAccessor.findUnique({ where: { [this.options.authFields.id]: this.session?.[this.options.authFields.id], }, select, }) } catch (e: any) { throw new DbAuthError.GenericError(e.message) } if (!user) { throw new DbAuthError.UserNotFoundError() } return user } // creates and returns a user, first checking that the username/password // values pass validation async _createUser() { const { username, password, ...userAttributes } = this.normalizedRequest.jsonBody || {} if ( this._validateField('username', username) && this._validateField('password', password) ) { const usernameMatchFlowOption = (this.options.signup as SignupFlowOptions) ?.usernameMatch const findUniqueUserMatchCriteriaOptions = this._getUserMatchCriteriaOptions(username, usernameMatchFlowOption) const user = await this.dbAccessor.findFirst({ where: findUniqueUserMatchCriteriaOptions, }) if (user) { throw new DbAuthError.DuplicateUsernameError( username, (this.options.signup as SignupFlowOptions)?.errors?.usernameTaken, ) } // if we get here everything is good, call the app's signup handler const [hashedPassword, salt] = hashPassword(password) const newUser = await (this.options.signup as SignupFlowOptions).handler({ username, hashedPassword, salt, userAttributes, }) return newUser } } // figure out which auth method we're trying to call async _getAuthMethod() { // try getting it from the query string, //auth?method=[methodName] let methodName = this.normalizedRequest.query?.method as AuthMethodNames if ( !DbAuthHandler.METHODS.includes(methodName) && this.normalizedRequest.jsonBody ) { // try getting it from the body in JSON: { method: [methodName] } try { methodName = this.normalizedRequest.jsonBody.method } catch { // there's no body, or it's not JSON, `handler` will return a 404 } } return methodName } // checks that a single field meets validation requirements // currently checks for presence only _validateField(name: string, value: string | undefined): value is string { // check for presence if (!value || value.trim() === '') { throw new DbAuthError.FieldRequiredError( name, (this.options.signup as SignupFlowOptions)?.errors?.fieldMissing, ) } else { return true } } _loginResponse( user: Record, statusCode = 200, ): [Record, Headers, { statusCode: number }] { return createLoginResponse(user, { cookie: this.options.cookie, allowedUserFields: this.allowedUserFields, expiresAt: this.sessionExpiresDate, statusCode, }) } _logoutResponse(response?: Record): AuthMethodOutput { return [response ? JSON.stringify(response) : '', this._deleteSessionHeader] } _ok( body: string | boolean | undefined | Record, headers = new Headers(), options = { statusCode: 200 }, ) { headers.append('content-type', 'application/json') return { statusCode: options.statusCode, body: typeof body === 'string' ? body : JSON.stringify(body), headers, } } _notFound() { return { statusCode: 404, } } _badRequest(message: string) { return { statusCode: 400, body: JSON.stringify({ error: message }), headers: new Headers({ 'content-type': 'application/json' }), } } _forbidden(message: string) { return { statusCode: 403, body: JSON.stringify({ error: message }), headers: new Headers({ 'content-type': 'application/json' }), } } _getUserMatchCriteriaOptions( username: string, usernameMatchFlowOption: string | undefined, ) { // Each db provider has it owns rules for case insensitive comparison. // We are checking if you have defined one for your db choice here // https://www.prisma.io/docs/concepts/components/prisma-client/case-sensitivity const findUniqueUserMatchCriteriaOptions = !usernameMatchFlowOption ? { [this.options.authFields.username]: username } : { [this.options.authFields.username]: { equals: username, mode: usernameMatchFlowOption, }, } return findUniqueUserMatchCriteriaOptions } }