|
| 1 | +/** |
| 2 | + * CoinPay OAuth provider. Lets a user sign into TronBrowser with their CoinPay |
| 3 | + * account so the browser can authorize x402 payments (see @tronbrowser/payments) |
| 4 | + * from their CoinPay global wallet addresses. |
| 5 | + * |
| 6 | + * Endpoints default to CoinPay's hosted service and are overridable for |
| 7 | + * self-hosted CoinPay deployments. |
| 8 | + */ |
| 9 | + |
| 10 | +import { |
| 11 | + isExpired, |
| 12 | + type OAuthConfig, |
| 13 | + type OAuthProvider, |
| 14 | + type OAuthTokens, |
| 15 | +} from './oauth.js'; |
| 16 | + |
| 17 | +export const COINPAY_DEFAULTS = { |
| 18 | + authorizeUrl: 'https://coinpay.profullstack.com/oauth/authorize', |
| 19 | + tokenUrl: 'https://coinpay.profullstack.com/oauth/token', |
| 20 | + /** Scopes needed to read wallet addresses and authorize x402 payments. */ |
| 21 | + scopes: ['wallet:read', 'payments:x402'], |
| 22 | +} as const; |
| 23 | + |
| 24 | +export interface CoinPayOAuthConfig { |
| 25 | + clientId: string; |
| 26 | + redirectUri: string; |
| 27 | + /** Override for self-hosted CoinPay; defaults to the hosted service. */ |
| 28 | + authorizeUrl?: string; |
| 29 | + tokenUrl?: string; |
| 30 | + scopes?: string[]; |
| 31 | +} |
| 32 | + |
| 33 | +/** Resolves user-supplied config against CoinPay defaults. */ |
| 34 | +export function resolveCoinPayConfig(cfg: CoinPayOAuthConfig): OAuthConfig { |
| 35 | + return { |
| 36 | + clientId: cfg.clientId, |
| 37 | + redirectUri: cfg.redirectUri, |
| 38 | + authorizeUrl: cfg.authorizeUrl ?? COINPAY_DEFAULTS.authorizeUrl, |
| 39 | + tokenUrl: cfg.tokenUrl ?? COINPAY_DEFAULTS.tokenUrl, |
| 40 | + scopes: cfg.scopes ?? [...COINPAY_DEFAULTS.scopes], |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * CoinPay OAuth provider (Authorization Code + PKCE). Network calls are stubbed |
| 46 | + * until M2; URL construction is implemented and tested now. |
| 47 | + */ |
| 48 | +export class CoinPayOAuthProvider implements OAuthProvider { |
| 49 | + readonly config: OAuthConfig; |
| 50 | + |
| 51 | + constructor(config: CoinPayOAuthConfig) { |
| 52 | + this.config = resolveCoinPayConfig(config); |
| 53 | + } |
| 54 | + |
| 55 | + authorizeUrl(state: string, codeChallenge: string): string { |
| 56 | + const u = new URL(this.config.authorizeUrl); |
| 57 | + u.searchParams.set('response_type', 'code'); |
| 58 | + u.searchParams.set('client_id', this.config.clientId); |
| 59 | + u.searchParams.set('redirect_uri', this.config.redirectUri); |
| 60 | + u.searchParams.set('scope', this.config.scopes.join(' ')); |
| 61 | + u.searchParams.set('state', state); |
| 62 | + u.searchParams.set('code_challenge', codeChallenge); |
| 63 | + u.searchParams.set('code_challenge_method', 'S256'); |
| 64 | + return u.toString(); |
| 65 | + } |
| 66 | + |
| 67 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 68 | + async exchangeCode(_code: string, _codeVerifier: string): Promise<OAuthTokens> { |
| 69 | + throw new Error('CoinPayOAuthProvider.exchangeCode: not implemented (M2)'); |
| 70 | + } |
| 71 | + |
| 72 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 73 | + async refresh(_refreshToken: string): Promise<OAuthTokens> { |
| 74 | + throw new Error('CoinPayOAuthProvider.refresh: not implemented (M2)'); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +export { isExpired }; |
0 commit comments