Skip to content

Commit

Permalink
fix: update session check (#73)
Browse files Browse the repository at this point in the history
Signed-off-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de>
  • Loading branch information
cre8 authored Jun 27, 2024
1 parent f2f8821 commit 72d1cf9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
23 changes: 20 additions & 3 deletions apps/holder-backend/src/app/auth/webauthn/webauthn.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,20 @@ export class WebauthnService {
this.loginSessions.delete(key);
}

/**
* Verify the authentication response
*/
async verifyAuthenticationResponse(
session: string,
user: string,
body: AuthenticationResponseJSON,
expectedOrigin: string
) {
// (Pseudocode) Get `options.challenge` that was saved above
const currentOptions: PublicKeyCredentialRequestOptionsJSON =
this.getCurrentAuthenticationOptions(session);
// (Pseudocode} Retrieve a passkey from the DB that
// should match the `id` in the returned credential
if (!currentOptions) {
throw new ConflictException('No authentication session found');
}
const passkey: Passkey = await this.getUserPasskey(user, body.id);

if (!passkey) {
Expand Down Expand Up @@ -241,6 +244,11 @@ export class WebauthnService {
await this.saveUpdatedCounter(passkey, newCounter);
}

/**
* Save the updated counter for a passkey
* @param passkey
* @param newCounter
*/
private async saveUpdatedCounter(passkey: Passkey, newCounter: number) {
passkey.counter = newCounter;
await this.passKeyRepository.save(passkey);
Expand All @@ -256,6 +264,15 @@ export class WebauthnService {
return this.passKeyRepository.findOne({ where: { id, user } });
}

/**
* Check if a user has any keys
*/
hasKeys(user: string) {
return this.passKeyRepository.count({ where: { user } }).then((count) => {
return count > 0;
});
}

/**
* Get all keys for a user
* @param sub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class CredentialSelection {
}

export class SubmissionRequest {
auth: {
auth?: {
session: string;
response: AuthenticationResponseJSON;
};
Expand Down
18 changes: 12 additions & 6 deletions apps/holder-backend/src/app/oid4vc/oid4vp/oid4vp.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Body,
ConflictException,
Controller,
Delete,
Param,
Expand Down Expand Up @@ -51,12 +52,17 @@ export class Oid4vpController {
@Req() req: Request
) {
const origin = req.headers.origin;
await this.webauthnService.verifyAuthenticationResponse(
value.auth.session,
user.sub,
value.auth.response,
origin
);
if (await this.webauthnService.hasKeys(user.sub)) {
if (!value.auth) {
throw new ConflictException('No authentication provided');
}
await this.webauthnService.verifyAuthenticationResponse(
value.auth.session,
user.sub,
value.auth.response,
origin
);
}
return this.oid4vciService.accept(id, user.sub, value.values);
}

Expand Down

0 comments on commit 72d1cf9

Please sign in to comment.