Skip to content

Instantly share code, notes, and snippets.

@saks
Created August 18, 2016 07:28
Show Gist options
  • Save saks/29e3bb2d92fc2db57d0e0308a148a79d to your computer and use it in GitHub Desktop.
Save saks/29e3bb2d92fc2db57d0e0308a148a79d to your computer and use it in GitHub Desktop.
-- /usr/local/openresty/bin/resty -e "require 'busted.runner'({ standalone = false })"
local jwt = require 'resty.jwt'
local shared_key_32 = '12341234123412341234123412341234'
local shared_key_64 = '12341234123412341234123412341234' ..
'12341234123412341234123412341234'
describe('jwe', function()
context('encode', function()
context('A128CBC-HS256 Direct Encryption', function()
local shared_key = shared_key_32
it('should understand itself', function()
local table_of_jwt = {
header = { alg = 'dir', enc = 'A128CBC-HS256' },
payload = { foo = 'bar' },
}
local jwt_token = jwt:sign(shared_key, table_of_jwt)
local jwt_obj = jwt:verify(shared_key, jwt_token)
assert.are_same(table_of_jwt.payload, jwt_obj.payload)
end)
end)
context('A256CBC-HS512 Direct Encryption', function()
local shared_key = shared_key_64
it('should understand itself', function()
local table_of_jwt = {
header = { alg = 'dir', enc = 'A256CBC-HS512' },
payload = { foo = 'bar' },
}
local jwt_token = jwt:sign(shared_key, table_of_jwt)
local jwt_obj = jwt:verify(shared_key, jwt_token)
assert.are_same(table_of_jwt.payload, jwt_obj.payload)
end)
end)
end)
context('verify', function()
context('A128CBC-HS256 Direct Encryption', function()
local shared_key = shared_key_32
local jwt_obj
before_each(function()
jwt_obj = jwt:verify(
shared_key,
"eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0." ..
".U6emIwy_yVkagUwQ4EjdFA.FrapgQVvG3uictQz9NPPMw.n" ..
"MoW0ShdgCN0JHw472SJjQ"
)
end)
it('should have correct payload', function()
assert.are_equal('bar', jwt_obj.payload.foo)
end)
it('should have a good reason', function()
assert.are_equal('everything is awesome~ :p', jwt_obj.reason)
end)
it('should have correct algorithm', function()
assert.are_equal(jwt_obj.header.alg, 'dir')
end)
it('should have correct enc', function()
assert.are_equal(jwt_obj.header.enc, 'A128CBC-HS256')
end)
it('should be valid', function()
assert.is_true(jwt_obj.valid)
end)
it('should be verified', function()
assert.is_true(jwt_obj.verified)
end)
end)
context('A256CBC-HS512 Direct Encryption', function()
local shared_key = shared_key_64
local jwt_obj
before_each(function()
jwt_obj = jwt:verify(
shared_key,
"eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIn0." ..
".M927Z_hNTmumFQE0rtRQCQ.nnd7AoE_2dgvws2-iay8qA.d" ..
"kyZuuks4Qm9Cd7VfEVSs07pi_Kyt0INVHTTesUC2BM"
)
end)
it('should have correct payload', function()
assert.are_equal('bar', jwt_obj.payload.foo)
end)
it('should have a good reason', function()
assert.are_equal('everything is awesome~ :p', jwt_obj.reason)
end)
it('should have correct algorithm', function()
assert.are_equal(jwt_obj.header.alg, 'dir')
end)
it('should have correct enc', function()
assert.are_equal(jwt_obj.header.enc, 'A256CBC-HS512')
end)
it('should be valid', function()
assert.is_true(jwt_obj.valid)
end)
it('should be verified', function()
assert.is_true(jwt_obj.verified)
end)
end)
end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment