| 1 | import { Body, Controller, Get, Post, Query, Req, Res } from '@nestjs/common'; |
| 2 | import { ApiExcludeController } |
| 3 | import { AllowAnonymous } |
| 4 | import type { Request, |
| 5 | |
| 6 | import { sendOAuth } |
| 7 | import { |
| 8 | type AccessTokenRequest, |
| 9 | type AuthorizeQuery, |
| 10 | OauthService, |
| 11 | } from './oauth.service.js'; |
| 12 | |
| 13 | /** GitHub's OAuth flows at its own paths. Each answers form-encoded or JSON, as the client's Accept header asks. */ |
| 14 | @Controller('login') |
| 15 | @ApiExcludeController() |
| 16 | @AllowAnonymous() |
| 17 | export class OauthController |
| 18 | constructor(private readonly |
| 19 | |
| 20 | /** A refusal is a page, fixed text only: nothing from the request reaches it. */ |
| 21 | @Get('oauth/authorize' |
| 22 | async authorize(@ |
| 23 | const result = |
| 24 | if ('redirect' in |
| 25 | res |
| 26 | .status(400) |
| 27 | .type('html') |
| 28 | .send( |
| 29 | `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Authorization failed</title></head><body><h1>Authorization failed</h1><p>${result.refused |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | @Post('device/code' |
| 34 | async deviceCode( |
| 35 | @Body() body |
| 36 | @Req() req |
| 37 | @Res() res |
| 38 | ) { |
| 39 | sendOAuth(req, res |
| 40 | } |
| 41 | |
| 42 | @Post('oauth/access_token' |
| 43 | async accessToken( |
| 44 | @Body() body |
| 45 | @Req() req |
| 46 | @Res() res |
| 47 | ) { |
| 48 | sendOAuth(req, res |
| 49 | } |
| 50 | } |