diff options
author | dittonjs <jditton.atomic@gmail.com> | 2022-03-08 18:25:58 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-08 18:25:58 -0700 |
commit | 5e2996c0fdeee2eaa88a72f79d2e0d600edeb5a6 (patch) | |
tree | 3e9cd6e10f81a65f76478910f6feb1b89d6ce1dc /server/providers/guards | |
parent | 15800f84a42b0aa53311ff88fa3d554653a2511a (diff) | |
parent | 1b7c6ee006b50ce29c9cb3ee42fad29b7db0f6f3 (diff) | |
download | locchat-5e2996c0fdeee2eaa88a72f79d2e0d600edeb5a6.tar.gz locchat-5e2996c0fdeee2eaa88a72f79d2e0d600edeb5a6.zip |
Merge pull request #4 from dittonjs/jd/add-websockets
Jd/add websockets
Diffstat (limited to 'server/providers/guards')
-rw-r--r-- | server/providers/guards/gatewayauth.guard.ts | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/server/providers/guards/gatewayauth.guard.ts b/server/providers/guards/gatewayauth.guard.ts new file mode 100644 index 0000000..0843752 --- /dev/null +++ b/server/providers/guards/gatewayauth.guard.ts @@ -0,0 +1,27 @@ +import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; +import { JwtService } from '../services/jwt.service'; +import { GuardUtil } from '../util/guard.util'; +import { Socket } from 'socket.io'; +import { WsException } from '@nestjs/websockets'; + +@Injectable() +export class GatewayAuthGuard implements CanActivate { + constructor(private guardUtil: GuardUtil, private jwtService: JwtService) {} + + canActivate(context: ExecutionContext) { + // Handlers and Controllers can both skip this guard in the event that + if (this.guardUtil.shouldSkip(this, context)) { + return true; + } + + const req = context.switchToHttp().getRequest() as Socket; + const jwt = req.handshake.auth.token; + if (!jwt) throw new WsException('Invalid auth token'); + try { + req.handshake.auth.jwtBody = this.jwtService.parseToken(jwt); + } catch (e) { + throw new WsException('Invalid auth token'); + } + return true; + } +} |