Skip to content

Commit

Permalink
update: swagger docs
Browse files Browse the repository at this point in the history
  • Loading branch information
siwonpada committed Sep 2, 2024
1 parent d1c24ad commit f63e4f2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { IdPGuard } from './guard/idp.guard';
import { GroupsGuard } from './guard/groups.guard';
import { GetUser } from './decorator/getUser.decorator';
import { User } from '@prisma/client';
import { ApiOAuth2, ApiOperation, ApiTags } from '@nestjs/swagger';
import {
ApiOAuth2,
ApiOkResponse,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { AccessTokenDto } from './dto/res/accessTokenRes.Dto';
import { UserResDto } from './dto/res/userRes.dto';

@ApiTags('auth')
@Controller('auth')
Expand All @@ -22,9 +29,13 @@ export class AuthController {
description:
'idp login에서 callback을 받아서, idp groups token을 발급받습니다.',
})
@ApiOkResponse({
type: AccessTokenDto,
description: 'idp groups token',
})
@Get('callback')
@UseGuards(IdPGuard)
login(@Req() req: any): any {
login(@Req() req: any): AccessTokenDto {
return req.user;
}

Expand All @@ -34,9 +45,13 @@ export class AuthController {
'사용자 정보를 조회합니다. 이떄, 사용자 정보는 groups에 저장된 정보만을 가져옵니다.',
})
@ApiOAuth2(['openid', 'email', 'profile'])
@ApiOkResponse({
type: UserResDto,
description: 'User information',
})
@Get('info')
@UseGuards(GroupsGuard)
getUserInfo(@GetUser() user: User): any {
getUserInfo(@GetUser() user: User): UserResDto {
return user;
}
}
3 changes: 2 additions & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { User } from '@prisma/client';
import { IdpService } from 'src/idp/idp.service';
import { UserService } from 'src/user/user.service';
import { AccessTokenDto } from './dto/res/accessTokenRes.Dto';

@Injectable()
export class AuthService {
Expand All @@ -11,7 +12,7 @@ export class AuthService {
private readonly idpService: IdpService,
) {}

async login(accessToken: string): Promise<{ accessToken: string }> {
async login(accessToken: string): Promise<AccessTokenDto> {
this.logger.log('Login with IDP');
const { uuid, name, email } =
await this.idpService.getUserInfo(accessToken);
Expand Down
6 changes: 6 additions & 0 deletions src/auth/dto/res/accessTokenRes.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class AccessTokenDto {
@ApiProperty()
accessToken: string;
}
16 changes: 16 additions & 0 deletions src/auth/dto/res/userRes.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { User } from '@prisma/client';

export class UserResDto implements User {
@ApiProperty()
uuid: string;

@ApiProperty()
name: string;

@ApiProperty()
email: string;

@ApiProperty()
createdAt: Date;
}
3 changes: 2 additions & 1 deletion src/auth/strategy/idp.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Strategy } from 'passport-oauth2';
import { PassportStrategy } from '@nestjs/passport';
import { ConfigService } from '@nestjs/config';
import { AuthService } from '../auth.service';
import { AccessTokenDto } from '../dto/res/accessTokenRes.Dto';

@Injectable()
export class IdpStrategy extends PassportStrategy(Strategy, 'idp') {
Expand All @@ -20,7 +21,7 @@ export class IdpStrategy extends PassportStrategy(Strategy, 'idp') {
});
}

async validate(accessToken: string): Promise<any> {
async validate(accessToken: string): Promise<AccessTokenDto> {
return this.authService.login(accessToken);
}
}
4 changes: 4 additions & 0 deletions src/external/external.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
import {
ApiBasicAuth,
ApiBearerAuth,
ApiCreatedResponse,
ApiForbiddenResponse,
ApiInternalServerErrorResponse,
ApiOkResponse,
ApiOperation,
ApiTags,
ApiUnauthorizedResponse,
Expand All @@ -28,6 +30,7 @@ export class ExternalController {
'해당 유저가 가입된 그룹 들을 확인할 수 있는 토큰을 생성합니다.',
})
@UseGuards(ClientGuard)
@ApiCreatedResponse({ type: ExternalTokenResDto })
@ApiUnauthorizedResponse()
@ApiForbiddenResponse()
@ApiInternalServerErrorResponse()
Expand All @@ -45,6 +48,7 @@ export class ExternalController {
description: '해당 유저가 가입된 그룹 들에 관한 정보를 가져옵니다.',
})
@UseGuards(ExternalGuard)
@ApiOkResponse({ type: ExternalInfoResDto })
@ApiUnauthorizedResponse()
@ApiInternalServerErrorResponse()
@ApiBearerAuth('external')
Expand Down

0 comments on commit f63e4f2

Please sign in to comment.