Skip to content

Commit

Permalink
Rename fn to explicitly indicate it converts the device to valid cred…
Browse files Browse the repository at this point in the history
…ential data.
  • Loading branch information
sea-snake committed Dec 30, 2024
1 parent 4bcb33c commit 06249ce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/frontend/src/flows/recovery/recoverWith/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import infoToastCopy from "$src/components/infoToast/copy.json";
import { promptUserNumberTemplate } from "$src/components/promptUserNumber";
import { toast } from "$src/components/toast";
import { I18n } from "$src/i18n";
import { convertToCredentialData } from "$src/utils/credential-devices";
import { convertToValidCredentialData } from "$src/utils/credential-devices";
import {
AuthFail,
Connection,
Expand Down Expand Up @@ -111,7 +111,7 @@ const attemptRecovery = async ({
}

const credentialData = recoveryCredentials
.map(convertToCredentialData)
.map(convertToValidCredentialData)
.filter(nonNullish);

return await connection.fromWebauthnCredentials(userNumber, credentialData);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/utils/credential-devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type CredentialData = {
const derFromPubkey = (pubkey: DeviceKey): DerEncodedPublicKey =>
new Uint8Array(pubkey).buffer as DerEncodedPublicKey;

export const convertToCredentialData = (
export const convertToValidCredentialData = (
device: Omit<DeviceData, "alias">
): CredentialData | undefined => {
// In certain cases, e.g. Chrome on Windows 10, an invalid credential id is
Expand Down
35 changes: 19 additions & 16 deletions src/frontend/src/utils/iiConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
} from "$src/repositories/identityMetadata";
import { ActorSubclass, DerEncodedPublicKey, Signature } from "@dfinity/agent";
import { DelegationIdentity, WebAuthnIdentity } from "@dfinity/identity";
import { CredentialData, convertToCredentialData } from "./credential-devices";
import {
CredentialData,
convertToValidCredentialData,
} from "./credential-devices";
import { AuthenticatedConnection, Connection } from "./iiConnection";
import { MultiWebAuthnIdentity } from "./multiWebAuthnIdentity";

Expand Down Expand Up @@ -171,7 +174,7 @@ describe("Connection.login", () => {
expect(loginResult.showAddCurrentDevice).toBe(false);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledTimes(1);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledWith(
[convertToCredentialData(mockDevice)],
[convertToValidCredentialData(mockDevice)],
"identity.ic0.app"
);
}
Expand All @@ -181,10 +184,10 @@ describe("Connection.login", () => {
// This one would fail because it's not the device the user is using at the moment.
const currentOriginDevice: DeviceData = createMockDevice(currentOrigin);
const currentOriginCredentialData =
convertToCredentialData(currentOriginDevice);
convertToValidCredentialData(currentOriginDevice);
const currentDevice: DeviceData = createMockDevice();
const currentDeviceCredentialData =
convertToCredentialData(currentDevice);
convertToValidCredentialData(currentDevice);
const mockActor = {
identity_info: vi.fn().mockResolvedValue({ Ok: { metadata: [] } }),
lookup: vi.fn().mockResolvedValue([currentOriginDevice, currentDevice]),
Expand Down Expand Up @@ -230,10 +233,10 @@ describe("Connection.login", () => {
it("connection doesn't exclude rpId if user has only one domain", async () => {
const currentOriginDevice: DeviceData = createMockDevice(currentOrigin);
const currentOriginCredentialData =
convertToCredentialData(currentOriginDevice);
convertToValidCredentialData(currentOriginDevice);
const currentOriginDevice2: DeviceData = createMockDevice(currentOrigin);
const currentOriginCredentialData2 =
convertToCredentialData(currentOriginDevice2);
convertToValidCredentialData(currentOriginDevice2);
const mockActor = {
identity_info: vi.fn().mockResolvedValue({ Ok: { metadata: [] } }),
lookup: vi
Expand Down Expand Up @@ -298,7 +301,7 @@ describe("Connection.login", () => {
expect(loginResult.connection).toBeInstanceOf(AuthenticatedConnection);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledTimes(1);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledWith(
[convertToCredentialData(mockDevice)],
[convertToValidCredentialData(mockDevice)],
undefined
);
}
Expand Down Expand Up @@ -326,7 +329,7 @@ describe("Connection.login", () => {
expect(loginResult.connection).toBeInstanceOf(AuthenticatedConnection);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledTimes(1);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledWith(
[convertToCredentialData(mockDevice)],
[convertToValidCredentialData(mockDevice)],
undefined
);
}
Expand All @@ -335,10 +338,10 @@ describe("Connection.login", () => {
it("connection does not exclude rpId when user cancels", async () => {
const currentOriginDevice: DeviceData = createMockDevice(currentOrigin);
const currentOriginCredentialData =
convertToCredentialData(currentOriginDevice);
convertToValidCredentialData(currentOriginDevice);
const currentDevice: DeviceData = createMockDevice();
const currentDeviceCredentialData =
convertToCredentialData(currentDevice);
convertToValidCredentialData(currentDevice);
const mockActor = {
identity_info: vi.fn().mockResolvedValue({ Ok: { metadata: [] } }),
lookup: vi.fn().mockResolvedValue([currentOriginDevice, currentDevice]),
Expand Down Expand Up @@ -401,7 +404,7 @@ describe("Connection.login", () => {
expect(loginResult.connection).toBeInstanceOf(AuthenticatedConnection);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledTimes(1);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledWith(
[convertToCredentialData(mockDevice)],
[convertToValidCredentialData(mockDevice)],
undefined
);
}
Expand Down Expand Up @@ -429,7 +432,7 @@ describe("Connection.login", () => {
expect(loginResult.showAddCurrentDevice).toBe(false);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledTimes(1);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledWith(
[convertToCredentialData(mockDevice)],
[convertToValidCredentialData(mockDevice)],
undefined
);
}
Expand All @@ -438,10 +441,10 @@ describe("Connection.login", () => {
it("connection does not exclude rpId when user cancels", async () => {
const currentOriginDevice: DeviceData = createMockDevice(currentOrigin);
const currentOriginCredentialData =
convertToCredentialData(currentOriginDevice);
convertToValidCredentialData(currentOriginDevice);
const currentDevice: DeviceData = createMockDevice();
const currentDeviceCredentialData =
convertToCredentialData(currentDevice);
convertToValidCredentialData(currentDevice);
const mockActor = {
identity_info: vi.fn().mockResolvedValue({ Ok: { metadata: [] } }),
lookup: vi.fn().mockResolvedValue([currentOriginDevice, currentDevice]),
Expand Down Expand Up @@ -501,7 +504,7 @@ describe("Connection.login", () => {
await connection.login(BigInt(12345));
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledTimes(1);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledWith(
[convertToCredentialData(deviceWithCredentialId)],
[convertToValidCredentialData(deviceWithCredentialId)],
undefined
);
});
Expand All @@ -525,7 +528,7 @@ describe("Connection.login", () => {
await connection.login(BigInt(12345));
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledTimes(1);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledWith(
[convertToCredentialData(deviceValidCredentialId)],
[convertToValidCredentialData(deviceValidCredentialId)],
undefined
);
});
Expand Down
7 changes: 5 additions & 2 deletions src/frontend/src/utils/iiConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ import {
} from "@dfinity/identity";
import { Principal } from "@dfinity/principal";
import { isNullish, nonNullish } from "@dfinity/utils";
import { convertToCredentialData, CredentialData } from "./credential-devices";
import {
convertToValidCredentialData,
CredentialData,
} from "./credential-devices";
import {
excludeCredentialsFromOrigins,
findWebAuthnRpId,
Expand Down Expand Up @@ -387,7 +390,7 @@ export class Connection {

return this.fromWebauthnCredentials(
userNumber,
devices.map(convertToCredentialData).filter(nonNullish)
devices.map(convertToValidCredentialData).filter(nonNullish)
);
};

Expand Down

0 comments on commit 06249ce

Please sign in to comment.