Skip to content

Commit

Permalink
exception and authn
Browse files Browse the repository at this point in the history
  • Loading branch information
wermarter committed Dec 25, 2023
1 parent ba55743 commit 19b398e
Show file tree
Hide file tree
Showing 57 changed files with 344 additions and 281 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './entity'
export * from './unauthenticated'
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { User } from './entity'

export const unauthenticatedUser: User = {
_id: 'unauthenticated',
name: 'Unauthenticated',
username: 'unauthenticated',
password: 'XXXXXXXXXXXXXXX',

createdAt: new Date(),
updatedAt: new Date(),
}

export function isUnauthenticatedUser(user: User) {
return user._id === unauthenticatedUser._id
}

This file was deleted.

This file was deleted.

15 changes: 15 additions & 0 deletions apps/levansy-access-service/src/domain/exception/authn/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpStatus } from '@nestjs/common'
import { DomainErrorCode } from '@diut/levansy-common'

import { EDomain } from '../base'

export class EAuthn extends EDomain {
constructor(
errorCode?: DomainErrorCode,
message?: string,
cause?: unknown,
httpStatus?: HttpStatus,
) {
super(errorCode ?? DomainErrorCode.AUTHN, message, cause, httpStatus)
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './base'
export * from './payload'
export * from './jwt'
15 changes: 15 additions & 0 deletions apps/levansy-access-service/src/domain/exception/authn/jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpStatus } from '@nestjs/common'
import { DomainErrorCode } from '@diut/levansy-common'

import { EAuthn } from './base'

export class EAuthnInvalidJwtToken extends EAuthn {
constructor() {
super(
DomainErrorCode.AUTHN_INVALID_JWT_TOKEN,
'Invalid JWT token',
undefined,
HttpStatus.UNAUTHORIZED,
)
}
}
15 changes: 15 additions & 0 deletions apps/levansy-access-service/src/domain/exception/authn/payload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpStatus } from '@nestjs/common'
import { DomainErrorCode } from '@diut/levansy-common'

import { EAuthn } from './base'

export class EAuthnPayloadNotFound extends EAuthn {
constructor() {
super(
DomainErrorCode.AUTHN_PAYLOAD_NOT_FOUND,
'Payload not found',
undefined,
HttpStatus.BAD_REQUEST,
)
}
}

This file was deleted.

15 changes: 15 additions & 0 deletions apps/levansy-access-service/src/domain/exception/authz/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpStatus } from '@nestjs/common'
import { DomainErrorCode } from '@diut/levansy-common'

import { EDomain } from '../base'

export class EAuthz extends EDomain {
constructor(
errorCode?: DomainErrorCode,
message?: string,
cause?: unknown,
httpStatus?: HttpStatus,
) {
super(errorCode ?? DomainErrorCode.AUTHZ, message, cause, httpStatus)
}
}
12 changes: 9 additions & 3 deletions apps/levansy-access-service/src/domain/exception/base.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { HttpStatus } from '@nestjs/common'
import { DomainErrorCode } from '@diut/levansy-common'

export class EDomain extends Error {
errorCode: string
errorCode: DomainErrorCode
httpStatus?: HttpStatus

constructor(
errorCode: string,
errorCode: DomainErrorCode,
message?: string,
cause?: unknown,
httpStatus?: HttpStatus,
Expand All @@ -19,6 +20,11 @@ export class EDomain extends Error {

export class EUnknown extends EDomain {
constructor(message?: string, cause?: unknown) {
super('E0000', message, cause, HttpStatus.INTERNAL_SERVER_ERROR)
super(
DomainErrorCode.UNKNOWN,
message,
cause,
HttpStatus.INTERNAL_SERVER_ERROR,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { HttpStatus } from '@nestjs/common'
import { DomainErrorCode } from '@diut/levansy-common'

import { EDomain } from '../base'

export class EEntity extends EDomain {
constructor(
errorCode?: string,
errorCode?: DomainErrorCode,
message?: string,
cause?: unknown,
httpStatus?: HttpStatus,
) {
super(errorCode ?? 'E3000', message, cause, httpStatus)
super(errorCode ?? DomainErrorCode.ENTITY, message, cause, httpStatus)
}
}
4 changes: 2 additions & 2 deletions apps/levansy-access-service/src/domain/exception/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './base'
export * from './authentication'
export * from './authorization'
export * from './authn'
export * from './authz'
export * from './entity'
export * from './service'
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { HttpStatus } from '@nestjs/common'
import { DomainErrorCode } from '@diut/levansy-common'

import { EDomain } from '../base'

export class EService extends EDomain {
constructor(
errorCode?: string,
errorCode?: DomainErrorCode,
message?: string,
cause?: unknown,
httpStatus?: HttpStatus,
) {
super(errorCode ?? 'E4000', message, cause, httpStatus)
super(errorCode ?? DomainErrorCode.SERVICE, message, cause, httpStatus)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

16 changes: 16 additions & 0 deletions apps/levansy-access-service/src/domain/interface/authz/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { User } from 'src/domain/entity'

export const AuthzContextToken = Symbol('AuthzContext')

export interface IAuthzContext {
prepareData(authPayload: AuthPayload | undefined): Promise<void>
getData(): ContextData
}

export type AuthPayload = {
userId: string
}

export type ContextData = {
user: User
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './context'
2 changes: 1 addition & 1 deletion apps/levansy-access-service/src/domain/interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './repository'
export * from './service'
export * from './authorization'
export * from './authz'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as argon2 from 'argon2'
import { JwtService } from '@nestjs/jwt'

import { UserFindOneUseCase } from '../user/find-one'
import { AuthPayload } from 'src/infrastructure/authentication'
import { AuthPayload } from 'src/domain/interface'

@Injectable()
export class AuthLoginUseCase {
Expand Down
9 changes: 3 additions & 6 deletions apps/levansy-access-service/src/domain/use-case/auth/me.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Inject, Injectable } from '@nestjs/common'

import {
AuthorizationContextToken,
IAuthorizationContext,
} from 'src/domain/interface'
import { AuthzContextToken, IAuthzContext } from 'src/domain/interface'

@Injectable()
export class AuthMeUseCase {
constructor(
@Inject(AuthorizationContextToken)
private authContext: IAuthorizationContext,
@Inject(AuthzContextToken)
private authContext: IAuthzContext,
) {}

async execute() {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 19b398e

Please sign in to comment.