Skip to content

Commit

Permalink
Get FIDO challenge asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
vicr123 committed Sep 23, 2024
1 parent f2d58eb commit ef13467
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export function LoginPasswordModal({
}
});

useEffect(() => {
if (acquisitionSession.loginTypes.includes("fido")) {
// Start getting the FIDO token now to save time later
void acquisitionSession.updateFidoToken();
}
}, []);

return (
<Modal
heading={
Expand Down
26 changes: 21 additions & 5 deletions Parlance.ClientApp/src/helpers/TokenAcquisitionSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class TokenAcquisitionSession {
private readonly _successFunction: (token: string) => void;
private readonly _failureFunction: () => void;
private loginSessionDetails: Record<string, any> = {};
private fidoTokenResponse: Promise<TokenResponseFido> | undefined;

constructor(
username: string,
Expand Down Expand Up @@ -162,15 +163,24 @@ export class TokenAcquisitionSession {
}
}

updateFidoToken() {
if (this.fidoTokenResponse) return this.fidoTokenResponse;
this.fidoTokenResponse = Fetch.post<TokenResponseFido>(
"/api/user/token",
{
type: "fido",
username: this._username,
},
);
return this.fidoTokenResponse;
}

async attemptFido2Login() {
Modal.mount(<LoginSecurityKeyModal />);

let details: TokenResponseFido;
try {
details = await Fetch.post<TokenResponseFido>("/api/user/token", {
type: "fido",
username: this._username,
});
details = await this.updateFidoToken();
} catch {
Modal.mount(
<LoginSecurityKeyFailureModal acquisitionSession={this} />,
Expand All @@ -196,7 +206,10 @@ export class TokenAcquisitionSession {
})) as PublicKeyCredential;

console.log(assertion);
if (!assertion) throw assertion;
if (!assertion) {
// noinspection ExceptionCaughtLocallyJS
throw assertion;
}

const response =
assertion.response as AuthenticatorAssertionResponse;
Expand All @@ -216,6 +229,9 @@ export class TokenAcquisitionSession {
},
});

// Require a new FIDO challenge next time in case this fails
this.fidoTokenResponse = undefined;

await this.attemptLogin({
fido2Details: details,
});
Expand Down

0 comments on commit ef13467

Please sign in to comment.