@@ -6,11 +6,16 @@ import {
66 HttpStatus ,
77 Get ,
88 Param ,
9+ Headers ,
10+ Res ,
11+ UseGuards ,
912} from '@nestjs/common' ;
13+ import { Response } from 'express' ;
1014import {
1115 AuthOrchestrator ,
1216 AuthenticationRequest ,
1317 AuthenticationResult ,
18+ AuthenticationRequestWithIdempotency ,
1419} from './auth-orchestrator.service' ;
1520import { Public } from './public.decorator' ;
1621
@@ -27,17 +32,37 @@ export class AuthOrchestratorController {
2732 * 3. Returns existing user + wallet if already exists
2833 *
2934 * All operations are idempotent.
30- *
31- * @Public - This endpoint must be public as it's used for initial authentication
32- * before an API key is available.
35+ * Supports optional Idempotency-Key header for request deduplication.
36+ * Protected by per-IP rate limiting to prevent brute force attacks.
3337 */
3438 @Post ( 'authenticate' )
35- @Public ( )
39+ @UseGuards ( AuthRateLimitGuard )
3640 @HttpCode ( HttpStatus . OK )
3741 async authenticate (
3842 @Body ( ) request : AuthenticationRequest ,
39- ) : Promise < AuthenticationResult > {
40- return await this . authOrchestrator . handleAuthentication ( request ) ;
43+ @Headers ( 'idempotency-key' ) idempotencyKey : string | undefined ,
44+ @Res ( ) response : Response ,
45+ ) : Promise < void > {
46+ const requestWithIdempotency : AuthenticationRequestWithIdempotency = {
47+ ...request ,
48+ idempotencyKey,
49+ } ;
50+
51+ const result = await this . authOrchestrator . handleAuthentication (
52+ requestWithIdempotency ,
53+ ) ;
54+
55+ // Extract and remove metadata before sending response
56+ const idempotencyReplayed = ( result as any ) . _idempotencyReplayed ?? false ;
57+ const responseBody = { ...result } ;
58+ delete ( responseBody as any ) . _idempotencyReplayed ;
59+
60+ // Set idempotency-replayed header if idempotency key was provided
61+ if ( idempotencyKey ) {
62+ response . setHeader ( 'Idempotency-Replayed' , idempotencyReplayed ? 'true' : 'false' ) ;
63+ }
64+
65+ response . json ( responseBody ) ;
4166 }
4267
4368 /**
0 commit comments