-
Notifications
You must be signed in to change notification settings - Fork 10
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
15 changed files
with
158 additions
and
49 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/application/queries/get-current-user-by-access-token.query.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,5 @@ | ||
import { IJwtPayload } from '../../domain' | ||
|
||
export class GetCurrentUserByAccessTokenQuery { | ||
public constructor(public input: IJwtPayload) {} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/application/queries/get-current-user-by-refresh-token.query.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,3 @@ | ||
export class GetCurrentUserByRefreshTokenQuery { | ||
public constructor(public token: string) {} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/application/queries/handlers/get-current-user-by-access-token.query.handler.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,27 @@ | ||
import { UnauthorizedException } from '@nestjs/common' | ||
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs' | ||
import { lastValueFrom } from 'rxjs' | ||
import { | ||
GetUserByJwtTokenAction, | ||
IAuthUserEntityForResponse, | ||
} from '../../../domain' | ||
import { GetCurrentUserByAccessTokenQuery } from '../get-current-user-by-access-token.query' | ||
|
||
@QueryHandler(GetCurrentUserByAccessTokenQuery) | ||
export class GetCurrentUserByAccessTokenQueryHandler | ||
implements | ||
IQueryHandler< | ||
GetCurrentUserByAccessTokenQuery, | ||
IAuthUserEntityForResponse | undefined | ||
> | ||
{ | ||
public constructor(protected readonly action: GetUserByJwtTokenAction) {} | ||
|
||
public async execute(query: GetCurrentUserByAccessTokenQuery) { | ||
try { | ||
return await lastValueFrom(this.action.handle(query.input)) | ||
} catch (error) { | ||
throw new UnauthorizedException() | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/application/queries/handlers/get-current-user-by-refresh-token.query.handler.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,41 @@ | ||
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs' | ||
import { lastValueFrom, map } from 'rxjs' | ||
import { | ||
AuthRepository, | ||
hideRedactedFields, | ||
IAuthDefinitions, | ||
IAuthUserEntityForResponse, | ||
InjectAuthDefinitions, | ||
TokenService, | ||
} from '../../../domain' | ||
import { GetCurrentUserByRefreshTokenQuery } from '../get-current-user-by-refresh-token.query' | ||
|
||
@QueryHandler(GetCurrentUserByRefreshTokenQuery) | ||
export class GetCurrentUserByRefreshTokenQueryHandler | ||
implements | ||
IQueryHandler< | ||
GetCurrentUserByRefreshTokenQuery, | ||
IAuthUserEntityForResponse | undefined | ||
> | ||
{ | ||
public constructor( | ||
@InjectAuthDefinitions() | ||
protected readonly authDefinitions: IAuthDefinitions, | ||
protected readonly authRepository: AuthRepository, | ||
protected readonly jwtService: TokenService | ||
) {} | ||
|
||
public async execute(query: GetCurrentUserByRefreshTokenQuery) { | ||
const jwtPayload = query.token | ||
? this.jwtService.decodeRefreshToken(query.token) | ||
: undefined | ||
|
||
return jwtPayload | ||
? await lastValueFrom( | ||
this.authRepository | ||
.getAuthUserByUsername(jwtPayload.username) | ||
.pipe(map(hideRedactedFields(this.authDefinitions.redactedFields))) | ||
) | ||
: undefined | ||
} | ||
} |
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 './get-current-user-by-access-token.query.handler' | ||
export * from './get-current-user-by-refresh-token.query.handler' | ||
export * from './login.query.handler' |
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 { IQueryHandler, QueryHandler } from '@nestjs/cqrs' | ||
import { lastValueFrom } from 'rxjs' | ||
import { IAuthUserEntityForResponse, LocalLoginAction } from '../../../domain' | ||
import { LoginQuery } from '../login.query' | ||
|
||
@QueryHandler(LoginQuery) | ||
export class LoginQueryHandler | ||
implements IQueryHandler<LoginQuery, IAuthUserEntityForResponse> { | ||
public constructor(protected readonly action: LocalLoginAction) { | ||
} | ||
|
||
public async execute(query: LoginQuery) { | ||
return lastValueFrom(this.action.handle(query.input)) | ||
} | ||
} |
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 './get-current-user-by-access-token.query' | ||
export * from './get-current-user-by-refresh-token.query' | ||
export * from './login.query' |
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 { AuthDto } from '../../domain' | ||
|
||
export class LoginQuery { | ||
public constructor(public input: AuthDto) {} | ||
} |
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 @@ | ||
import { HttpStatus, UnauthorizedException } from '@nestjs/common' | ||
import { TestingModule } from '@nestjs/testing/testing-module' | ||
import { lastValueFrom, map } from 'rxjs' | ||
import { | ||
createTestingModule, | ||
|
@@ -12,13 +13,17 @@ import { generateExecutionContextForJwtStrategy } from './helpers/test-guard.hel | |
describe('AuthJwtGuard', () => { | ||
let correctAccessToken: string | ||
|
||
let app: TestingModule | ||
|
||
beforeAll(async () => { | ||
const userInfo: IAuthUserEntityForResponse = { | ||
id: '123456', | ||
username: '[email protected]', | ||
} | ||
|
||
const app = await createTestingModule(defaultAuthDefinitionsFixture()) | ||
app = await createTestingModule(defaultAuthDefinitionsFixture()) | ||
|
||
await app.init() | ||
|
||
const service = app.get(TokenService) | ||
|
||
|
@@ -30,7 +35,7 @@ describe('AuthJwtGuard', () => { | |
}) | ||
|
||
it('should allow user to continue when accessToken is correct', async function () { | ||
const guard = new AuthJwtGuard() | ||
const guard = app.get(AuthJwtGuard) | ||
|
||
expect( | ||
await guard.canActivate( | ||
|
@@ -40,7 +45,7 @@ describe('AuthJwtGuard', () => { | |
}) | ||
|
||
it('should not allow user to continue when accessToken is invalid', async function () { | ||
const guard = new AuthJwtGuard() | ||
const guard = app.get(AuthJwtGuard) | ||
|
||
try { | ||
await guard.canActivate( | ||
|
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
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