|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | +import { createSession, setSessionCookies } from '@/lib/auth/session' |
| 3 | +import { consumeNonce, hasActiveNonce } from '@/lib/auth/store' |
| 4 | +import { sha256Hex } from '@/lib/auth/crypto' |
| 5 | +import { |
| 6 | + buildAuthMessage, |
| 7 | + isValidStellarAddress, |
| 8 | + normalizeWalletAddress, |
| 9 | + verifyStellarSignature, |
| 10 | +} from '@/lib/auth/stellar' |
| 11 | + |
| 12 | +interface VerifyRequestBody { |
| 13 | + walletAddress?: string |
| 14 | + nonce?: string |
| 15 | + signature?: string |
| 16 | + message?: string |
| 17 | +} |
| 18 | + |
| 19 | +export async function POST(request: NextRequest): Promise<NextResponse> { |
| 20 | + try { |
| 21 | + const body: VerifyRequestBody = await request.json() |
| 22 | + |
| 23 | + const walletAddress = body.walletAddress?.trim() |
| 24 | + const nonce = body.nonce?.trim() |
| 25 | + const signature = body.signature?.trim() |
| 26 | + |
| 27 | + if (!walletAddress || !nonce || !signature) { |
| 28 | + return NextResponse.json( |
| 29 | + { |
| 30 | + error: 'Missing walletAddress, nonce, or signature', |
| 31 | + code: 'INVALID_AUTH_PAYLOAD', |
| 32 | + }, |
| 33 | + { status: 400 } |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + if (!isValidStellarAddress(walletAddress)) { |
| 38 | + return NextResponse.json( |
| 39 | + { |
| 40 | + error: 'Invalid wallet address', |
| 41 | + code: 'INVALID_WALLET_ADDRESS', |
| 42 | + }, |
| 43 | + { status: 400 } |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + const normalizedWallet = normalizeWalletAddress(walletAddress) |
| 48 | + const nonceHash = sha256Hex(nonce) |
| 49 | + const expectedMessage = buildAuthMessage(normalizedWallet, nonce) |
| 50 | + |
| 51 | + if (body.message && body.message !== expectedMessage) { |
| 52 | + return NextResponse.json( |
| 53 | + { |
| 54 | + error: 'Signed message does not match expected format', |
| 55 | + code: 'MESSAGE_MISMATCH', |
| 56 | + }, |
| 57 | + { status: 400 } |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + const nonceIsValid = await hasActiveNonce({ |
| 62 | + walletAddress: normalizedWallet, |
| 63 | + nonceHash, |
| 64 | + }) |
| 65 | + if (!nonceIsValid) { |
| 66 | + return NextResponse.json( |
| 67 | + { |
| 68 | + error: 'Nonce is invalid, expired, or already used', |
| 69 | + code: 'INVALID_NONCE', |
| 70 | + }, |
| 71 | + { status: 401 } |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + const signatureIsValid = verifyStellarSignature({ |
| 76 | + walletAddress: normalizedWallet, |
| 77 | + message: expectedMessage, |
| 78 | + signature, |
| 79 | + }) |
| 80 | + if (!signatureIsValid) { |
| 81 | + return NextResponse.json( |
| 82 | + { |
| 83 | + error: 'Invalid wallet signature', |
| 84 | + code: 'INVALID_SIGNATURE', |
| 85 | + }, |
| 86 | + { status: 401 } |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + const consumed = await consumeNonce({ |
| 91 | + walletAddress: normalizedWallet, |
| 92 | + nonceHash, |
| 93 | + }) |
| 94 | + if (!consumed) { |
| 95 | + return NextResponse.json( |
| 96 | + { |
| 97 | + error: 'Nonce is invalid, expired, or already used', |
| 98 | + code: 'INVALID_NONCE', |
| 99 | + }, |
| 100 | + { status: 401 } |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + const session = await createSession(request, normalizedWallet) |
| 105 | + const response = NextResponse.json( |
| 106 | + { |
| 107 | + walletAddress: normalizedWallet, |
| 108 | + accessTokenExpiresAt: session.accessTokenExpiresAt.toISOString(), |
| 109 | + refreshTokenExpiresAt: session.refreshTokenExpiresAt.toISOString(), |
| 110 | + }, |
| 111 | + { status: 200 } |
| 112 | + ) |
| 113 | + setSessionCookies(response, session) |
| 114 | + |
| 115 | + return response |
| 116 | + } catch { |
| 117 | + return NextResponse.json( |
| 118 | + { |
| 119 | + error: 'Authentication failed', |
| 120 | + code: 'AUTH_VERIFICATION_FAILED', |
| 121 | + }, |
| 122 | + { status: 500 } |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
0 commit comments