Skip to content
This repository was archived by the owner on Jan 26, 2025. It is now read-only.
This repository was archived by the owner on Jan 26, 2025. It is now read-only.

a way to verify using a pem ? #1029

Description

@walshe

I usually verify a jwt using a pem ( jwt.verify(token, pem, { algorithms: [completeDecodedToken.header.alg] });) calculated by using kid in header and using that to find correct jwk which can be used to create a pem... but the verify() in your lib needs an audience(s) in the second param

const completeDecodedToken = jwtJsDecode.jwtDecode(token);

  console.log("decoded token", completeDecodedToken);

  if(!completeDecodedToken){
    throw new Error(`Could not decode JWT: ${token}`);
  }
  
  // decode token and use kid to find correct jwk
  const jwk = keys.filter(jwk => jwk.kid == completeDecodedToken.header.kid)

  // verify the token
  if(!jwk.length){
    throw new Error(`Could not find matching jwk for kid ${completeDecodedToken.header.kid}`);
  }

  //use kid to create a pem (https://serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file)
  const pem = jwkToPem(jwk[0]);

  console.log('verifying token using pem..')

  try{
    const jwt = require('jsonwebtoken'); // for seom reason this wont work with okta tokens 
    jwt.verify(token, pem, { algorithms: [completeDecodedToken.header.alg] });
  }catch(err){

    console.warn('token verification failed', err.message, err.name);
    context.fail("Unauthorized");
    return;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions