Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use config related origins #2862

Merged
merged 6 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/frontend/src/utils/findWebAuthnFlows.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { LEGACY_II_URL } from "$src/config";
import { CredentialData } from "./credential-devices";
import { findWebAuthnFlows } from "./findWebAuthnFlows";
import { PROD_DOMAINS } from "./findWebAuthnRpId";

describe("findWebAuthnFlows", () => {
const currentOrigin = "https://identity.internetcomputer.org";
const nonCurrentOrigin1 = "https://identity.ic0.app";
const nonCurrentOrigin1RpId = new URL(nonCurrentOrigin1).hostname;
const nonCurrentOrigin2 = "https://identity.icp0.io";
const nonCurrentOrigin2RpId = new URL(nonCurrentOrigin2).hostname;
const relatedOrigins = PROD_DOMAINS;
const relatedOrigins = [
"https://identity.ic0.app",
"https://identity.internetcomputer.org",
"https://identity.icp0.io",
];

const createMockCredential = (
origin: string | undefined
Expand Down
14 changes: 12 additions & 2 deletions src/frontend/src/utils/findWebAuthnRpId.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { CredentialData } from "./credential-devices";
import {
BETA_DOMAINS,
PROD_DOMAINS,
excludeCredentialsFromOrigins,
findWebAuthnRpId,
} from "./findWebAuthnRpId";

const BETA_DOMAINS = [
"https://beta.identity.ic0.app",
"https://beta.identity.internetcomputer.org",
"https://fgte5-ciaaa-aaaad-aaatq-cai.ic0.app",
];

const PROD_DOMAINS = [
"https://identity.ic0.app",
"https://identity.internetcomputer.org",
"https://identity.icp0.io",
];

describe("findWebAuthnRpId", () => {
const mockDeviceData = (origin?: string): CredentialData => ({
origin,
Expand Down
37 changes: 30 additions & 7 deletions src/frontend/src/utils/iiConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ const DEFAULT_INIT: InternetIdentityInit = {
captcha_config: [],
openid_google: [],
register_rate_limit: [],
related_origins: [],
related_origins: [
[
"https://identity.ic0.app",
"https://identity.internetcomputer.org",
"https://identity.icp0.io",
],
],
};

const mockActor = {
Expand Down Expand Up @@ -119,6 +125,7 @@ test("commits changes on identity metadata", async () => {
mockActor
);

expect(infoResponse).toBeUndefined();
await vi.waitFor(() => expect(infoResponse).toEqual(mockRawMetadata));

expect(await connection.getIdentityMetadata()).toEqual(mockIdentityMetadata);
Expand Down Expand Up @@ -212,6 +219,28 @@ describe("Connection.login", () => {
}
});

it("login returns undefined RP ID if no related origins are in the config", async () => {
const config: InternetIdentityInit = {
...DEFAULT_INIT,
related_origins: [],
};
const connection = new Connection("aaaaa-aa", config, mockActor);

const loginResult = await connection.login(BigInt(12345));

expect(loginResult.kind).toBe("loginSuccess");
if (loginResult.kind === "loginSuccess") {
expect(loginResult.connection).toBeInstanceOf(AuthenticatedConnection);
expect(loginResult.showAddCurrentDevice).toBe(false);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledTimes(1);
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledWith(
[convertToValidCredentialData(mockDevice)],
undefined,
false
);
}
});

it("connection excludes rpId when user cancels", async () => {
// This one would fail because it's not the device the user is using at the moment.
const currentOriginDevice: DeviceData = createMockDevice(currentOrigin);
Expand Down Expand Up @@ -566,12 +595,6 @@ describe("Connection.login", () => {
credential_id: [Uint8Array.from([0, 0, 0, 0, 0])],
};
const mockActor = {
identity_info: vi.fn().mockImplementation(async () => {
// The `await` is necessary to make sure that the `getterResponse` is set before the test continues.
infoResponse = await mockRawMetadata;
return { Ok: { metadata: mockRawMetadata } };
}),
identity_metadata_replace: vi.fn().mockResolvedValue({ Ok: null }),
lookup: vi.fn().mockResolvedValue([pinDevice]),
} as unknown as ActorSubclass<_SERVICE>;

Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/utils/iiConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import {
CredentialData,
} from "./credential-devices";
import { findWebAuthnFlows, WebAuthnFlow } from "./findWebAuthnFlows";
import { relatedDomains } from "./findWebAuthnRpId";
import { MultiWebAuthnIdentity } from "./multiWebAuthnIdentity";
import { isRecoveryDevice, RecoveryDevice } from "./recoveryDevice";
import { supportsWebauthRoR } from "./userAgent";
Expand Down Expand Up @@ -432,7 +431,8 @@ export class Connection {
supportsRor: supportsWebauthRoR(window.navigator.userAgent),
devices: credentials,
currentOrigin: window.location.origin,
relatedOrigins: relatedDomains(),
// Empty array is the same as no related origins.
relatedOrigins: this.canisterConfig.related_origins[0] ?? [],
});
this.webAuthFlows = {
flows,
Expand Down
Loading