Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/src/user/controller/oAuth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class OAuthController {
@Res() res: Response,
) {
await this.oauthService.callback(callbackDto, res);
return res.redirect(`${OAUTH_URL_PATH.BASE_URL}/oauth-success`);
return res;
}

@Get('e2e/callback')
Expand Down
24 changes: 19 additions & 5 deletions server/src/user/dto/request/oAuthCallbackDto.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

import { IsNotEmpty, IsString } from 'class-validator';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';

export class OAuthCallbackRequestDto {
@ApiProperty({
@ApiPropertyOptional({
example: 'testCode',
description: 'Access Token ๊ฐฑ์‹  ํ† ํฐ',
})
@IsOptional()
@IsNotEmpty({
message: 'code๋Š” ํ•„์ˆ˜ ์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.',
message: 'code๋Š” ๋นˆ ๋ฌธ์ž์—ด์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.',
})
@IsString({
message: '๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.',
})
code: string;
code?: string;

@ApiProperty({
example: '{ provider: {ํ”Œ๋žซํผ ์ข…๋ฅ˜}}',
Expand All @@ -27,6 +28,19 @@ export class OAuthCallbackRequestDto {
})
state: string;

@ApiPropertyOptional({
example: 'error=access_denied',
description: 'OAuth ์„œ๋ฒ„ ๋กœ๊ทธ์ธ ์ค‘ ์‹คํŒจ ๋ฐœ์ƒ ์‹œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’',
})
@IsOptional()
@IsNotEmpty({
message: 'error๋Š” ๋นˆ ๋ฌธ์ž์—ด์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.',
})
@IsString({
message: '๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.',
})
error?: string;

constructor(partial: Partial<OAuthCallbackRequestDto>) {
Object.assign(this, partial);
}
Expand Down
12 changes: 11 additions & 1 deletion server/src/user/service/oAuth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import { cookieConfig } from '@common/cookie/cookie.config';
import { Payload } from '@common/guard/jwt.guard';
import { WinstonLoggerService } from '@common/logger/logger.service';

import { OAuthType, StateData } from '@user/constant/oauth.constant';
import {
OAUTH_URL_PATH,
OAuthType,
StateData,
} from '@user/constant/oauth.constant';
import { REFRESH_TOKEN_TTL } from '@user/constant/user.constants';
import { OAuthCallbackRequestDto } from '@user/dto/request/oAuthCallbackDto';
import { Provider } from '@user/entity/provider.entity';
Expand Down Expand Up @@ -43,6 +47,10 @@ export class OAuthService {
const stateData = this.parseStateData(callbackDto.state);
const { provider: providerType } = stateData;

if (callbackDto.error || !callbackDto.code) {
return res.redirect(`${OAUTH_URL_PATH.BASE_URL}/signin`);
}

const tokenData = await this.providers[providerType].getTokens(
callbackDto.code,
);
Expand All @@ -60,6 +68,8 @@ export class OAuthService {
},
res,
);

return res.redirect(`${OAUTH_URL_PATH.BASE_URL}/oauth-success`);
}

async e2eCallback(providerType: OAuthType, res: Response) {
Expand Down
53 changes: 39 additions & 14 deletions server/test/oauth/dto/oauth-callback.dto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ describe('OAuthCallbackRequestDto Test', () => {
});

describe('code', () => {
it('code๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ์‹คํŒจํ•œ๋‹ค.', async () => {
// given
dto.code = null;

// when
const errors = await validate(dto);

// then
expect(errors).toHaveLength(1);
expect(errors[0].constraints).toHaveProperty('isNotEmpty');
});

it('code๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์ผ ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ์‹คํŒจํ•œ๋‹ค.', async () => {
// given
dto.code = '';
Expand Down Expand Up @@ -89,10 +77,47 @@ describe('OAuthCallbackRequestDto Test', () => {
// given
dto.state = 1 as any;

// when
//when
const errors = await validate(dto);

// then
//then
expect(errors).toHaveLength(1);
expect(errors[0].constraints).toHaveProperty('isString');
});
});

describe('error', () => {
it('error๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ์„ฑ๊ณตํ•œ๋‹ค.', async () => {
//given
dto.error = 'access_denied';

//when
const errors = await validate(dto);

//then
expect(errors).toHaveLength(0);
});

it('error๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์ผ ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ์‹คํŒจํ•œ๋‹ค.', async () => {
//given
dto.error = '';

//when
const errors = await validate(dto);

//then
expect(errors).toHaveLength(1);
expect(errors[0].constraints).toHaveProperty('isNotEmpty');
});

it('error๊ฐ€ ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ์ˆซ์ž์ผ ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ์‹คํŒจํ•œ๋‹ค.', async () => {
//given
dto.error = 1 as any;

//when
const errors = await validate(dto);

//then
expect(errors).toHaveLength(1);
expect(errors[0].constraints).toHaveProperty('isString');
});
Expand Down