forked from veridatum-labs/earnproof-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.controller.ts
More file actions
160 lines (153 loc) · 5.12 KB
/
Copy pathauth.controller.ts
File metadata and controls
160 lines (153 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Body, Controller, Get, HttpCode, HttpStatus, Post, UseGuards } from "@nestjs/common";
import {
ApiBearerAuth,
ApiOperation,
ApiResponse,
ApiTags,
} from "@nestjs/swagger";
import { CurrentUser } from "../common/decorators/current-user.decorator";
import { ApiErrorDto } from "../common/dto/api-error.dto";
import { AuthGuard } from "../common/guards/auth.guard";
import { AuthenticatedSession } from "./auth.types";
import { AuthService } from "./auth.service";
import { ChallengeResponseDto } from "./dto/challenge-response.dto";
import { CreateChallengeDto } from "./dto/create-challenge.dto";
import { LogoutResponseDto } from "./dto/logout-response.dto";
import { RotateResponseDto } from "./dto/rotate-response.dto";
import { SessionResponseDto } from "./dto/session-response.dto";
import { VerifyChallengeDto } from "./dto/verify-challenge.dto";
import { VerifyResponseDto } from "./dto/verify-response.dto";
import { SessionService } from "./session.service";
@ApiTags("auth")
@Controller("auth")
export class AuthController {
constructor(
private readonly authService: AuthService,
private readonly sessionService: SessionService,
) {}
@ApiOperation({
summary: "Request a wallet challenge",
description:
"Returns a message that the client must sign with the Stellar wallet identified by " +
"`walletAddress`. The challenge expires in 5 minutes and can be used only once.",
})
@ApiResponse({
status: HttpStatus.CREATED,
description: "Challenge created successfully.",
type: ChallengeResponseDto,
})
@ApiResponse({
status: HttpStatus.UNPROCESSABLE_ENTITY,
description: "Request body failed validation.",
type: ApiErrorDto,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: "The wallet address is not a valid Stellar Ed25519 public key.",
type: ApiErrorDto,
})
@Post("challenge")
createChallenge(@Body() body: CreateChallengeDto) {
return this.authService.createChallenge(body.walletAddress);
}
@ApiOperation({
summary: "Verify a wallet signature and obtain a session token",
description:
"Verifies the Ed25519 signature over the challenge message and, if valid, returns a " +
"Bearer token scoped to the authenticated wallet. The challenge is consumed and cannot " +
"be replayed.",
})
@ApiResponse({
status: HttpStatus.CREATED,
description: "Signature verified. Session token issued.",
type: VerifyResponseDto,
})
@ApiResponse({
status: HttpStatus.UNPROCESSABLE_ENTITY,
description: "Request body failed validation.",
type: ApiErrorDto,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: "The wallet address is not a valid Stellar Ed25519 public key.",
type: ApiErrorDto,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: "Challenge expired/used, or wallet signature is invalid.",
type: ApiErrorDto,
})
@Post("verify")
verifyChallenge(@Body() body: VerifyChallengeDto) {
return this.authService.verifyChallenge(body);
}
@ApiOperation({
summary: "Return the current session user",
description: "Returns the full profile of the authenticated user from the database.",
})
@ApiBearerAuth()
@ApiResponse({
status: HttpStatus.OK,
description: "Current session details.",
type: SessionResponseDto,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: "Bearer token is missing, malformed, invalid, or expired.",
type: ApiErrorDto,
})
@UseGuards(AuthGuard)
@Get("session")
getSession(@CurrentUser() session: AuthenticatedSession) {
return this.authService.getSession(session.id);
}
@ApiOperation({
summary: "Log out and revoke the active session",
description:
"Revokes the authenticated session server-side so its bearer token cannot be reused.",
})
@ApiResponse({
status: HttpStatus.OK,
description: "Session revoked successfully.",
type: LogoutResponseDto,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: "Bearer token is missing, malformed, invalid, expired, or revoked.",
type: ApiErrorDto,
})
@ApiBearerAuth()
@UseGuards(AuthGuard)
@HttpCode(HttpStatus.OK)
@Post("logout")
async logout(@CurrentUser() session: AuthenticatedSession) {
await this.authService.logout(session.sessionId);
return { status: "ok" };
}
@ApiOperation({
summary: "Rotate the active session",
description:
"Atomically revokes the current session and returns a fresh opaque bearer token.",
})
@ApiResponse({
status: HttpStatus.OK,
description: "Session rotated successfully.",
type: RotateResponseDto,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: "The active session is unavailable, expired, or already revoked.",
type: ApiErrorDto,
})
@ApiBearerAuth()
@UseGuards(AuthGuard)
@HttpCode(HttpStatus.OK)
@Post("rotate")
async rotate(@CurrentUser() session: AuthenticatedSession) {
const { token, sessionId, expiresAt } = await this.sessionService.rotate(
session.sessionId,
session,
);
return { token, tokenType: "Bearer", sessionId, expiresAt };
}
}