<commit_before>local jwt_parser = require "kong.plugins.jwt.jwt_parser"
local fixtures = require "spec.03-plugins.17-jwt.fixtures"
local helpers = require "spec.helpers"
local u = helpers.unindent

describe("Plugin: jwt (parser)", function()
  describe("Encoding", function()
    it("should properly encode using HS256", function()
      local token = jwt_parser.encode({
        sub = "1234567890",
        name = "<NAME>",
        admin = true
      }, "secret")

      if helpers.openresty_ver_num < 11123 then
        assert.equal(u([[
          <KEY>
        ]], true), token)
      else
        assert.equal(u([[
          <KEY>
        ]], true), token)
      end
    end)
    it("should properly encode using RS256", function()
      local token = jwt_parser.encode({
        sub = "1234567890",
        name = "<NAME>",
        admin = true
      }, fixtures.rs256_private_key, 'RS256')

      if helpers.openresty_ver_num < 11123 then
        assert.equal(u([[
          <KEY>
        ]], true), token)
      else
        assert.equal(u([[
          <KEY>6HMzyF3R8dysiNivScwVTHHNkM2VrF
          BqyGv84CMqdr42bk0z4WiNnVOzSqReub9DXSDx4gNd9hK41UChFv6k2iDELXP0nwllnyu
          qbGbjm0HM7GOAptzViFqULEBvGb-J-s99Q
        ]], true), token)
      end
    end)
    it("should encode using RS512", function()
      local token = jwt_parser.encode({
        sub   = "1234567890",
        name  = "<NAME>",
        admin = true,
      }, fixtures.rs512_private_key, "RS512")

      assert.equal([[<KEY>]] ..
                   [[<KEY>]] ..
                   [[<KEY>]] ..
                   [[iU011QqAmlMM8oIRCnoGKvA63XeE7M6qPsNkJ_vHMoqO-Hg3ajx1R]] ..
                   [[aWmVaHyeTCkkyStNvh88phVSH5EB5wIYjukHErRXLCTL9UhE0Z60f]] ..
                   [[NzLeEZ5yJZS-rhOK3fa0QSVoTD2QKVITYBcX_xt6NzHzTTx_3kQ1K]] ..
                   [[lcuueNlOLmCYx_6tissUvMY91KjcZfs3z9tYREu5paFx0pSiPvgNB]] ..
                   [[vrWQfbm3irr-1YcBH7wJuIinPDrERVohK1v37t8fDnSqhi1tWUati]] ..
                   [[7Mynkb3JrpCeF3IyReSvkhQA]], token)
    end)

    it("should encode using ES256", function()
      local token = jwt_parser.encode({
        sub = "5656565656",
        name = "<NAME>",
        admin = true
      }, fixtures.es256_private_key, 'ES256')
      assert.truthy(token)
    end)
  end)
  describe("Decoding", function()
    it("throws an error if not given a string", function()
      assert.has_error(function()
        jwt_parser:new()
      end, "Token must be a string, got nil")
    end)
    it("refuses invalid typ", function()
      local token = jwt_parser.encode({sub = "1234"}, "secret", nil, {typ = "foo"})<commit_msg>tests(jwt) proper RS512 fixture for OpenResty 1.11.2.3+<commit_after>local jwt_parser = require "kong.plugins.jwt.jwt_parser"
local fixtures = require "spec.03-plugins.17-jwt.fixtures"
local helpers = require "spec.helpers"
local u = helpers.unindent

describe("Plugin: jwt (parser)", function()
  describe("Encoding", function()
    it("should properly encode using HS256", function()
      local token = jwt_parser.encode({
        sub = "1234567890",
        name = "<NAME>",
        admin = true
      }, "secret")

      if helpers.openresty_ver_num < 11123 then
        assert.equal(u([[
          <KEY>
        ]], true), token)
      else
        assert.equal(u([[
          <KEY>
        ]], true), token)
      end
    end)
    it("should properly encode using RS256", function()
      local token = jwt_parser.encode({
        sub = "1234567890",
        name = "<NAME>",
        admin = true
      }, fixtures.rs256_private_key, 'RS256')

      if helpers.openresty_ver_num < 11123 then
        assert.equal(u([[
          <KEY>
        ]], true), token)
      else
        assert.equal(u([[
          <KEY>
        ]], true), token)
      end
    end)
    it("should encode using RS512", function()
      local token = jwt_parser.encode({
        sub   = "1234567890",
        name  = "<NAME>",
        admin = true,
      }, fixtures.rs512_private_key, "RS512")

      if helpers.openresty_ver_num < 11123 then
        assert.equal(u([[
          <KEY>
        ]], true), token)

      else
        assert.equal(u([[
          <KEY>
          KXI18ZRTBOmh4hV66fzbzTxh_kO7Z-h0hOfdVbtqEJRLlQkgq3c_JUB5Sky2WLkK3rFWm
          UecxqNnQXKYkOgq_DuXU8KIv7GGzpTELww
        ]], true), token)
      end
    end)

    it("should encode using ES256", function()
      local token = jwt_parser.encode({
        sub = "5656565656",
        name = "<NAME>",
        admin = true
      }, fixtures.es256_private_key, 'ES256')
      assert.truthy(token)
    end)
  end)
  describe("Decoding", function()
    it("throws an error if not given a string", function()
      assert.has_error(function()
        jwt_parser:new()
      end, "Token must be a string, got nil")
    end)
    it("refuses invalid typ", function()
      local token = jwt_parser.encode({sub = "1234"}, "secret", nil, {typ = "foo"})
{'fraction_non_alphanumeric': 0.1273317112733171, 'fraction_numerical': 0.054136253041362534, 'mean_word_length': 2.253957783641161, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1878575', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1727, 'n_words': 341}