## 概要 [Task2800: API修正(トークン生成API)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2800) 既存のトークン生成APIに、利用規約バージョンのチェック処理を追加しました。 また、チェック処理で同意済みバージョンが最新でないときのエラー時にログアウトしないような処理を追加しました。 ## レビューポイント なし ## UIの変更 なし ## 動作確認状況 UT,ローカルでの動作確認済み ## 補足 なし
139 lines
3.7 KiB
TypeScript
139 lines
3.7 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
HttpException,
|
|
HttpStatus,
|
|
Post,
|
|
Req,
|
|
} from '@nestjs/common';
|
|
import {
|
|
ApiResponse,
|
|
ApiOperation,
|
|
ApiBearerAuth,
|
|
ApiTags,
|
|
} from '@nestjs/swagger';
|
|
import { makeErrorResponse } from '../../common/error/makeErrorResponse';
|
|
import { ErrorResponse } from '../../common/error/types/types';
|
|
import { AuthService } from './auth.service';
|
|
import {
|
|
AccessTokenResponse,
|
|
TokenRequest,
|
|
TokenResponse,
|
|
} from './types/types';
|
|
import { retrieveAuthorizationToken } from '../../common/http/helper';
|
|
import { makeContext } from '../../common/log';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
@ApiTags('auth')
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
constructor(
|
|
// TODO「タスク 1828: IDトークンを一度しか使えないようにする」で使用する予定
|
|
// private readonly redisService: RedisService,
|
|
private readonly authService: AuthService,
|
|
) {}
|
|
|
|
@Post('token')
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: TokenResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー/同意済み利用規約が最新でない場合',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
description:
|
|
'AzureADB2Cでのサインイン後に払いだされるIDトークンを元に認証用のアクセストークンとリフレッシュトークンを生成します',
|
|
operationId: 'token',
|
|
})
|
|
async token(@Body() body: TokenRequest): Promise<TokenResponse> {
|
|
const idToken = await this.authService.getVerifiedIdToken(body.idToken);
|
|
|
|
const isVerified = await this.authService.isVerifiedUser(idToken);
|
|
if (!isVerified) {
|
|
throw new HttpException(
|
|
makeErrorResponse('E010201'),
|
|
HttpStatus.BAD_REQUEST,
|
|
);
|
|
}
|
|
|
|
const context = makeContext(uuidv4());
|
|
|
|
// 同意済み利用規約バージョンが最新かチェック
|
|
const isAcceptedLatestVersion =
|
|
await this.authService.isAcceptedLatestVersion(context, idToken);
|
|
|
|
// 最新でなければエラー
|
|
if (!isAcceptedLatestVersion) {
|
|
throw new HttpException(
|
|
makeErrorResponse('E010209'),
|
|
HttpStatus.UNAUTHORIZED,
|
|
);
|
|
}
|
|
|
|
const refreshToken = await this.authService.generateRefreshToken(
|
|
context,
|
|
idToken,
|
|
body.type,
|
|
);
|
|
|
|
const accessToken = await this.authService.generateAccessToken(
|
|
context,
|
|
refreshToken,
|
|
);
|
|
|
|
return {
|
|
accessToken,
|
|
refreshToken,
|
|
};
|
|
}
|
|
|
|
@Post('accessToken')
|
|
@ApiBearerAuth()
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: AccessTokenResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
operationId: 'accessToken',
|
|
description: 'リフレッシュトークンを元にアクセストークンを再生成します',
|
|
})
|
|
async accessToken(@Req() req): Promise<AccessTokenResponse> {
|
|
const refreshToken = retrieveAuthorizationToken(req);
|
|
|
|
if (!refreshToken) {
|
|
throw new HttpException(
|
|
makeErrorResponse('E000107'),
|
|
HttpStatus.UNAUTHORIZED,
|
|
);
|
|
}
|
|
|
|
const context = makeContext(uuidv4());
|
|
|
|
const accessToken = await this.authService.generateAccessToken(
|
|
context,
|
|
refreshToken,
|
|
);
|
|
return { accessToken };
|
|
}
|
|
}
|