This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: add webauthn for testing Signed-off-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de> * fix: add webauthn keys for verification Signed-off-by: Mirko Mollik <mirkomollik@gmail.com> * fix: set correct config Signed-off-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de> --------- Signed-off-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de> Signed-off-by: Mirko Mollik <mirkomollik@gmail.com>
- Loading branch information
Showing
28 changed files
with
1,389 additions
and
249 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,5 @@ | ||
export class KeycloakUser { | ||
sub: string; | ||
email: string; | ||
[key: string]: unknown; | ||
} |
15 changes: 15 additions & 0 deletions
15
apps/holder-backend/src/app/auth/webauthn/dto/authentication-response.dto.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { | ||
AuthenticationExtensionsClientOutputs, | ||
AuthenticationResponseJSON, | ||
AuthenticatorAssertionResponseJSON, | ||
AuthenticatorAttachment, | ||
} from '@simplewebauthn/types'; | ||
|
||
export class AuthenticationResponse implements AuthenticationResponseJSON { | ||
id: string; | ||
rawId: string; | ||
response: AuthenticatorAssertionResponseJSON; | ||
authenticatorAttachment?: AuthenticatorAttachment; | ||
clientExtensionResults: AuthenticationExtensionsClientOutputs; | ||
type: 'public-key'; | ||
} |
15 changes: 15 additions & 0 deletions
15
apps/holder-backend/src/app/auth/webauthn/dto/registration-response.dto.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { | ||
AuthenticationExtensionsClientOutputs, | ||
AuthenticatorAttachment, | ||
AuthenticatorAttestationResponseJSON, | ||
RegistrationResponseJSON, | ||
} from '@simplewebauthn/types'; | ||
|
||
export class RegistrationResponse implements RegistrationResponseJSON { | ||
id: string; | ||
rawId: string; | ||
response: AuthenticatorAttestationResponseJSON; | ||
authenticatorAttachment?: AuthenticatorAttachment; | ||
clientExtensionResults: AuthenticationExtensionsClientOutputs; | ||
type: 'public-key'; | ||
} |
46 changes: 46 additions & 0 deletions
46
apps/holder-backend/src/app/auth/webauthn/entities/passkey.entity.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
BeforeInsert, | ||
BeforeUpdate, | ||
Column, | ||
Entity, | ||
PrimaryColumn, | ||
} from 'typeorm'; | ||
import type { | ||
AuthenticatorTransportFuture, | ||
CredentialDeviceType, | ||
Base64URLString, | ||
} from '@simplewebauthn/types'; | ||
|
||
@Entity() | ||
export class Passkey { | ||
// SQL: Store as `TEXT`. Index this column | ||
@PrimaryColumn() | ||
id: Base64URLString; | ||
// SQL: Store raw bytes as `BYTEA`/`BLOB`/etc... | ||
// Caution: Node ORM's may map this to a Buffer on retrieval, | ||
// convert to Uint8Array as necessary | ||
@Column() | ||
publicKey: string; | ||
|
||
// SQL: Foreign Key to an instance of your internal user model | ||
@Column() | ||
user: string; | ||
// SQL: Store as `TEXT`. Index this column. A UNIQUE constraint on | ||
// (webAuthnUserID + user) also achieves maximum user privacy | ||
@Column() | ||
webauthnUserID: Base64URLString; | ||
// SQL: Consider `BIGINT` since some authenticators return atomic timestamps as counters | ||
@Column({ type: 'bigint' }) | ||
counter: number; | ||
// SQL: `VARCHAR(32)` or similar, longest possible value is currently 12 characters | ||
// Ex: 'singleDevice' | 'multiDevice' | ||
@Column() | ||
deviceType: CredentialDeviceType; | ||
// SQL: `BOOL` or whatever similar type is supported | ||
@Column({ type: 'boolean' }) | ||
backedUp: boolean; | ||
// SQL: `VARCHAR(255)` and store string array as a CSV string | ||
// Ex: ['ble' | 'cable' | 'hybrid' | 'internal' | 'nfc' | 'smart-card' | 'usb'] | ||
@Column({ nullable: true, type: 'json' }) | ||
transports?: AuthenticatorTransportFuture[]; | ||
} |
66 changes: 66 additions & 0 deletions
66
apps/holder-backend/src/app/auth/webauthn/webauthn.controller.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
Post, | ||
Req, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ApiOAuth2, ApiOperation, ApiTags } from '@nestjs/swagger'; | ||
import { AuthGuard, AuthenticatedUser } from 'nest-keycloak-connect'; | ||
import { WebauthnService } from './webauthn.service'; | ||
import { KeycloakUser } from '../user'; | ||
import { RegistrationResponse } from './dto/registration-response.dto'; | ||
import { Request } from 'express'; | ||
|
||
@UseGuards(AuthGuard) | ||
@ApiOAuth2([]) | ||
@ApiTags('auth') | ||
@Controller('webauthn') | ||
export class WebAuthnController { | ||
constructor(private webAuthnService: WebauthnService) {} | ||
|
||
@ApiOperation({ summary: 'get registration options' }) | ||
@Get('registration') | ||
getRegistrationOptions(@AuthenticatedUser() user: KeycloakUser) { | ||
return this.webAuthnService.generateRegistrationOptions( | ||
user.sub, | ||
user.email | ||
); | ||
} | ||
|
||
@ApiOperation({ summary: 'complete registration' }) | ||
@Post('registration') | ||
verifyRegistration( | ||
@AuthenticatedUser() user: KeycloakUser, | ||
@Body() body: RegistrationResponse, | ||
@Req() req: Request | ||
) { | ||
const expectedOrigin = req.headers.origin; | ||
return this.webAuthnService.startRegistration( | ||
user.sub, | ||
body, | ||
expectedOrigin | ||
); | ||
} | ||
|
||
@ApiOperation({ summary: 'get keys' }) | ||
@Get('keys') | ||
getKeys(@AuthenticatedUser() user: KeycloakUser) { | ||
return this.webAuthnService.getKeys(user.sub); | ||
} | ||
|
||
@ApiOperation({ summary: 'delete key' }) | ||
@Delete('keys/:id') | ||
deleteKey(@AuthenticatedUser() user: KeycloakUser, @Param('id') id: string) { | ||
return this.webAuthnService.deleteKey(user.sub, id); | ||
} | ||
|
||
@ApiOperation({ summary: 'get authentication options' }) | ||
@Get('authentication') | ||
getAuthenticationOptions(@AuthenticatedUser() user: KeycloakUser) { | ||
return this.webAuthnService.generateAuthenticationOptions(user.sub); | ||
} | ||
} |
Oops, something went wrong.