Skip to content

Commit

Permalink
improve jwks handling
Browse files Browse the repository at this point in the history
  • Loading branch information
plasticviking committed Feb 15, 2023
1 parent 708ee97 commit 4c76ef8
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 31 deletions.
183 changes: 161 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"ip": "^1.1.4",
"js-yaml": "^3.13.1",
"jsonwebtoken": "^8.2.1",
"jwks-rsa": "^3.0.1",
"knex": "^0.21.21",
"lodash": "^4.17.13",
"minio": "^7.0.16",
Expand Down
17 changes: 8 additions & 9 deletions src/libs/authmware.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

'use strict';

import { logger, errorWithCode, getJwtCertificate } from '@bcgov/nodejs-common-utils';
import { logger, errorWithCode } from '@bcgov/nodejs-common-utils';
import passport from 'passport';
import { ExtractJwt, Strategy as JwtStrategy } from 'passport-jwt';
import jwksRsa from 'jwks-rsa';
import config from '../config';
import DataManager from './db2';
import { SSO_ROLE_MAP } from '../constants';
Expand Down Expand Up @@ -52,16 +53,15 @@ export default async function initPassport(app) {
});

try {
const {
certificate,
algorithm
} = await getJwtCertificate(config.get('sso:certsUrl'));

const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.algorithms = [algorithm];
opts.secretOrKey = certificate;
opts.passReqToCallback = true;
opts.secretOrKeyProvider = jwksRsa.passportJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: config.get('sso:certsUrl')
});

// For development purposes only ignore the expiration
// time of tokens.
Expand All @@ -71,7 +71,6 @@ export default async function initPassport(app) {

const jwtStrategy = new JwtStrategy(opts, async (req, jwtPayload, done) => {
try {

let user;

// first try the guid
Expand Down

0 comments on commit 4c76ef8

Please sign in to comment.