decode.js 767 B

123456789101112131415161718192021222324252627282930
  1. var jws = require('jws');
  2. module.exports = function (jwt, options) {
  3. options = options || {};
  4. var decoded = jws.decode(jwt, options);
  5. if (!decoded) { return null; }
  6. var payload = decoded.payload;
  7. //try parse the payload
  8. if(typeof payload === 'string') {
  9. try {
  10. var obj = JSON.parse(payload);
  11. if(obj !== null && typeof obj === 'object') {
  12. payload = obj;
  13. }
  14. } catch (e) { }
  15. }
  16. //return header if `complete` option is enabled. header includes claims
  17. //such as `kid` and `alg` used to select the key within a JWKS needed to
  18. //verify the signature
  19. if (options.complete === true) {
  20. return {
  21. header: decoded.header,
  22. payload: payload,
  23. signature: decoded.signature
  24. };
  25. }
  26. return payload;
  27. };