Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create and verify jwt hs256 #700

Merged
merged 3 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion src/lua/zencode_w3c.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,40 @@ local function export_verification_method(doc)
return res
end

local function import_jwt(obj)
local res = {}
local toks = strtok(obj, '.')
res.header = JSON.decode(ZEN.get(toks[1], '.', O.from_url64, tostring):string())
res.header = deepmap(function(s)
if type(s) == 'string' then
return O.from_string(s)
elseif type(s) == 'number' then
return F.new(s)
else
return s
end
end, res.header)
res.payload = JSON.decode(ZEN.get(toks[2], '.', O.from_url64, tostring):string())
res.payload = deepmap(function(s)
if type(s) == 'string' then
return O.from_string(s)
elseif type(s) == 'number' then
return F.new(s)
else
return s
end
end, res.payload)
res.signature = ZEN.get(toks[3], '.', O.from_url64, tostring)
return res
end


local function export_jwt(obj)
local header = O.to_url64(O.from_string(JSON.encode(obj.header, 'string')))
local payload = O.to_url64(O.from_string(JSON.encode(obj.payload, 'string')))
return header .. '.' .. payload .. '.' .. obj.signature:url64()
end

ZEN.add_schema(
{
did_document = { import = import_did_document,
Expand All @@ -105,7 +139,9 @@ ZEN.add_schema(
zentype = 'e'
})
return (deepmap(OCTET.from_string, obj))
end
end,
json_web_token = { import = import_jwt,
export = export_jwt }
}
)

Expand Down Expand Up @@ -343,3 +379,44 @@ When(
ZEN.CODEC[pk_name].name = pk_name
end
)

function create_jwt_hs256(payload, password)
local header, b64header, b64payload, hmac
header = {
alg=O.from_string("HS256"),
typ=O.from_string("JWT")
}
b64header = O.from_string(JSON.encode(header, 'string')):url64()
b64payload = O.from_string(JSON.encode(payload, 'string')):url64()
hash = HASH.new("sha256")

local signature = hash:hmac(
password,
b64header .. '.' .. b64payload)
return {
header=header,
payload=payload,
signature=signature,
}
end

When(
"create the json web token of '' using ''",
function(payload_name, password_name)
local payload = have(payload_name)
local password = mayhave(password_name) or password_name
empty'json_web_token'
ACK.json_web_token = create_jwt_hs256(payload, password)
new_codec("json_web_token")
end
)

IfWhen(
"verify the json web token in '' using ''",
function(hmac_name, password_name)
local hmac = have(hmac_name)
local password = mayhave(password_name) or password_name
local jwt_hs256 = create_jwt_hs256(hmac.payload, password)
ZEN.assert(jwt_hs256.signature == hmac.signature, "Could not re-create HMAC")
end
)
36 changes: 36 additions & 0 deletions test/zencode/w3c.bats
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,39 @@ EOF
run $ZENROOM_EXECUTABLE -z -a did_documents.json verify_wrong_did_doc.zen
assert_line '[W] The signer id in proof is different from the one in not_signer_did_document'
}
@test "JWT HS256 creation" {
cat <<EOF > jwt_hs256.data
{
"payload": {
"iat": 15162,
"name": "John Doe",
"sub": "1234567890"
},
"password": "password"
}
EOF
cat <<EOF | zexe jwt_hs256.zen jwt_hs256.data
Scenario 'w3c': did document manipulation
Given I have a 'string dictionary' named 'payload'
Given I have a 'string' named 'password'

When I create the json web token of 'payload' using 'password'

Then print the 'json web token'
EOF
save_output jwt_hs256.json
assert_output '{"json_web_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyLCJuYW1lIjoiSm9obiBEb2UiLCJzdWIiOiIxMjM0NTY3ODkwIn0.t_oDRdxZOP3rL53qUL5cS74WsqrZWWsXaIZT-AQL4WU"}'
}

@test "JWT HS256 verify" {
cat <<EOF | zexe jwt_hs256_verify.zen jwt_hs256.json
Scenario 'w3c': did document manipulation
Given I have a 'json web token'

When I verify the json web token in 'json web token' using 'password'

Then print the string 'ok'
EOF
save_output jwt_hs256_verify.json
assert_output '{"output":["ok"]}'
}
Loading