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

Implement mock openID actor methods #2761

Merged
merged 3 commits into from
Dec 31, 2024
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
28 changes: 26 additions & 2 deletions src/frontend/src/utils/iiConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
IdentityMetadata,
IdentityMetadataRepository,
} from "$src/repositories/identityMetadata";
import { JWT, MockOpenID, OpenIDCredential, Salt } from "$src/utils/mockOpenID";
import { diagnosticInfo, unknownToString } from "$src/utils/utils";
import {
Actor,
Expand Down Expand Up @@ -153,6 +154,8 @@ export class Connection {
// registered in another domain. In this case, we must try the other rpID.
// Map<userNumber, Set<rpID>>
private _cancelledRpIds: Map<bigint, Set<string | undefined>> = new Map();
protected _mockOpenID = new MockOpenID();

public constructor(
readonly canisterId: string,
// Used for testing purposes
Expand Down Expand Up @@ -615,6 +618,7 @@ export class Connection {

export class AuthenticatedConnection extends Connection {
private metadataRepository: IdentityMetadataRepository;

public constructor(
public canisterId: string,
public identity: SignIdentity,
Expand Down Expand Up @@ -662,9 +666,15 @@ export class AuthenticatedConnection extends Connection {
return this.actor;
}

getAnchorInfo = async (): Promise<IdentityAnchorInfo> => {
getAnchorInfo = async (): Promise<
IdentityAnchorInfo & { credentials: OpenIDCredential[] }
> => {
const actor = await this.getActor();
return await actor.get_anchor_info(this.userNumber);
const anchorInfo = await actor.get_anchor_info(this.userNumber);
const mockedAnchorInfo = await this._mockOpenID.get_anchor_info(
this.userNumber
);
return { ...anchorInfo, ...mockedAnchorInfo };
};

getPrincipal = async ({
Expand Down Expand Up @@ -906,6 +916,20 @@ export class AuthenticatedConnection extends Connection {
console.error("Unknown error", err);
return { error: "internal_error" };
};

addJWT = async (jwt: JWT, salt: Salt): Promise<void> => {
const result = await this._mockOpenID.add_jwt(this.userNumber, jwt, salt);
if ("Err" in result) {
throw new Error(result.Err);
}
};

removeJWT = async (iss: string, sub: string): Promise<void> => {
const result = await this._mockOpenID.remove_jwt(this.userNumber, iss, sub);
if ("Err" in result) {
throw new Error(result.Err);
}
};
}

// Reads the "origin" used to infer what domain a FIDO device is available on.
Expand Down
66 changes: 66 additions & 0 deletions src/frontend/src/utils/mockOpenID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { MetadataMapV2, UserNumber } from "$generated/internet_identity_types";
import { Principal } from "@dfinity/principal";

export type JWT = string;
export type Salt = Uint8Array;

export interface OpenIDCredential {
iss: string;
sub: string;
aud: string;
principal: Principal;
last_usage_timestamp: bigint;
metadata: MetadataMapV2;
}

// Mocked implementation of new or existing actor methods with only additional data
export class MockOpenID {
#credentials: OpenIDCredential[] = [];

add_jwt(
_userNumber: UserNumber,
jwt: JWT,
_salt: Salt
): Promise<{ Ok: null } | { Err: string }> {
const [_header, body, _signature] = jwt.split(".");
const { iss, sub, aud, email, name, picture } = JSON.parse(atob(body));
if (
this.#credentials.find(
(credential) => credential.iss === iss && credential.sub === sub
)
) {
return Promise.resolve({ Err: "This account has already been linked" });
}
this.#credentials.push({
iss,
sub,
aud,
principal: Principal.anonymous(),
last_usage_timestamp: BigInt(Date.now()) * BigInt(1000000),
metadata: [
["email", { String: email }],
["name", { String: name }],
["picture", { String: picture }],
],
});
return Promise.resolve({ Ok: null });
}

remove_jwt(
_userNumber: UserNumber,
iss: string,
sub: string
): Promise<{ Ok: null } | { Err: string }> {
const index = this.#credentials.findIndex(
(credential) => credential.iss === iss && credential.sub === sub
);
this.#credentials.splice(index, 1);
return Promise.resolve({ Ok: null });
}

get_anchor_info(_userNumber: UserNumber): Promise<{
credentials: OpenIDCredential[];
}> {
return Promise.resolve({ credentials: this.#credentials });
}
}
Loading