|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Get, |
| 5 | + Post, |
| 6 | + Query, |
| 7 | + Response, |
| 8 | + UseGuards, |
| 9 | +} from '@nestjs/common' |
| 10 | +import { Octokit } from '@octokit/rest' |
| 11 | +import { IResponse } from 'src/utils/interface' |
| 12 | +import { createOAuthAppAuth } from '@octokit/auth-oauth-app' |
| 13 | +import { AuthenticationService } from '../authentication.service' |
| 14 | +import { UserService } from 'src/user/user.service' |
| 15 | +import { ApiResponseObject, ResponseUtil } from 'src/utils/response' |
| 16 | +import { GithubService } from './github.service' |
| 17 | +import { InjectUser } from 'src/utils/decorator' |
| 18 | +import { User, UserWithProfile } from 'src/user/entities/user' |
| 19 | +import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger' |
| 20 | +import { GithubJumpLoginDto } from '../dto/github-jump-login.dto' |
| 21 | +import { JwtAuthGuard } from '../jwt.auth.guard' |
| 22 | +import { GithubBind } from '../dto/github-bind.dto' |
| 23 | +import { GithubSigninDto } from '../dto/github-signin.dto' |
| 24 | +import { AuthProviderState } from '../entities/auth-provider' |
| 25 | + |
| 26 | +@ApiTags('Authentication') |
| 27 | +@Controller('auth/github') |
| 28 | +export class GithubAuthController { |
| 29 | + constructor( |
| 30 | + private readonly authService: AuthenticationService, |
| 31 | + private readonly userService: UserService, |
| 32 | + private readonly githubService: GithubService, |
| 33 | + ) {} |
| 34 | + |
| 35 | + @ApiOperation({ summary: 'Redirect to the login page of github' }) |
| 36 | + @Get('jump_login') |
| 37 | + async jumpLogin( |
| 38 | + @Query() dto: GithubJumpLoginDto, |
| 39 | + @Response() res: IResponse, |
| 40 | + ) { |
| 41 | + const provider = await this.authService.getGithubProvider() |
| 42 | + if (provider.state !== AuthProviderState.Enabled) { |
| 43 | + return ResponseUtil.error('github signin not allowed') |
| 44 | + } |
| 45 | + |
| 46 | + res.redirect( |
| 47 | + `https://github.com/login/oauth/authorize?client_id=${ |
| 48 | + provider.config.clientId |
| 49 | + }&redirect_uri=${encodeURIComponent(dto.redirectUri)}`, |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + @ApiOperation({ summary: 'Signin by github' }) |
| 54 | + @ApiResponse({ type: ResponseUtil }) |
| 55 | + @Post('signin') |
| 56 | + async signin(@Body() dto: GithubSigninDto) { |
| 57 | + const provider = await this.authService.getGithubProvider() |
| 58 | + if (provider.state !== AuthProviderState.Enabled) { |
| 59 | + return ResponseUtil.error('github signin not allowed') |
| 60 | + } |
| 61 | + |
| 62 | + const githubAuth = createOAuthAppAuth({ |
| 63 | + clientId: provider.config.clientId, |
| 64 | + clientSecret: provider.config.clientSecret, |
| 65 | + clientType: 'oauth-app', |
| 66 | + }) |
| 67 | + |
| 68 | + let auth |
| 69 | + try { |
| 70 | + auth = await githubAuth({ |
| 71 | + type: 'oauth-user', |
| 72 | + code: dto.code, |
| 73 | + }) |
| 74 | + } catch (e) { |
| 75 | + console.log(e) |
| 76 | + return ResponseUtil.error(e.message) |
| 77 | + } |
| 78 | + |
| 79 | + if (!auth) { |
| 80 | + return ResponseUtil.error('github auth failed') |
| 81 | + } |
| 82 | + |
| 83 | + const octokit = new Octokit({ |
| 84 | + auth: auth.token, |
| 85 | + }) |
| 86 | + |
| 87 | + const _profile = await octokit.rest.users.getAuthenticated() |
| 88 | + if (!_profile.data) { |
| 89 | + return ResponseUtil.error('github auth failed') |
| 90 | + } |
| 91 | + |
| 92 | + const githubProfile = { |
| 93 | + gid: _profile.data.id, |
| 94 | + name: _profile.data.name, |
| 95 | + avatar: _profile.data.avatar_url, |
| 96 | + } |
| 97 | + |
| 98 | + const user = await this.userService.findOneByGithub(githubProfile.gid) |
| 99 | + if (!user) { |
| 100 | + const token = this.githubService.signGithubTemporaryToken(githubProfile) |
| 101 | + return ResponseUtil.build(token, 'should bind user') |
| 102 | + } |
| 103 | + |
| 104 | + const token = this.authService.getAccessTokenByUser(user) |
| 105 | + return ResponseUtil.ok(token) |
| 106 | + } |
| 107 | + |
| 108 | + @ApiOperation({ summary: 'Bind github' }) |
| 109 | + @ApiResponseObject(UserWithProfile) |
| 110 | + @UseGuards(JwtAuthGuard) |
| 111 | + @Post('bind') |
| 112 | + async bind(@Body() dto: GithubBind, @InjectUser() user: User) { |
| 113 | + const [ok, githubProfile] = this.githubService.verifyGithubTemporaryToken( |
| 114 | + dto.token, |
| 115 | + ) |
| 116 | + if (!ok) { |
| 117 | + return ResponseUtil.error('invalid token') |
| 118 | + } |
| 119 | + |
| 120 | + if (user.github) { |
| 121 | + return ResponseUtil.error('duplicate bindings to github') |
| 122 | + } |
| 123 | + |
| 124 | + const _user = await this.userService.findOneByGithub(githubProfile.gid) |
| 125 | + if (_user) return ResponseUtil.error('user has been bound') |
| 126 | + |
| 127 | + await this.userService.updateUser(user._id, { |
| 128 | + github: githubProfile.gid, |
| 129 | + }) |
| 130 | + |
| 131 | + if (dto.isRegister) { |
| 132 | + await this.userService.updateAvatarUrl(githubProfile.avatar, user._id) |
| 133 | + } |
| 134 | + |
| 135 | + const res = await this.userService.findOneById(user._id) |
| 136 | + return ResponseUtil.ok(res) |
| 137 | + } |
| 138 | + |
| 139 | + @ApiOperation({ summary: 'Unbind github' }) |
| 140 | + @ApiResponseObject(UserWithProfile) |
| 141 | + @UseGuards(JwtAuthGuard) |
| 142 | + @Post('unbind') |
| 143 | + async unbind(@InjectUser() user: User) { |
| 144 | + if (!user.github) { |
| 145 | + return ResponseUtil.error('not yet bound to github') |
| 146 | + } |
| 147 | + |
| 148 | + const res = await this.userService.updateUser(user._id, { |
| 149 | + github: null, |
| 150 | + }) |
| 151 | + return ResponseUtil.ok(res) |
| 152 | + } |
| 153 | +} |
0 commit comments