All files / src/authzero/utils/jwt jwtLib.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 6/6
100% Lines 19/19

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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 5412x 12x   12x 12x       12x     39x 39x         39x 3x     3x 1x   2x 2x           39x 4x             4x         4x 3x   1x            
import { Injectable } from '@nestjs/common';
import jwksClient, { SigningKey } from 'jwks-rsa';
 
import jwt from 'jsonwebtoken';
import { EnvironmentService } from '../../../common_services/config/envService';
import { IJwtLib } from './iJwtLib';
 
@Injectable()
export default class JwtLib implements IJwtLib {
  client: any ;
 
  constructor(private readonly envService: EnvironmentService) {
    this.client = jwksClient({
      jwksUri: this.envService.auth0UrlJwtWellKnown,
    });
  }
 
  getKey = (header: any, callback: (arg0: any, arg1: any) => void) => {
    this.client.getSigningKey(
      header.kid,
      (err: Error | null, key: SigningKey) => {
        if (err) {
          callback(err, null);
        } else {
          const signingKey = key.getPublicKey();
          callback(null, signingKey);
        }
      },
    );
  }
 
  decodeJwt = (token: string) => new Promise((res, rej) => {
    const options = {
      algorithm: 'RS256',
      issuer: this.envService.auth0JwtIssuer,
      maxAge: this.envService.auth0JwtMaxAge,
      ignoreExpiration: false,
    };
 
    jwt.verify(
      token,
      this.getKey,
      options,
      (err: unknown, decoded: unknown) => {
        if (err) {
          rej(err);
        } else {
          res(decoded);
        }
      },
    );
  })
}