|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Post, |
| 4 | + Body, |
| 5 | + Get, |
| 6 | + Query, |
| 7 | + Res, |
| 8 | + HttpStatus, |
| 9 | +} from "@nestjs/common"; |
| 10 | +import { Response } from "express"; |
| 11 | +import { AuthService } from "./auth.service"; |
| 12 | +import { Public } from "./public.decorator"; |
| 13 | +import { |
| 14 | + AuthResultQueryDto, |
| 15 | + LogoutQueryDto, |
| 16 | + OAuthCallbackQueryDto, |
| 17 | + RefreshTokenDto, |
| 18 | +} from "./dto"; |
| 19 | + |
| 20 | +/** |
| 21 | + * Thin HTTP layer that exposes the OAuth entrypoints to the frontend. |
| 22 | + * All routes are public because authorization happens via bearer tokens on other controllers. |
| 23 | + */ |
| 24 | +@Controller("auth") |
| 25 | +export class AuthController { |
| 26 | + constructor(private readonly authService: AuthService) {} |
| 27 | + |
| 28 | + /** |
| 29 | + * Backend endpoint the SPA uses to refresh provider tokens. |
| 30 | + * Delegates to AuthService so only the backend interacts with Keycloak using the client secret. |
| 31 | + */ |
| 32 | + @Public() |
| 33 | + @Post("refresh") |
| 34 | + async refreshToken(@Body() body: RefreshTokenDto) { |
| 35 | + const tokens = await this.authService.refreshAccessToken( |
| 36 | + body.refresh_token, |
| 37 | + ); |
| 38 | + return { |
| 39 | + access_token: tokens.access_token, |
| 40 | + refresh_token: tokens.refresh_token, |
| 41 | + id_token: tokens.id_token, |
| 42 | + expires_in: tokens.expires_in, |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Redirects the browser to the Keycloak authorization endpoint. |
| 48 | + * Using a backend redirect keeps client_id/secret pairing server-side (no exposed secrets). |
| 49 | + */ |
| 50 | + @Public() |
| 51 | + @Get("login") |
| 52 | + async getLoginUrl(@Res() res: Response) { |
| 53 | + try { |
| 54 | + const loginUrl = this.authService.getLoginUrl(); |
| 55 | + res.redirect(loginUrl); |
| 56 | + } catch { |
| 57 | + res |
| 58 | + .status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 59 | + .json({ error: "Failed to generate login URL" }); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Drives the browser through the Keycloak logout endpoint (if an ID token hint is provided). |
| 65 | + * This ensures realm sessions are terminated and the SPA gets a clean slate. |
| 66 | + */ |
| 67 | + @Public() |
| 68 | + @Get("logout") |
| 69 | + async logout(@Query() query: LogoutQueryDto, @Res() res: Response) { |
| 70 | + try { |
| 71 | + const logoutUrl = this.authService.getLogoutUrl(query.id_token_hint); |
| 72 | + res.redirect(logoutUrl); |
| 73 | + } catch { |
| 74 | + res |
| 75 | + .status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 76 | + .json({ error: "Failed to generate logout URL" }); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Receives the redirect from Keycloak after the user authenticates. |
| 82 | + * Converts the authorization code into tokens and then bounces the browser back to the SPA |
| 83 | + * with an opaque `auth_result` identifier. |
| 84 | + */ |
| 85 | + @Public() |
| 86 | + @Get("callback") |
| 87 | + async oauthCallback( |
| 88 | + @Query() query: OAuthCallbackQueryDto, |
| 89 | + @Res() res: Response, |
| 90 | + ) { |
| 91 | + try { |
| 92 | + const resultId = await this.authService.handleCallback( |
| 93 | + query.code, |
| 94 | + query.state, |
| 95 | + ); |
| 96 | + const redirectUrl = this.authService.buildAuthResultRedirect(resultId); |
| 97 | + return res.redirect(redirectUrl); |
| 98 | + } catch (error) { |
| 99 | + console.error("OAuth callback handling failed:", error); |
| 100 | + const redirectUrl = |
| 101 | + this.authService.buildErrorRedirect("callback_failed"); |
| 102 | + return res.redirect(redirectUrl); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * One-time endpoint the SPA calls immediately after redirect to retrieve the provider tokens. |
| 108 | + * The `resultId` is invalidated after the first successful read to keep the flow stateless. |
| 109 | + */ |
| 110 | + @Public() |
| 111 | + @Get("result") |
| 112 | + async consumeResult(@Query() query: AuthResultQueryDto) { |
| 113 | + return this.authService.consumeAuthResult(query.result); |
| 114 | + } |
| 115 | +} |
0 commit comments