-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement basic ws client connection auth
- Loading branch information
Showing
4 changed files
with
75 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { AuthenticationService } from 'modules/authentication/authentication.service'; | ||
import { Socket } from 'socket.io'; | ||
import { WsException } from '@nestjs/websockets'; | ||
import { DataSource } from 'typeorm'; | ||
import { User } from 'modules/users/user.entity'; | ||
|
||
/** | ||
* @description As of today, NestJS doesnt treat websockets as first class citizens, meaning that the useful | ||
* guard decorators that we have for HTTPS request wont work for webscokets gatewau, so we need to implement something hand crafter | ||
* but trying to follow the same pattern as the HTTP guards. | ||
* | ||
* @note: Chek https://github.com/nestjs/nest/issues/882 | ||
*/ | ||
|
||
@Injectable() | ||
export class WsAuthGuard { | ||
logger: Logger = new Logger(WsAuthGuard.name); | ||
|
||
constructor( | ||
private readonly auth: AuthenticationService, | ||
private readonly datasource: DataSource, | ||
) {} | ||
|
||
async verifyUser(client: Socket): Promise<User> { | ||
const { token } = client.handshake?.auth; | ||
if (!token) { | ||
throw new WsException('Unauthorized'); | ||
} | ||
try { | ||
const { sub } = this.auth.verifyToken(token as string); | ||
const user: User = await this.datasource | ||
.getRepository(User) | ||
.findOneOrFail({ where: { email: sub } }); | ||
return user; | ||
} catch (e) { | ||
throw new WsException('Unauthorized'); | ||
} | ||
} | ||
} |