All files / src/integrations koa.js

100% Statements 20/20
88.89% Branches 16/18
100% Functions 4/4
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 382x 2x   9x   9x 1x     8x   8x   6x     6x 1x     5x 5x   3x 1x       2x       2x          
import { ArgumentError } from '../errors';
import { JwksClient } from '../JwksClient';
 
module.exports.koaJwtSecret = (options = {}) => {
 
  if (!options.jwksUri) {
    throw new ArgumentError('No JWKS URI provided');
  }
 
  const client = new JwksClient(options);
 
  return function secretProvider({ alg, kid } = {}) {
 
    return new Promise((resolve, reject) => {
 
      // Only RS256 is supported.
      if (alg !== 'RS256') {
        return reject(new Error('Missing / invalid token algorithm'));
      }
 
      client.getSigningKey(kid, (err, key) => {
        if (err) {
 
          if (options.handleSigningKeyError) {
            return options.handleSigningKeyError(err)
              .then(reject);
          }
 
          return reject(err);
        }
 
        // Provide the key.
        resolve(key.publicKey || key.rsaPublicKey);
      });
    });
  };
};