|
| 1 | +import { |
| 2 | + createBitcoinWalletAccessToken, |
| 3 | + readOAuthClientCredentials, |
| 4 | + validateOAuthClientCredentials, |
| 5 | + verifyBitcoinWalletAuthorizationCode, |
| 6 | + verifyPkce, |
| 7 | +} from '@/lib/bitcoin-wallet-oauth-provider'; |
| 8 | + |
| 9 | +export const runtime = 'nodejs'; |
| 10 | + |
| 11 | +function readString(value: FormDataEntryValue | null) { |
| 12 | + return typeof value === 'string' && value.trim() ? value.trim() : null; |
| 13 | +} |
| 14 | + |
| 15 | +function jsonResponse(body: unknown, init?: ResponseInit) { |
| 16 | + return new Response(JSON.stringify(body), { |
| 17 | + ...init, |
| 18 | + headers: { |
| 19 | + 'Content-Type': 'application/json', |
| 20 | + ...(init?.headers ?? {}), |
| 21 | + }, |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +async function readBody(request: Request) { |
| 26 | + const contentType = request.headers.get('content-type') ?? ''; |
| 27 | + if (contentType.includes('application/json')) { |
| 28 | + const json = await request.json(); |
| 29 | + return new URLSearchParams( |
| 30 | + Object.entries(json && typeof json === 'object' ? json : {}).map(([key, value]) => [ |
| 31 | + key, |
| 32 | + typeof value === 'string' ? value : value == null ? '' : String(value), |
| 33 | + ]), |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + return new URLSearchParams(await request.text()); |
| 38 | +} |
| 39 | + |
| 40 | +export async function POST(request: Request) { |
| 41 | + let body: URLSearchParams; |
| 42 | + try { |
| 43 | + body = await readBody(request); |
| 44 | + } catch { |
| 45 | + return jsonResponse({ error: 'invalid_request', error_description: 'Invalid OAuth token body.' }, { status: 400 }); |
| 46 | + } |
| 47 | + |
| 48 | + const credentials = readOAuthClientCredentials(request, body); |
| 49 | + if (!validateOAuthClientCredentials(credentials)) { |
| 50 | + return jsonResponse({ error: 'invalid_client' }, { status: 401 }); |
| 51 | + } |
| 52 | + |
| 53 | + if (body.get('grant_type') !== 'authorization_code') { |
| 54 | + return jsonResponse({ error: 'unsupported_grant_type' }, { status: 400 }); |
| 55 | + } |
| 56 | + |
| 57 | + const code = body.get('code'); |
| 58 | + if (!code) { |
| 59 | + return jsonResponse({ error: 'invalid_request', error_description: 'Missing authorization code.' }, { status: 400 }); |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + const codePayload = verifyBitcoinWalletAuthorizationCode(code); |
| 64 | + const redirectUri = readString(body.get('redirect_uri')); |
| 65 | + if (redirectUri && redirectUri !== codePayload.redirect_uri) { |
| 66 | + return jsonResponse({ error: 'invalid_grant', error_description: 'Redirect URI mismatch.' }, { status: 400 }); |
| 67 | + } |
| 68 | + |
| 69 | + if (!verifyPkce({ |
| 70 | + codeChallenge: codePayload.code_challenge, |
| 71 | + codeChallengeMethod: codePayload.code_challenge_method, |
| 72 | + codeVerifier: body.get('code_verifier'), |
| 73 | + })) { |
| 74 | + return jsonResponse({ error: 'invalid_grant', error_description: 'PKCE verification failed.' }, { status: 400 }); |
| 75 | + } |
| 76 | + |
| 77 | + const token = createBitcoinWalletAccessToken({ |
| 78 | + codePayload, |
| 79 | + scope: body.get('scope'), |
| 80 | + }); |
| 81 | + |
| 82 | + return jsonResponse({ |
| 83 | + access_token: token.accessToken, |
| 84 | + token_type: 'Bearer', |
| 85 | + expires_in: token.expiresIn, |
| 86 | + scope: codePayload.scope, |
| 87 | + }); |
| 88 | + } catch (error) { |
| 89 | + return jsonResponse( |
| 90 | + { |
| 91 | + error: 'invalid_grant', |
| 92 | + error_description: error instanceof Error ? error.message : 'Authorization code is invalid.', |
| 93 | + }, |
| 94 | + { status: 400 }, |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments