generated from WhiteKiwi/nestjs-server-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
500 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export interface Env { | ||
PORT: string; | ||
ENCRYPT_KEY: string; | ||
JWT_SECRET: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
|
||
import { AuthService } from './auth.service'; | ||
|
||
@Controller({ | ||
version: '1', | ||
path: 'auth', | ||
}) | ||
export class AuthController { | ||
constructor(private readonly authService: AuthService) {} | ||
|
||
@Post('login') | ||
async login(@Body() dto: any): Promise<any> { | ||
return await this.authService.login(dto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
|
||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthStrategy } from './auth.strategy'; | ||
import { JwtAuthenticatorModule } from './authenticators'; | ||
|
||
@Module({ | ||
imports: [PassportModule, JwtAuthenticatorModule], | ||
controllers: [AuthController], | ||
providers: [AuthService, AuthStrategy], | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
|
||
import { Authenticator, AUTHENTICATOR_KEY } from './authenticators'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
@Inject(AUTHENTICATOR_KEY) | ||
private readonly authenticator: Authenticator, | ||
) {} | ||
|
||
async login(dto: any) { | ||
return await this.authenticator.auth(dto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Request } from 'express'; | ||
import { Strategy } from 'passport-custom'; | ||
|
||
import { Authenticator, AUTHENTICATOR_KEY } from './authenticators'; | ||
|
||
@Injectable() | ||
export class AuthStrategy extends PassportStrategy(Strategy) { | ||
constructor( | ||
@Inject(AUTHENTICATOR_KEY) | ||
private readonly authenticator: Authenticator, | ||
) { | ||
super(); | ||
} | ||
|
||
async validate(req: Request) { | ||
return await this.authenticator.validate(req); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Request } from 'express'; | ||
|
||
/** | ||
* AuthenticatorModule은 AUTHENTICATOR_KEY exports 해야함 | ||
*/ | ||
export const AUTHENTICATOR_KEY = Symbol('AUTHENTICATOR_KEY'); | ||
|
||
export interface Authenticator { | ||
/** | ||
* dto 인증 후 response data를 반환합니다 | ||
* @throws UnauthorizedException 인증 실패 | ||
* @returns response data | ||
*/ | ||
auth(dto: any): Promise<any>; | ||
|
||
/** | ||
* 요청을 validate 한 후 payload를 반환합니다 | ||
* @throws UnauthorizedException 인증 실패 | ||
*/ | ||
validate(req: Request): any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './jwt'; | ||
export * from './no'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export class AuthDto { | ||
id!: string; | ||
password!: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './jwt.authenticator.module'; |
31 changes: 31 additions & 0 deletions
31
src/modules/auth/authenticators/impl/jwt/jwt.authenticator.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Env } from '@config/env'; | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
|
||
import { AuthDataStorageModule } from '../../../data-storage'; | ||
import { AUTHENTICATOR_KEY } from '../../authenticator'; | ||
import { JwtAuthenticator } from './jwt.authenticator'; | ||
|
||
@Module({ | ||
imports: [ | ||
AuthDataStorageModule, | ||
JwtModule.registerAsync({ | ||
inject: [ConfigService], | ||
useFactory: (configService: ConfigService<Env>) => { | ||
return { | ||
secret: configService.get('JWT_SECRET'), | ||
signOptions: { expiresIn: '90d' }, | ||
}; | ||
}, | ||
}), | ||
], | ||
providers: [ | ||
{ | ||
provide: AUTHENTICATOR_KEY, | ||
useClass: JwtAuthenticator, | ||
}, | ||
], | ||
exports: [AUTHENTICATOR_KEY], | ||
}) | ||
export class JwtAuthenticatorModule {} |
63 changes: 63 additions & 0 deletions
63
src/modules/auth/authenticators/impl/jwt/jwt.authenticator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { Request } from 'express'; | ||
import { omit } from 'lodash'; | ||
import { ExtractJwt, JwtFromRequestFunction } from 'passport-jwt'; | ||
|
||
import { AUTH_DATA_STORAGE_KEY, AuthDataStorage } from '../../../data-storage'; | ||
import { Authenticator } from '../../authenticator'; | ||
import { AuthDto } from './auth.dto'; | ||
|
||
type User = { | ||
id: string; | ||
password: string; | ||
}; | ||
|
||
@Injectable() | ||
export class JwtAuthenticator implements Authenticator { | ||
private readonly jwtFromRequestFunction: JwtFromRequestFunction = | ||
ExtractJwt.fromAuthHeaderAsBearerToken(); | ||
|
||
constructor( | ||
@Inject(AUTH_DATA_STORAGE_KEY) | ||
private readonly dataStorage: AuthDataStorage<User>, | ||
private readonly jwtService: JwtService, | ||
) {} | ||
|
||
async auth(dto: any): Promise<any> { | ||
this.validateDto(dto); | ||
|
||
const user = await this.dataStorage.load(dto.id); | ||
|
||
this.validateUser(dto, user); | ||
|
||
const accessToken = await this.jwtService.signAsync(omit(user, 'password')); | ||
return { accessToken }; | ||
} | ||
|
||
private validateDto(dto: any): asserts dto is AuthDto { | ||
if (typeof dto.id !== 'string') throw new UnauthorizedException(); | ||
if (typeof dto.password !== 'string') throw new UnauthorizedException(); | ||
} | ||
|
||
private validateUser(dto: AuthDto, user?: User): asserts user is User { | ||
if (!user) { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
// TODO: dto.password + salt의 해시랑 user.password 비교 | ||
if (dto.password !== user.password) { | ||
throw new UnauthorizedException(); | ||
} | ||
} | ||
|
||
async validate(req: Request) { | ||
const token = this.jwtFromRequestFunction(req); | ||
|
||
if (!token) { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
return await this.jwtService.verifyAsync(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './no.authenticator.module'; |
15 changes: 15 additions & 0 deletions
15
src/modules/auth/authenticators/impl/no/no.authenticator.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { AUTHENTICATOR_KEY } from '../../authenticator'; | ||
import { NoAuthenticator } from './no.authenticator'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: AUTHENTICATOR_KEY, | ||
useClass: NoAuthenticator, | ||
}, | ||
], | ||
exports: [AUTHENTICATOR_KEY], | ||
}) | ||
export class NoAuthenticatorModule {} |
14 changes: 14 additions & 0 deletions
14
src/modules/auth/authenticators/impl/no/no.authenticator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
|
||
import { Authenticator } from '../../authenticator'; | ||
|
||
@Injectable() | ||
export class NoAuthenticator implements Authenticator { | ||
async auth(): Promise<never> { | ||
throw new NotFoundException(); | ||
} | ||
|
||
async validate() { | ||
return {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './authenticator'; | ||
export * from './impl'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class ConfigAuthGuard extends AuthGuard('custom') {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { AUTH_DATA_STORAGE_KEY } from './auth.data-storage'; | ||
import { SampleDataStorage } from './impl'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: AUTH_DATA_STORAGE_KEY, | ||
useClass: SampleDataStorage, | ||
}, | ||
], | ||
exports: [AUTH_DATA_STORAGE_KEY], | ||
}) | ||
export class AuthDataStorageModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { DataStorage } from '@modules/data-storage'; | ||
|
||
export const AUTH_DATA_STORAGE_KEY = Symbol('AUTH_DATA_STORAGE_KEY'); | ||
|
||
export type AuthDataStorage<User = unknown> = DataStorage<{ | ||
[userId: UserId]: User; | ||
}>; | ||
|
||
// `user_id:${userId}` | ||
export type UserId = string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './sample.data-storage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AuthDataStorage } from '../auth.data-storage'; | ||
|
||
export class SampleDataStorage | ||
implements AuthDataStorage<{ id: string; password: string }> | ||
{ | ||
/** | ||
* | ||
* @param key user id | ||
*/ | ||
async load(key: string) { | ||
void key; | ||
|
||
return { | ||
id: 'sample', | ||
password: 'sample', | ||
}; | ||
} | ||
|
||
async save(): Promise<never> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './auth.data-storage'; | ||
export * from './auth.data-storage.module'; | ||
export * from './impl'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './auth.module'; | ||
export * from './config.auth-guard'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './auth'; | ||
export * from './cloud-config'; |
Oops, something went wrong.