-
Notifications
You must be signed in to change notification settings - Fork 0
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
21 changed files
with
157 additions
and
82 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
61 changes: 61 additions & 0 deletions
61
apps/levansy-access-service/src/presentation/http/common/auth/cookie.service.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,61 @@ | ||
import { NodeEnv } from '@diut/common' | ||
import { Inject, Injectable } from '@nestjs/common' | ||
import { CookieOptions, Response, Request } from 'express' | ||
|
||
import { | ||
AppConfig, | ||
AuthConfig, | ||
loadAppConfig, | ||
loadAuthConfig, | ||
} from 'src/config' | ||
|
||
export type AuthCookiePayload = { | ||
accessToken: string | ||
} | ||
|
||
@Injectable() | ||
export class AuthCookieService { | ||
private readonly accessTokenCookieName: string | ||
|
||
constructor( | ||
@Inject(loadAuthConfig.KEY) private authConfig: AuthConfig, | ||
@Inject(loadAppConfig.KEY) private appConfig: AppConfig, | ||
) { | ||
this.accessTokenCookieName = `${appConfig.SERVICE_NAME}-access_token` | ||
} | ||
|
||
private makeCookieOptions(): CookieOptions { | ||
const isDevelopment = this.appConfig.NODE_ENV === NodeEnv.Development | ||
|
||
return { | ||
signed: true, | ||
httpOnly: true, | ||
secure: !isDevelopment, | ||
sameSite: 'lax', | ||
expires: new Date( | ||
Date.now() + parseInt(this.authConfig.AUTH_JWT_EXPIRE_SECONDS) * 1000, | ||
), | ||
} | ||
} | ||
|
||
setAuthCookie(res: Response, cookiePayload: AuthCookiePayload) { | ||
this.clearAuthCookie(res) | ||
|
||
const cookieOptions = this.makeCookieOptions() | ||
res.cookie( | ||
this.accessTokenCookieName, | ||
cookiePayload.accessToken, | ||
cookieOptions, | ||
) | ||
} | ||
|
||
getAuthCookie(req: Request): Partial<AuthCookiePayload> { | ||
const accessToken = req.signedCookies[this.accessTokenCookieName] | ||
|
||
return { accessToken } | ||
} | ||
|
||
clearAuthCookie(res: Response) { | ||
res.clearCookie(this.accessTokenCookieName) | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
apps/levansy-access-service/src/presentation/http/common/auth/guards.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { AuthContextGuard } from './context.guard' | ||
import { AuthJwtGuard } from './jwt' | ||
import { HttpAuthContextGuard } from './context.guard' | ||
import { HttpJwtGuard } from './jwt' | ||
|
||
export const authGuards = [AuthJwtGuard, AuthContextGuard] | ||
export const authGuards = [HttpJwtGuard, HttpAuthContextGuard] |
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
2 changes: 0 additions & 2 deletions
2
apps/levansy-access-service/src/presentation/http/common/auth/jwt/common.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 |
---|---|---|
@@ -1,3 +1 @@ | ||
export const AUTH_JWT_STRATEGY_KEY = 'auth.jwt' | ||
|
||
export const AUTH_JWT_ACCESS_TOKEN_COOKIE = 'diut_levansy_access_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
23 changes: 9 additions & 14 deletions
23
apps/levansy-access-service/src/presentation/http/common/auth/jwt/strategy.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
5 changes: 3 additions & 2 deletions
5
apps/levansy-access-service/src/presentation/http/common/auth/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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { ModuleMetadata } from '@nestjs/common' | ||
|
||
import { JwtStrategy } from './jwt' | ||
import { HttpJwtStrategy } from './jwt' | ||
import { AuthCookieService } from './cookie.service' | ||
|
||
export const authMetadata: ModuleMetadata = { | ||
providers: [JwtStrategy], | ||
providers: [HttpJwtStrategy, AuthCookieService], | ||
} |
9 changes: 0 additions & 9 deletions
9
apps/levansy-access-service/src/presentation/http/common/decorator/controller-decorators.ts
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
...evansy-access-service/src/presentation/http/common/decorator/http-controller.decorator.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,21 @@ | ||
import { UseFilters, UseGuards } from '@nestjs/common' | ||
import { | ||
CustomHttpController, | ||
CustomHttpControllerOptions, | ||
} from '@diut/nest-core' | ||
|
||
import { authGuards } from '../auth' | ||
import { exceptionFilters } from '../exception' | ||
|
||
export const HttpController = (options: CustomHttpControllerOptions) => { | ||
const customOptions: CustomHttpControllerOptions = { | ||
...options, | ||
controllerDecorators: [ | ||
...(options?.controllerDecorators ?? []), | ||
UseGuards(...authGuards), | ||
UseFilters(...exceptionFilters), | ||
], | ||
} | ||
|
||
return CustomHttpController(customOptions) | ||
} |
22 changes: 22 additions & 0 deletions
22
apps/levansy-access-service/src/presentation/http/common/decorator/http-route.decorator.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,22 @@ | ||
import { CustomHttpRoute, CustomHttpRouteOptions } from '@diut/nest-core' | ||
|
||
import { HttpErrorResponse } from '../dto' | ||
|
||
export const HttpRoute = (options: CustomHttpRouteOptions) => { | ||
const customOptions: CustomHttpRouteOptions = { | ||
...options, | ||
openApi: { | ||
...options?.openApi, | ||
responses: [ | ||
...(options?.openApi?.responses ?? []), | ||
{ | ||
status: '4XX', | ||
description: 'Custom error', | ||
type: HttpErrorResponse, | ||
}, | ||
], | ||
}, | ||
} | ||
|
||
return CustomHttpRoute(customOptions) | ||
} |
4 changes: 2 additions & 2 deletions
4
apps/levansy-access-service/src/presentation/http/common/decorator/index.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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './controller-decorators' | ||
export * from './route-decorators' | ||
export * from './http-controller.decorator' | ||
export * from './http-route.decorator' |
1 change: 0 additions & 1 deletion
1
apps/levansy-access-service/src/presentation/http/common/decorator/route-decorators.ts
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.