-
Notifications
You must be signed in to change notification settings - Fork 0
feat: require key-value adapter for storing generated passkey challange #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yaroslav8765
wants to merge
3
commits into
main
Choose a base branch
from
feature/AdminForth/1139/question-in-current-implementa
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
52d97ab
feat: require key-value adapter for storing generated passkey challange
yaroslav8765 d6da1df
fix: update challenge handling to have blacklist of used challenges
yaroslav8765 2ce581c
fix!: force user to use KeyValueAdapter for the improved sequrity
yaroslav8765 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { AdminForthPlugin, Filters, suggestIfTypo, HttpExtra } from "adminforth"; | ||
| import { AdminForthPlugin, Filters, suggestIfTypo, HttpExtra, convertPeriodToSeconds } from "adminforth"; | ||
| import type { AdminForthResource, AdminUser, IAdminForth, IHttpServer, IAdminForthAuth, BeforeLoginConfirmationFunction, IAdminForthHttpResponse } from "adminforth"; | ||
| import twofactor from 'node-2fa'; | ||
| import { PluginOptions } from "./types.js" | ||
|
|
@@ -29,6 +29,34 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { | |
| return `single`; | ||
| } | ||
|
|
||
| private saveChallengeToStorage(challenge: string, expiresIn?: string): void { | ||
| const expiresInSeconds = expiresIn ? convertPeriodToSeconds(expiresIn) : undefined; | ||
| this.options.passkeys.keyValueAdapter.set(challenge, 'stub_value', expiresInSeconds); | ||
| } | ||
|
|
||
| private async checkIfChallengeInBlackList(challenge: string): Promise<boolean> { | ||
| const res = await this.options.passkeys.keyValueAdapter.get(challenge); | ||
| if (!res) { | ||
| this.saveChallengeToStorage(challenge, this.options.passkeys?.challengeValidityPeriod || '2m'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yaroslav8765 "check" functions should not mutate state, this does not sound good, 100% better reading would be checkIfChellengeNotUsed() and separate method useChellenge() |
||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private async validateCookiesForPasskeyLogin(cookies: any): Promise<{ok: boolean, decodedPasskeysCookies?: any, error?: string}> { | ||
| const passkeysCookies = this.adminforth.auth.getCustomCookie({cookies: cookies, name: `passkeyLoginTemporaryJWT`}); | ||
| if (!passkeysCookies) { | ||
| return { ok: false, error: 'Passkey token is required' }; | ||
| } | ||
|
|
||
| const decodedPasskeysCookies = await this.adminforth.auth.verify(passkeysCookies, 'tempLoginPasskeyChallenge', false); | ||
| const isChallangeValid = await this.checkIfChallengeInBlackList(decodedPasskeysCookies.challenge); | ||
|
|
||
| if (!decodedPasskeysCookies || !isChallangeValid) { | ||
| return { ok: false, error: 'Invalid passkey' }; | ||
| } | ||
| return { ok: true, decodedPasskeysCookies }; | ||
| } | ||
|
|
||
| public async checkIfSkipSetupAllowSkipVerify(adminUser: AdminUser): Promise<{ skipAllowed: boolean }> { | ||
| if (this.options.usersFilterToAllowSkipSetup) { | ||
|
|
@@ -171,18 +199,11 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { | |
| return { error: "Wrong or expired OTP code" } | ||
| }; | ||
| } else if (confirmationResult.mode === "passkey") { | ||
| //TODO: fix ts-ignore after releasing new version of adminforth with updated types | ||
| //@ts-ignore | ||
| const passkeysCookies = this.adminforth.auth.getCustomCookie({cookies: cookies, name: `passkeyLoginTemporaryJWT`}); | ||
| if (!passkeysCookies) { | ||
| return { error: 'Passkey token is required' }; | ||
| } | ||
|
|
||
| const decodedPasskeysCookies = await this.adminforth.auth.verify(passkeysCookies, 'tempLoginPasskeyChallenge', false); | ||
| if (!decodedPasskeysCookies) { | ||
| return { error: 'Invalid passkey' }; | ||
| const cookiesValidationResult = await this.validateCookiesForPasskeyLogin(cookies); | ||
| if (!cookiesValidationResult.ok) { | ||
| return { error: cookiesValidationResult.error }; | ||
| } | ||
| const verificationResult = await this.verifyPasskeyResponse(confirmationResult.result, opts.userPk, decodedPasskeysCookies ); | ||
| const verificationResult = await this.verifyPasskeyResponse(confirmationResult.result, opts.userPk, cookiesValidationResult.decodedPasskeysCookies ); | ||
|
|
||
| if (verificationResult.ok && verificationResult.passkeyConfirmed) { | ||
|
|
||
|
|
@@ -340,6 +361,10 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { | |
| throw new Error('Passkeys credentialIdFieldName is required'); | ||
| } | ||
|
|
||
| if (!this.options.passkeys.keyValueAdapter) { | ||
| throw new Error('Passkeys keyValueAdapter is required'); | ||
| } | ||
|
|
||
| const credentialResource = adminforth.config.resources.find(r => r.resourceId === this.options.passkeys.credentialResourceID); | ||
| const credentialIDField = credentialResource.columns.find(c => c.name === this.options.passkeys.credentialIdFieldName); | ||
| if ( !credentialIDField ) { | ||
|
|
@@ -529,15 +554,11 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { | |
| let verified = null; | ||
| if (body.usePasskey && this.options.passkeys) { | ||
| // passkeys are enabled and user wants to use them | ||
| const passkeysCookies = this.adminforth.auth.getCustomCookie({cookies: cookies, name: `passkeyLoginTemporaryJWT`}); | ||
| if (!passkeysCookies) { | ||
| return { error: 'Passkey token is required' }; | ||
| const cookiesValidationResult = await this.validateCookiesForPasskeyLogin(cookies); | ||
ivictbor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!cookiesValidationResult.ok) { | ||
| return { error: cookiesValidationResult.error }; | ||
| } | ||
| const decodedPasskeysCookies = await this.adminforth.auth.verify(passkeysCookies, 'tempLoginPasskeyChallenge', false); | ||
| if (!decodedPasskeysCookies) { | ||
| return { error: 'Invalid passkey' }; | ||
| } | ||
| const res = await this.verifyPasskeyResponse(body.passkeyOptions, decoded.pk, decodedPasskeysCookies); | ||
| const res = await this.verifyPasskeyResponse(body.passkeyOptions, decoded.pk, cookiesValidationResult.decodedPasskeysCookies); | ||
| if (res.ok && res.passkeyConfirmed) { | ||
| verified = true; | ||
| } | ||
|
|
@@ -572,14 +593,9 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { | |
| return { error: 'Passkey response is required' }; | ||
| } | ||
|
|
||
| const totpTemporaryJWT = this.adminforth.auth.getCustomCookie({cookies: cookies, name: "passkeyLoginTemporaryJWT"}); | ||
| if (!totpTemporaryJWT) { | ||
| return { error: 'Authentication session is expired. Please, try again' } | ||
| } | ||
|
|
||
| const decoded = await this.adminforth.auth.verify(totpTemporaryJWT, 'tempLoginPasskeyChallenge', false); | ||
| if (!decoded) { | ||
| return { error: 'Authentication session is expired. Please, try again' } | ||
| const cookiesValidationResult = await this.validateCookiesForPasskeyLogin(cookies); | ||
| if (!cookiesValidationResult.ok) { | ||
| return { error: cookiesValidationResult.error }; | ||
| } | ||
|
|
||
| let parsedPasskeyResponse; | ||
|
|
@@ -612,7 +628,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { | |
| return { error: 'User not found' }; | ||
| } | ||
|
|
||
| const verificationResult = await this.verifyPasskeyResponse(passkeyResponse, userPk, decoded); | ||
| const verificationResult = await this.verifyPasskeyResponse(passkeyResponse, userPk, cookiesValidationResult.decodedPasskeysCookies); | ||
| if (!verificationResult.ok || !verificationResult.passkeyConfirmed) { | ||
| return { error: 'Passkey verification failed' }; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.