Skip to content

Commit

Permalink
chore: remove use of @asserts (#31)
Browse files Browse the repository at this point in the history
* chore: remove use of `@assert`s

Check and throw errors explicitly instead of using `@assert`.

fixes: #27

* Apply suggestions from code review

Co-authored-by: Dilum Aluthge <[email protected]>

---------

Co-authored-by: Dilum Aluthge <[email protected]>
  • Loading branch information
tanmaykm and DilumAluthge authored Jun 18, 2024
1 parent cb0d5e5 commit bce27d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ keywords = ["julialang", "jwt", "jwt-authentication", "jwkset", "signing"]
license = "MIT"
desc = "JSON Web Tokens (JWT) for Julia"
authors = ["Tanmay Mohapatra <[email protected]>"]
version = "0.2.5"
version = "0.3.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
16 changes: 8 additions & 8 deletions src/JWTs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ mutable struct JWT

function JWT(; jwt::Union{Nothing,String}=nothing, payload::Union{Nothing,Dict{String,Any},String}=nothing)
if jwt !== nothing
@assert payload === nothing
(payload === nothing) || throw(ArgumentError("payload must be nothing if jwt is provided"))
parts = split(jwt, ".")
if length(parts) == 3
new(parts[2], parts[1], parts[3], false, nothing)
else
new("", nothing, nothing, true, false)
end
else
@assert payload !== nothing
(payload !== nothing) || throw(ArgumentError("payload must be provided if jwt is not"))
new(isa(payload, String) ? payload : urlenc(base64encode(JSON.json(payload))), nothing, nothing, false, nothing)
end
end
Expand Down Expand Up @@ -109,10 +109,10 @@ isvalid(jwt::JWT) = jwt.valid
Get the key id from the JWT header, or `nothing` if the `kid` parameter is not included in the JWT header.
The JWT must be signed. An `AssertionError` is thrown otherwise.
The JWT must be signed. An exception is thrown otherwise.
"""
function kid(jwt::JWT)::String
@assert issigned(jwt)
issigned(jwt) || throw(ArgumentError("jwt is not signed"))
get(decodepart(jwt.header), "kid", nothing)
end

Expand All @@ -121,10 +121,10 @@ end
Get the key algorithm from the JWT header, or `nothing` if the `alg` parameter is not included in the JWT header.
The JWT must be signed. An `AssertionError` is thrown otherwise.
The JWT must be signed. An exception is thrown otherwise.
"""
function alg(jwt::JWT)::String
@assert issigned(jwt)
issigned(jwt) || throw(ArgumentError("jwt is not signed"))
get(decodepart(jwt.header), "alg", nothing)
end

Expand All @@ -150,7 +150,7 @@ show(io::IO, jwt::JWT) = print(io, issigned(jwt) ? join([jwt.header, jwt.payload
validate!(jwt, keyset)
Validate the JWT using the keys in the keyset.
The JWT must be signed. An `AssertionError` is thrown otherwise.
The JWT must be signed. An exception is thrown otherwise.
The keyset must contain the key id from the JWT header. A KeyError is thrown otherwise.
Returns `true` if the JWT is valid, `false` otherwise.
Expand All @@ -163,7 +163,7 @@ function validate!(jwt::JWT, keyset::JWKSet, kid::String)
end
function validate!(jwt::JWT, key::JWK)
isverified(jwt) && (return isvalid(jwt))
@assert issigned(jwt)
issigned(jwt) || throw(ArgumentError("jwt is not signed"))

data = jwt.header * "." * jwt.payload
sigbytes = base64decode(urldec(jwt.signature))
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ function test_signing_keys(keyset, signingkeyset)
for d in test_payload_data
jwt = JWT(; payload=d)
@test claims(jwt) == d
@test_throws AssertionError JWTs.alg(jwt)
@test_throws AssertionError kid(jwt)
@test_throws ArgumentError JWTs.alg(jwt)
@test_throws ArgumentError kid(jwt)
@test !issigned(jwt)
sign!(jwt, signingkeyset, k)
@test issigned(jwt)
Expand Down

0 comments on commit bce27d2

Please sign in to comment.