-
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.
Add authentication in webhook (#175)
* Make verification asynchronous * Add adapter for authentication * Add jwt auth guard * Implement authentication for gateway
- Loading branch information
1 parent
d576276
commit 07efc2d
Showing
8 changed files
with
117 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { INestApplicationContext } from '@nestjs/common'; | ||
import { IoAdapter } from '@nestjs/platform-socket.io'; | ||
import { AuthService } from 'src/auth/auth.service'; | ||
|
||
// This adapter can be used to authenticate socket.io connections | ||
// Not using this for now because we want to provide some events for non-authenticated users | ||
// For implementation: https://github.com/nestjs/nest/issues/882#issuecomment-632698668 | ||
export class AuthenticationIoAdapter extends IoAdapter { | ||
private readonly authService: AuthService; | ||
constructor(private app: INestApplicationContext) { | ||
super(app); | ||
this.authService = this.app.get(AuthService); | ||
} | ||
createIOServer(port: number, options?: any): any { | ||
options.allowRequest = async (request, allowFunction) => { | ||
const token = request.headers.cookie | ||
?.split('; ') | ||
?.find((c) => c.startsWith('token=')) | ||
?.split('=')[1]; | ||
|
||
try { | ||
await this.authService.verifyAccessToken(token); | ||
return allowFunction(null, true); | ||
} catch (error) { | ||
return allowFunction('Unauthorized', false); | ||
} | ||
}; | ||
return super.createIOServer(port, options); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EventsGateway } from './events.gateway'; | ||
import { PongMatchGateway } from './pong-match.gateway'; | ||
import { AuthModule } from 'src/auth/auth.module'; | ||
|
||
@Module({ | ||
providers: [EventsGateway, PongMatchGateway], | ||
imports: [AuthModule], | ||
}) | ||
export class EventsModule {} |
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,30 @@ | ||
import { CanActivate, ExecutionContext } from '@nestjs/common'; | ||
// import { WsException } from '@nestjs/websockets'; | ||
import { AuthService } from 'src/auth/auth.service'; | ||
|
||
export class UserGuardWs implements CanActivate { | ||
constructor(private authService: AuthService) {} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
const client = context.switchToWs().getClient(); | ||
|
||
// This is cashing the user in the client object. | ||
// This means that the user could be outdated. | ||
if (client.user) return true; | ||
|
||
// When handleConnection is called, the connection is already created. | ||
// This means that clients can send messages before `client.user` is set. | ||
// The code below makes sure that authorized users don't get unauthorized. | ||
const token = client.request.headers.cookie?.split('token=')[1]; | ||
if (!token) return false; | ||
|
||
try { | ||
const user = await this.authService.verifyAccessToken(token); | ||
if (!user) return false; | ||
client.user = user; | ||
} catch { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |