|
| 1 | +import { type Readable, derived, writable, get } from "svelte/store"; |
| 2 | +import { isNullish } from "@dfinity/utils"; |
| 3 | +import type { InternetIdentityInit } from "$lib/generated/internet_identity_types"; |
| 4 | +import { Principal } from "@dfinity/principal"; |
| 5 | +import { |
| 6 | + authenticationProtocol, |
| 7 | + AuthRequest, |
| 8 | +} from "$lib/flows/authorize/postMessageInterface"; |
| 9 | +import { authenticatedStore } from "$lib/stores/authentication.store"; |
| 10 | +import { Connection } from "$lib/utils/iiConnection"; |
| 11 | +import { fetchDelegation } from "$lib/flows/authorize/fetchDelegation"; |
| 12 | + |
| 13 | +export type AuthorizationContext = { |
| 14 | + authRequest: AuthRequest; |
| 15 | + requestOrigin: string; |
| 16 | +}; |
| 17 | + |
| 18 | +export type AuthorizationStatus = |
| 19 | + | "init" |
| 20 | + | "orphan" |
| 21 | + | "closed" |
| 22 | + | "waiting" |
| 23 | + | "validating" |
| 24 | + | "invalid" |
| 25 | + | "authenticating" |
| 26 | + | "success" |
| 27 | + | "failure"; |
| 28 | + |
| 29 | +type AuthorizationStore = Readable<{ |
| 30 | + context?: AuthorizationContext; |
| 31 | + status: AuthorizationStatus; |
| 32 | +}> & { |
| 33 | + init: (params: { |
| 34 | + canisterId: Principal; |
| 35 | + canisterConfig: InternetIdentityInit; |
| 36 | + }) => Promise<void>; |
| 37 | + authorize: (accountNumber: bigint | undefined) => Promise<void>; |
| 38 | +}; |
| 39 | + |
| 40 | +const internalStore = writable<{ |
| 41 | + context?: AuthorizationContext; |
| 42 | + status: AuthorizationStatus; |
| 43 | +}>({ status: "init" }); |
| 44 | + |
| 45 | +let authorize: (accountNumber: bigint | undefined) => Promise<void>; |
| 46 | + |
| 47 | +export const authorizationStore: AuthorizationStore = { |
| 48 | + init: async ({ canisterId, canisterConfig }) => { |
| 49 | + const status = await authenticationProtocol({ |
| 50 | + authenticate: (context) => { |
| 51 | + console.log("authenticate", context); |
| 52 | + internalStore.set({ context, status: "authenticating" }); |
| 53 | + return new Promise((resolve) => { |
| 54 | + authorize = async (_accountNumber) => { |
| 55 | + // TODO: use prepare/get account delegation instead of iiConnection |
| 56 | + const { identityNumber, identity } = get(authenticatedStore); |
| 57 | + const { connection } = await new Connection( |
| 58 | + canisterId.toText(), |
| 59 | + canisterConfig, |
| 60 | + ).fromDelegationIdentity(identityNumber, identity); |
| 61 | + const derivationOrigin = |
| 62 | + context.authRequest.derivationOrigin ?? context.requestOrigin; |
| 63 | + const result = await fetchDelegation({ |
| 64 | + connection, |
| 65 | + derivationOrigin, |
| 66 | + publicKey: context.authRequest.sessionPublicKey, |
| 67 | + maxTimeToLive: context.authRequest.maxTimeToLive, |
| 68 | + }); |
| 69 | + if ("error" in result) { |
| 70 | + resolve({ kind: "failure", text: "Couldn't fetch delegation" }); |
| 71 | + return; |
| 72 | + } |
| 73 | + const [userKey, parsed_signed_delegation] = result; |
| 74 | + resolve({ |
| 75 | + kind: "success", |
| 76 | + delegations: [parsed_signed_delegation], |
| 77 | + userPublicKey: new Uint8Array(userKey), |
| 78 | + // This is a authnMethod forwarded to the app that requested authorization. |
| 79 | + // We don't want to leak which authnMethod was used. |
| 80 | + authnMethod: "passkey", |
| 81 | + }); |
| 82 | + }; |
| 83 | + }); |
| 84 | + }, |
| 85 | + onProgress: (status) => |
| 86 | + internalStore.update((value) => ({ ...value, status })), |
| 87 | + }); |
| 88 | + internalStore.update((value) => ({ ...value, status })); |
| 89 | + }, |
| 90 | + subscribe: (...args) => internalStore.subscribe(...args), |
| 91 | + authorize: (accountNumber) => { |
| 92 | + if (isNullish(authorize)) { |
| 93 | + throw new Error("Not ready yet for authorization"); |
| 94 | + } |
| 95 | + return authorize(accountNumber); |
| 96 | + }, |
| 97 | +}; |
| 98 | + |
| 99 | +export const authorizationContextStore: Readable<AuthorizationContext> = |
| 100 | + derived(authorizationStore, ({ context }) => { |
| 101 | + if (isNullish(context)) { |
| 102 | + throw new Error("Authorization context is not available yet"); |
| 103 | + } |
| 104 | + return context; |
| 105 | + }); |
| 106 | + |
| 107 | +export const authorizationStatusStore: Readable<AuthorizationStatus> = derived( |
| 108 | + internalStore, |
| 109 | + ({ status }) => status, |
| 110 | +); |
0 commit comments