|
| 1 | +import { Address, Hex } from 'ox' |
| 2 | +import { Signers } from '@0xsequence/wallet-core' |
| 3 | +import { Handler } from './handler.js' |
| 4 | +import * as Identity from '@0xsequence/identity-instrument' |
| 5 | +import * as Db from '../../dbs/index.js' |
| 6 | +import { Signatures } from '../signatures.js' |
| 7 | +import { SignerActionable, SignerReady, SignerUnavailable, BaseSignatureRequest } from '../types/signature-request.js' |
| 8 | +import { IdentitySigner } from '../../identity/signer.js' |
| 9 | +import { IdentityHandler } from './identity.js' |
| 10 | +import { Kinds } from '../types/signer.js' |
| 11 | +import type { WdkEnv } from '../../env.js' |
| 12 | + |
| 13 | +type RespondFn = (idToken: string) => Promise<void> |
| 14 | + |
| 15 | +export type PromptIdTokenHandler = (kind: 'google-id-token' | `custom-${string}`, respond: RespondFn) => Promise<void> |
| 16 | + |
| 17 | +export class IdTokenHandler extends IdentityHandler implements Handler { |
| 18 | + private onPromptIdToken: undefined | PromptIdTokenHandler |
| 19 | + |
| 20 | + constructor( |
| 21 | + public readonly signupKind: 'google-id-token' | `custom-${string}`, |
| 22 | + public readonly issuer: string, |
| 23 | + public readonly audience: string, |
| 24 | + nitro: Identity.IdentityInstrument, |
| 25 | + signatures: Signatures, |
| 26 | + authKeys: Db.AuthKeys, |
| 27 | + env?: WdkEnv, |
| 28 | + ) { |
| 29 | + super(nitro, authKeys, signatures, Identity.IdentityType.OIDC, env) |
| 30 | + } |
| 31 | + |
| 32 | + public get kind() { |
| 33 | + if (this.signupKind === 'google-id-token') { |
| 34 | + return Kinds.LoginGoogle |
| 35 | + } |
| 36 | + return 'login-' + this.signupKind |
| 37 | + } |
| 38 | + |
| 39 | + public registerUI(onPromptIdToken: PromptIdTokenHandler) { |
| 40 | + this.onPromptIdToken = onPromptIdToken |
| 41 | + return () => { |
| 42 | + this.onPromptIdToken = undefined |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public unregisterUI() { |
| 47 | + this.onPromptIdToken = undefined |
| 48 | + } |
| 49 | + |
| 50 | + public async completeAuth(idToken: string): Promise<[IdentitySigner, { [key: string]: string }]> { |
| 51 | + const challenge = new Identity.IdTokenChallenge(this.issuer, this.audience, idToken) |
| 52 | + await this.nitroCommitVerifier(challenge) |
| 53 | + const { signer: identitySigner, email } = await this.nitroCompleteAuth(challenge) |
| 54 | + |
| 55 | + return [identitySigner, { email }] |
| 56 | + } |
| 57 | + |
| 58 | + public async getSigner(): Promise<{ signer: Signers.Signer & Signers.Witnessable; email: string }> { |
| 59 | + const onPromptIdToken = this.onPromptIdToken |
| 60 | + if (!onPromptIdToken) { |
| 61 | + throw new Error('id-token-handler-ui-not-registered') |
| 62 | + } |
| 63 | + |
| 64 | + return await this.handleAuth(onPromptIdToken) |
| 65 | + } |
| 66 | + |
| 67 | + async status( |
| 68 | + address: Address.Address, |
| 69 | + _imageHash: Hex.Hex | undefined, |
| 70 | + request: BaseSignatureRequest, |
| 71 | + ): Promise<SignerUnavailable | SignerReady | SignerActionable> { |
| 72 | + const signer = await this.getAuthKeySigner(address) |
| 73 | + if (signer) { |
| 74 | + return { |
| 75 | + address, |
| 76 | + handler: this, |
| 77 | + status: 'ready', |
| 78 | + handle: async () => { |
| 79 | + await this.sign(signer, request) |
| 80 | + return true |
| 81 | + }, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const onPromptIdToken = this.onPromptIdToken |
| 86 | + if (!onPromptIdToken) { |
| 87 | + return { |
| 88 | + address, |
| 89 | + handler: this, |
| 90 | + reason: 'ui-not-registered', |
| 91 | + status: 'unavailable', |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return { |
| 96 | + address, |
| 97 | + handler: this, |
| 98 | + status: 'actionable', |
| 99 | + message: 'request-id-token', |
| 100 | + handle: async () => { |
| 101 | + try { |
| 102 | + const { signer } = await this.handleAuth(onPromptIdToken) |
| 103 | + const signerAddress = (await signer.address) as Address.Address |
| 104 | + if (!Address.isEqual(signerAddress, address)) { |
| 105 | + // ID-token auth prompts are keyed by provider kind, not the requested signer address. |
| 106 | + // For example, a user can pick a different Google account in the account picker and |
| 107 | + // return a token for a different identity than this request expects. |
| 108 | + await this.clearAuthKeySigner(signerAddress) |
| 109 | + throw new Error('id-token-signer-mismatch') |
| 110 | + } |
| 111 | + return true |
| 112 | + } catch { |
| 113 | + return false |
| 114 | + } |
| 115 | + }, |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private handleAuth( |
| 120 | + onPromptIdToken: PromptIdTokenHandler, |
| 121 | + ): Promise<{ signer: Signers.Signer & Signers.Witnessable; email: string }> { |
| 122 | + // eslint-disable-next-line no-async-promise-executor |
| 123 | + return new Promise(async (resolve, reject) => { |
| 124 | + try { |
| 125 | + const respond: RespondFn = async (idToken) => { |
| 126 | + try { |
| 127 | + const [signer, metadata] = await this.completeAuth(idToken) |
| 128 | + resolve({ signer, email: metadata.email || '' }) |
| 129 | + } catch (error) { |
| 130 | + reject(error) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + await onPromptIdToken(this.signupKind, respond) |
| 135 | + } catch (error) { |
| 136 | + reject(error) |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
0 commit comments