Home > Software design >  Node: Convert json to azure web token
Node: Convert json to azure web token

Time:08-04

I am practicing to convert json to azure token. From converting json to azure token I am using jwtwebtoken package. I have created custom payload and after that invoke this payload jwtwebtoken's sign function. It creates a token but it not azure valid. For azure token validation it throws me this error: No KID specified and JWKS endpoint returned more than 1 key. In my payload I passed Kid but I don't know how to put in as Header's kid.

This is how convert jwt token.

const jwt =. require('jsonwebtoken')


const session = {
  aud: '53ee92fd-4b6b-4ca5-8846-34d60f441e7e',
  iss: 'https://sts.windows.net/a652adc3-7bb3-4312-8eb0-29w9w848/',
  acr: '1',
  aio: 'ASQA2/8TAAAAABdGB29gJ6Sny3 Mkr7 NglS3od1934hdVNuTDNyJhsYFFME=',
  amr: ['pwd'],
  appid: '53e2827w-4b6b-4ca5-8846-34d60f441e7e',
  appidacr: '1',
  family_name: 'doe',
  given_name: 'john',
  ipaddr: '85.76.79.111',
  name: 'admin-test',
  oid: 'a1e9b2c2-efb7-4e85-9918273-10202',
  onprem_sid: 'S-1-5-21-1632691039-112712222-3397904514-192345',
  rh: '0.AQIAw61SprN7EkOOsGqzI_fWzf2SDJDJDJENS.',
  roles: ['admin'],
  scp: 'User.Read',
  sub: 'e-BpFMk9HFzaFKDtJWxWaUyssLD1aglN-MWOAXhkQPZKb0Q',
  tid: 'a652adc3-7bb3-4312-8eb0-6ab323f7aadd6cd',
  unique_name: '[email protected]',
  upn: 'john doe',
  uti: 'OMKuhAZ87ESKAVZrJF1aAA',
  ver: '1.0',
  sgrExternalSitecode: ['209292', '139911'],
  extensionAttribute7: '817272',
  x5t: '91818273-1019181',
  kid: '191818273-1019181' // I am passing kid
}


const accessToken = jwt.sign(session, 'secret', { expiresIn: 60 * 60 })

console.log(accessToken)
<script src="js/jwt-decode.min.js"></script>
ValidAzure Token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVgxODE3MjczIiwia2lkIjoiMlpRcEozVXBiakFZWDE4MTcyNzMifQ.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sInVwbiI6ImpvaG4gZG9lIiwidXRpIjoiT01LdWhBWjg3RVNLQVZackpGMWFBQSIsInZlciI6IjEuMCIsInNnckV4dGVybmFsU2l0ZWNvZGUiOlsiMjA5MjkyIiwiMTM5OTExIl0sImV4dGVuc2lvbkF0dHJpYnV0ZTciOiI4MTcyNzIiLCJ4NXQiOiI5MTgxODI3My0xMDE5MTgxIiwia2lkIjoiMTkxODE4MjczLTEwMTkxODEiLCJpYXQiOjE2NTk1MzA2MDUsImV4cCI6MTY1OTUzNDIwNX0.P8cuQFyiSKWOx-Sk8KxciUxYx6bR06yxYTe0AEwRa4c"

CodePudding user response:

The keyId claim kidcan be added as an option to the sign function like so:

kid = "191818273-1019181"
const accessToken = jwt.sign(session, 'secret', { keyid: kid, expiresIn: 60 * 60 })
  • Related