Skip to content

Commit

Permalink
Merge ac1f6f4 into 5262183
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmaykm committed Jan 27, 2021
2 parents 5262183 + ac1f6f4 commit 91d197a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -9,6 +9,8 @@ julia:
- 1.1
- 1.2
- 1.3
- 1.4
- 1.5
- nightly

# # Uncomment the following lines to allow failures on nightly julia
Expand Down
3 changes: 2 additions & 1 deletion Project.toml
@@ -1,9 +1,10 @@
name = "JWTs"
uuid = "d850fbd6-035d-5a70-a269-1ca2e636ac6c"
keywords = ["julialang", "jwt", "jwt-authentication", "jwkset", "signing"]
authors = ["Tanmay Mohapatra <tanmaykm@gmail.com>"]
license = "MIT"
desc = "JSON Web Tokens (JWT) for Julia"
version = "0.1.2"
version = "0.1.3"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
22 changes: 18 additions & 4 deletions src/JWTs.jl
Expand Up @@ -39,8 +39,17 @@ mutable struct JWKSet
function JWKSet(url::String)
new(url, Dict{String,JWK}())
end

function JWKSet(keyset::Vector)
keysetdict = Dict{String,JWK}()
refresh!(keyset, keysetdict)
new("", keysetdict)
end
end
function show(io::IO, jwk::JWKSet)
print(io, "JWKSet $(length(jwk.keys)) keys")
isempty(jwk.url) || print(io, " ($(jwk.url))")
end
show(io::IO, jwt::JWKSet) = print(io, "JWKSet $(length(jwt.keys)) keys ($(jwt.url))")

"""
JWT represents a JWT payload at the minimum.
Expand Down Expand Up @@ -152,16 +161,21 @@ function refresh!(keyset::JWKSet, keyseturl::String)
end

function refresh!(keyset::JWKSet)
keys = Dict{String,JWK}()
refresh!(keyset.url, keys)
keyset.keys = keys
if !isempty(keyset.url)
keys = Dict{String,JWK}()
refresh!(keyset.url, keys)
keyset.keys = keys
end
nothing
end

function refresh!(keyseturl::String, keysetdict::Dict{String,JWK})
jstr = startswith(keyseturl, "file://") ? readchomp(keyseturl[8:end]) : String(HTTP.request("GET", keyseturl).body)
keys = JSON.parse(jstr)["keys"]
refresh!(keys, keysetdict)
end

function refresh!(keys::Vector, keysetdict::Dict{String,JWK})
for key in keys
kid = key["kid"]
kty = key["kty"]
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Expand Up @@ -24,6 +24,15 @@ function test_and_get_keyset(url)
keyset
end

function test_in_mem_keyset(template)
print_header("keyset: $template")
keyset = JWKSet(JSON.parse(read(template, String))["keys"])
@test length(keyset.keys) == 4
for (k,v) in keyset.keys
println(" ", k, " => ", v.key)
end
end

function test_signing(keyset_url)
keyset = test_and_get_keyset(keyset_url)

Expand Down Expand Up @@ -98,3 +107,4 @@ end

test_and_get_keyset("https://www.googleapis.com/oauth2/v3/certs")
test_signing("file://" * joinpath(@__DIR__, "jwkkey.json"))
test_in_mem_keyset(joinpath(@__DIR__, "jwkkey.json"))
2 changes: 1 addition & 1 deletion tools/jwt_create.jl
Expand Up @@ -24,7 +24,7 @@ function main()

jwt = JWT(; payload=JSON.parse(readchomp(ARGS[1])))
sign!(jwt, keyset, kid)
println(jwt)
println(string(jwt))
end

main()

0 comments on commit 91d197a

Please sign in to comment.