You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

  1. var fs = require('fs');
  2. var path = require('path');
  3. var jwt = require('../index');
  4. var JsonWebTokenError = require('../lib/JsonWebTokenError');
  5. var expect = require('chai').expect;
  6. var pub = fs.readFileSync(path.join(__dirname, 'pub.pem'), 'utf8');
  7. // priv is never used
  8. // var priv = fs.readFileSync(path.join(__dirname, 'priv.pem'));
  9. var TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MjY1NDY5MTl9.ETgkTn8BaxIX4YqvUWVFPmum3moNZ7oARZtSBXb_vP4';
  10. describe('when setting a wrong `header.alg`', function () {
  11. describe('signing with pub key as symmetric', function () {
  12. it('should not verify', function () {
  13. expect(function () {
  14. jwt.verify(TOKEN, pub);
  15. }).to.throw(JsonWebTokenError, /invalid algorithm/);
  16. });
  17. });
  18. describe('signing with pub key as HS256 and whitelisting only RS256', function () {
  19. it('should not verify', function () {
  20. expect(function () {
  21. jwt.verify(TOKEN, pub, {algorithms: ['RS256']});
  22. }).to.throw(JsonWebTokenError, /invalid algorithm/);
  23. });
  24. });
  25. describe('signing with HS256 and checking with HS384', function () {
  26. it('should not verify', function () {
  27. expect(function () {
  28. var token = jwt.sign({foo: 'bar'}, 'secret', {algorithm: 'HS256'});
  29. jwt.verify(token, 'some secret', {algorithms: ['HS384']});
  30. }).to.throw(JsonWebTokenError, /invalid algorithm/);
  31. });
  32. });
  33. });