Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hardcoded IP and prevent service from crashing on error. #34

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/guard/ws.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import * as jwt from 'jsonwebtoken';
const jwksClient = require('jwks-rsa');

const client = jwksClient({
jwksUri: 'http://103.154.251.109:9011/.well-known/jwks.json',
jwksUri: process.env.TRANSPORT_SOCKET_JWT_AUTH_URL,
requestHeaders: {}, // Optional
timeout: 30000, // Defaults to 30s
});

const getKey = (header, callback) => {
client.getSigningKey(header.kid, function (err, key) {
if (err || !key || !(key.publicKey || key.rsaPublicKey)) {
callback(err, null);
return;
}
const signingKey = key.publicKey || key.rsaPublicKey;
callback(null, signingKey);
});
Expand All @@ -27,18 +31,24 @@ export class WsGuard implements CanActivate {
canActivate(
context: any,
): boolean | any | Promise<boolean | any> | Observable<boolean | any> {
this.logger.error(`Trying to authenticate user`);
this.logger.log(`Trying to authenticate user`);
const bearerToken =
context.args[0].handshake.headers.authorization.split(' ')[1];
return new Promise(function (resolve, reject) {
jwt.verify(bearerToken, getKey, function (err, decoded) {
if (err || !decoded || !decoded['sub'] || !decoded['preferred_username']) {
reject('User could not be resolved!');
return;
}
console.log(decoded);
context.args[0].handshake.headers.userId = decoded.sub;
context.args[0].handshake.headers.userPhone =
decoded['preferred_username'];
if (err) resolve(false);
resolve(true);
});
})
.catch(err => {
this.logger.error(err);
});
}
}
Loading