-
Notifications
You must be signed in to change notification settings - Fork 2
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
8 changed files
with
119 additions
and
15 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
15 changes: 15 additions & 0 deletions
15
src/modules/auth/exceptions/ws-auth-business.exceptions.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 { WsException } from '@nestjs/websockets'; | ||
|
||
export class WSAuthBusinessExceptions { | ||
static userNotFoundException() { | ||
return new WsException('Usuario não encontrado'); | ||
} | ||
|
||
static emailNotVerifiedException() { | ||
return new WsException('Usuário não foi confirmado.'); | ||
} | ||
|
||
static invalidTokenException() { | ||
return new WsException('Token inválido.'); | ||
} | ||
} |
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,59 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { JwtStrategies } from '~/modules/auth/jwt.strategies'; | ||
import { JwtAuthPayload } from './interfaces/jwt-auth-payload.interface'; | ||
import { UserService } from '../user/user.service'; | ||
import { WSAuthBusinessExceptions } from './exceptions/ws-auth-business.exceptions'; | ||
import { IGetConfirmed } from '../user/interfaces/get-confirmed.interface'; | ||
import { Socket } from 'socket.io'; | ||
|
||
@Injectable() | ||
export class WSAuthGuard implements CanActivate { | ||
constructor( | ||
private jwtStrategies: JwtStrategies, | ||
private userService: UserService, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const socket: Socket = context.switchToWs().getClient(); | ||
|
||
const token = this.extractTokenFromHeader(socket); | ||
|
||
let payload: JwtAuthPayload; | ||
|
||
try { | ||
payload = await this.jwtStrategies.auth.verify(token); | ||
} catch (e) { | ||
throw WSAuthBusinessExceptions.invalidTokenException(); | ||
} | ||
|
||
await this.isEmailConfirmed(payload); | ||
|
||
socket['user'] = payload; | ||
|
||
return true; | ||
} | ||
|
||
private async isEmailConfirmed(payload: JwtAuthPayload): Promise<void> { | ||
let user: IGetConfirmed; | ||
|
||
try { | ||
user = await this.userService.getConfirmedUser(payload.userId); | ||
} catch (e) { | ||
throw WSAuthBusinessExceptions.userNotFoundException(); | ||
} | ||
|
||
if (!user.emailConfirmed) { | ||
throw WSAuthBusinessExceptions.emailNotVerifiedException(); | ||
} | ||
} | ||
|
||
private extractTokenFromHeader(socket: Socket): string { | ||
const [type, token] = | ||
socket.handshake.headers.authorization?.split(' ') ?? []; | ||
|
||
if (!token || type !== 'Bearer') | ||
throw WSAuthBusinessExceptions.invalidTokenException(); | ||
|
||
return 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
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,6 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ChatGateway } from './chat.gateway'; | ||
import { ChatService } from './chat.service'; | ||
import { AuthModule } from '../auth/auth.module'; | ||
import { UserModule } from '../user/user.module'; | ||
|
||
@Module({ providers: [ChatGateway, ChatService] }) | ||
@Module({ | ||
imports: [UserModule, AuthModule], | ||
providers: [ChatGateway, ChatService], | ||
}) | ||
export class ChatModule {} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Catch, ArgumentsHost } from '@nestjs/common'; | ||
import { BaseWsExceptionFilter, WsException } from '@nestjs/websockets'; | ||
|
||
@Catch(WsException) | ||
export class WSExceptionFilter extends BaseWsExceptionFilter { | ||
catch(exception: WsException, host: ArgumentsHost) { | ||
const callback = host.getArgByIndex(2); | ||
if (callback && typeof callback === 'function') { | ||
callback({ exception: exception.message }); | ||
} | ||
} | ||
} |