|
| 1 | +import { Hex, Address, Bytes } from 'ox' |
| 2 | +import { Handler } from './handler.js' |
| 3 | +import * as Db from '../../dbs/index.js' |
| 4 | +import { Signatures } from '../signatures.js' |
| 5 | +import * as Identity from '../../identity/index.js' |
| 6 | +import { SignerUnavailable, SignerReady, SignerActionable, BaseSignatureRequest } from '../types/signature-request.js' |
| 7 | +import { IdentitySigner } from '../../identity/signer.js' |
| 8 | +import { IdentityHandler } from './identity.js' |
| 9 | + |
| 10 | +export class AuthCodeHandler extends IdentityHandler implements Handler { |
| 11 | + protected redirectUri: string = '' |
| 12 | + |
| 13 | + constructor( |
| 14 | + public readonly signupKind: 'apple' | 'google-pkce', |
| 15 | + public readonly issuer: string, |
| 16 | + public readonly audience: string, |
| 17 | + nitro: Identity.IdentityInstrument, |
| 18 | + signatures: Signatures, |
| 19 | + protected readonly commitments: Db.AuthCommitments, |
| 20 | + authKeys: Db.AuthKeys, |
| 21 | + ) { |
| 22 | + super(nitro, authKeys, signatures, Identity.IdentityType.OIDC) |
| 23 | + } |
| 24 | + |
| 25 | + public get kind() { |
| 26 | + return 'login-' + this.signupKind |
| 27 | + } |
| 28 | + |
| 29 | + public setRedirectUri(redirectUri: string) { |
| 30 | + this.redirectUri = redirectUri |
| 31 | + } |
| 32 | + |
| 33 | + public async commitAuth(target: string, isSignUp: boolean, state?: string, signer?: string) { |
| 34 | + if (!state) { |
| 35 | + state = Hex.fromBytes(Bytes.random(32)) |
| 36 | + } |
| 37 | + |
| 38 | + await this.commitments.set({ |
| 39 | + id: state, |
| 40 | + kind: this.signupKind, |
| 41 | + signer, |
| 42 | + target, |
| 43 | + metadata: {}, |
| 44 | + isSignUp, |
| 45 | + }) |
| 46 | + |
| 47 | + const searchParams = new URLSearchParams({ |
| 48 | + client_id: this.audience, |
| 49 | + redirect_uri: this.redirectUri, |
| 50 | + response_type: 'code', |
| 51 | + scope: 'openid', |
| 52 | + state, |
| 53 | + }) |
| 54 | + |
| 55 | + const oauthUrl = this.oauthUrl() |
| 56 | + return `${oauthUrl}?${searchParams.toString()}` |
| 57 | + } |
| 58 | + |
| 59 | + public async completeAuth( |
| 60 | + commitment: Db.AuthCommitment, |
| 61 | + code: string, |
| 62 | + ): Promise<[IdentitySigner, { [key: string]: string }]> { |
| 63 | + let challenge = new Identity.AuthCodeChallenge(this.issuer, this.audience, this.redirectUri, code) |
| 64 | + if (commitment.signer) { |
| 65 | + challenge = challenge.withSigner(commitment.signer) |
| 66 | + } |
| 67 | + await this.nitroCommitVerifier(challenge) |
| 68 | + const signer = await this.nitroCompleteAuth(challenge) |
| 69 | + |
| 70 | + return [signer, {}] |
| 71 | + } |
| 72 | + |
| 73 | + async status( |
| 74 | + address: Address.Address, |
| 75 | + _imageHash: Hex.Hex | undefined, |
| 76 | + request: BaseSignatureRequest, |
| 77 | + ): Promise<SignerUnavailable | SignerReady | SignerActionable> { |
| 78 | + // Normalize address |
| 79 | + const normalizedAddress = Address.checksum(address) |
| 80 | + const signer = await this.getAuthKeySigner(normalizedAddress) |
| 81 | + if (signer) { |
| 82 | + return { |
| 83 | + address: normalizedAddress, |
| 84 | + handler: this, |
| 85 | + status: 'ready', |
| 86 | + handle: async () => { |
| 87 | + await this.sign(signer, request) |
| 88 | + return true |
| 89 | + }, |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + address: normalizedAddress, |
| 95 | + handler: this, |
| 96 | + status: 'actionable', |
| 97 | + message: 'request-redirect', |
| 98 | + handle: async () => { |
| 99 | + const url = await this.commitAuth(window.location.pathname, false, request.id, normalizedAddress) |
| 100 | + window.location.href = url |
| 101 | + return true |
| 102 | + }, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected oauthUrl() { |
| 107 | + switch (this.issuer) { |
| 108 | + case 'https://accounts.google.com': |
| 109 | + return 'https://accounts.google.com/o/oauth2/v2/auth' |
| 110 | + case 'https://appleid.apple.com': |
| 111 | + return 'https://appleid.apple.com/auth/authorize' |
| 112 | + default: |
| 113 | + throw new Error('unsupported-issuer') |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments