|
| 1 | +import urlcat from 'urlcat' |
| 2 | + |
| 3 | +import { SessionType, type Session } from '../types/Session.js' |
| 4 | +import { fetchJSON } from '../helpers/fetchJSON.js' |
| 5 | +import { BaseSession } from '../Session/Session.js' |
| 6 | + |
| 7 | +export const WARPCAST_ROOT_URL_V2 = 'https://api.warpcast.com/v2' |
| 8 | +export const FAKE_SIGNER_REQUEST_TOKEN = 'fake_signer_request_token' |
| 9 | + |
| 10 | +export enum FarcasterSponsorship { |
| 11 | + Firefly = 'firefly', |
| 12 | +} |
| 13 | + |
| 14 | +export class FarcasterSession extends BaseSession implements Session { |
| 15 | + constructor( |
| 16 | + /** |
| 17 | + * Fid |
| 18 | + */ |
| 19 | + profileId: string, |
| 20 | + /** |
| 21 | + * the private key of the signer |
| 22 | + */ |
| 23 | + token: string, |
| 24 | + createdAt: number, |
| 25 | + expiresAt: number, |
| 26 | + public signerRequestToken?: string, |
| 27 | + public channelToken?: string, |
| 28 | + public sponsorshipSignature?: string, |
| 29 | + public walletAddress?: string, |
| 30 | + ) { |
| 31 | + super(SessionType.Farcaster, profileId, token, createdAt, expiresAt) |
| 32 | + } |
| 33 | + |
| 34 | + override serialize(): `${SessionType}:${string}:${string}:${string}` { |
| 35 | + return [ |
| 36 | + super.serialize(), |
| 37 | + this.signerRequestToken ?? '', |
| 38 | + this.channelToken ?? '', |
| 39 | + this.sponsorshipSignature ?? '', |
| 40 | + ].join(':') as `${SessionType}:${string}:${string}:${string}` |
| 41 | + } |
| 42 | + |
| 43 | + refresh(): Promise<void> { |
| 44 | + throw new Error('Not allowed') |
| 45 | + } |
| 46 | + |
| 47 | + async destroy(): Promise<void> { |
| 48 | + const url = urlcat(WARPCAST_ROOT_URL_V2, '/auth') |
| 49 | + const response = await fetchJSON<{ |
| 50 | + result: { |
| 51 | + success: boolean |
| 52 | + } |
| 53 | + }>(url, { |
| 54 | + method: 'DELETE', |
| 55 | + headers: { |
| 56 | + Authorization: `Bearer ${this.token}`, |
| 57 | + }, |
| 58 | + body: JSON.stringify({ |
| 59 | + method: 'revokeToken', |
| 60 | + params: { |
| 61 | + timestamp: this.createdAt, |
| 62 | + }, |
| 63 | + }), |
| 64 | + }) |
| 65 | + |
| 66 | + // indicate the session is destroyed |
| 67 | + this.expiresAt = 0 |
| 68 | + |
| 69 | + if (!response.result.success) throw new Error('Failed to destroy the session.') |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + static isGrantByPermission( |
| 74 | + session: Session | null, |
| 75 | + // if strict is true, the session must have a valid signer request token |
| 76 | + strict = false, |
| 77 | + ): session is FarcasterSession & { signerRequestToken: string } { |
| 78 | + if (!session) return false |
| 79 | + const token = (session as FarcasterSession).signerRequestToken |
| 80 | + return ( |
| 81 | + session.type === SessionType.Farcaster && |
| 82 | + !!token && |
| 83 | + // strict mode |
| 84 | + (strict ? token !== FAKE_SIGNER_REQUEST_TOKEN : true) |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + static isSponsorship( |
| 89 | + session: Session | null, |
| 90 | + strict = false, |
| 91 | + ): session is FarcasterSession & { signerRequestToken: string; sponsorshipSignature: string } { |
| 92 | + if (!session) return false |
| 93 | + return ( |
| 94 | + FarcasterSession.isGrantByPermission(session, strict) && |
| 95 | + !!(session as FarcasterSession).sponsorshipSignature |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + static isRelayService(session: Session | null): session is FarcasterSession & { channelToken: string } { |
| 100 | + if (!session) return false |
| 101 | + return session.type === 'Farcaster' && !!(session as FarcasterSession).channelToken |
| 102 | + } |
| 103 | + |
| 104 | + static isLoginByWallet(session: Session | null): session is FarcasterSession & { walletAddress: string } { |
| 105 | + if (!session) return false |
| 106 | + return session.type === 'Farcaster' && !!(session as FarcasterSession).walletAddress |
| 107 | + } |
| 108 | +} |
0 commit comments