mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 08:43:33 +00:00
[EC-598] feat: fully working register and assert flow
This commit is contained in:
@@ -210,6 +210,8 @@ export default class RuntimeBackground {
|
||||
break;
|
||||
case "fido2RegisterCredentialRequest":
|
||||
return await this.main.fido2Service.createCredential(msg.data);
|
||||
case "fido2GetCredentialRequest":
|
||||
return await this.main.fido2Service.assertCredential(msg.data);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { Fido2Utils } from "@bitwarden/common/abstractions/fido2/fido2-utils";
|
||||
import {
|
||||
CredentialAssertParams,
|
||||
CredentialAssertResult,
|
||||
CredentialRegistrationParams,
|
||||
CredentialRegistrationResult,
|
||||
} from "@bitwarden/common/abstractions/fido2/fido2.service.abstraction";
|
||||
@@ -62,4 +64,38 @@ export class WebauthnUtils {
|
||||
getClientExtensionResults: () => ({}),
|
||||
};
|
||||
}
|
||||
|
||||
static mapCredentialRequestOptions(
|
||||
options: CredentialRequestOptions,
|
||||
origin: string
|
||||
): CredentialAssertParams {
|
||||
const keyOptions = options.publicKey;
|
||||
|
||||
if (keyOptions == undefined) {
|
||||
throw new Error("Public-key options not found");
|
||||
}
|
||||
|
||||
return {
|
||||
origin,
|
||||
allowedCredentialIds:
|
||||
keyOptions.allowCredentials?.map((c) => Fido2Utils.bufferToString(c.id)) ?? [],
|
||||
challenge: Fido2Utils.bufferToString(keyOptions.challenge),
|
||||
rpId: keyOptions.rpId,
|
||||
};
|
||||
}
|
||||
|
||||
static mapCredentialAssertResult(result: CredentialAssertResult): PublicKeyCredential {
|
||||
return {
|
||||
id: result.credentialId,
|
||||
rawId: Fido2Utils.stringToBuffer(result.credentialId),
|
||||
type: "public-key",
|
||||
response: {
|
||||
authenticatorData: Fido2Utils.stringToBuffer(result.authenticatorData),
|
||||
clientDataJSON: Fido2Utils.stringToBuffer(result.clientDataJSON),
|
||||
signature: Fido2Utils.stringToBuffer(result.signature),
|
||||
userHandle: Fido2Utils.stringToBuffer(result.userHandle),
|
||||
} as AuthenticatorAssertionResponse,
|
||||
getClientExtensionResults: () => ({}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,5 +29,22 @@ messenger.addHandler(async (message) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (message.type === MessageType.CredentialGetRequest) {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.runtime.sendMessage(
|
||||
{
|
||||
command: "fido2GetCredentialRequest",
|
||||
data: message.data,
|
||||
},
|
||||
(response) => {
|
||||
resolve({
|
||||
type: MessageType.CredentialGetResponse,
|
||||
result: response,
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
return undefined;
|
||||
});
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import {
|
||||
CredentialAssertParams,
|
||||
CredentialAssertResult,
|
||||
CredentialRegistrationParams,
|
||||
CredentialRegistrationResult,
|
||||
} from "@bitwarden/common/abstractions/fido2/fido2.service.abstraction";
|
||||
@@ -25,10 +27,12 @@ export type CredentialCreationResponse = {
|
||||
|
||||
export type CredentialGetRequest = {
|
||||
type: MessageType.CredentialGetRequest;
|
||||
data: CredentialAssertParams;
|
||||
};
|
||||
|
||||
export type CredentialGetResponse = {
|
||||
type: MessageType.CredentialGetResponse;
|
||||
result?: CredentialAssertResult;
|
||||
};
|
||||
|
||||
export type AbortRequest = {
|
||||
|
||||
@@ -27,5 +27,14 @@ navigator.credentials.create = async (options?: CredentialCreationOptions): Prom
|
||||
};
|
||||
|
||||
navigator.credentials.get = async (options?: CredentialRequestOptions): Promise<Credential> => {
|
||||
return await browserCredentials.get(options);
|
||||
const response = await messenger.request({
|
||||
type: MessageType.CredentialGetRequest,
|
||||
data: WebauthnUtils.mapCredentialRequestOptions(options, window.location.origin),
|
||||
});
|
||||
|
||||
if (response.type !== MessageType.CredentialGetResponse) {
|
||||
return await browserCredentials.get(options);
|
||||
}
|
||||
|
||||
return WebauthnUtils.mapCredentialAssertResult(response.result);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user