Skip to content

Commit bce27d2

Browse files
chore: remove use of @asserts (#31)
* 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 <dilum@aluthge.com> --------- Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
1 parent cb0d5e5 commit bce27d2

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ keywords = ["julialang", "jwt", "jwt-authentication", "jwkset", "signing"]
44
license = "MIT"
55
desc = "JSON Web Tokens (JWT) for Julia"
66
authors = ["Tanmay Mohapatra <tanmaykm@gmail.com>"]
7-
version = "0.2.5"
7+
version = "0.3.0"
88

99
[deps]
1010
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

src/JWTs.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ mutable struct JWT
7070

7171
function JWT(; jwt::Union{Nothing,String}=nothing, payload::Union{Nothing,Dict{String,Any},String}=nothing)
7272
if jwt !== nothing
73-
@assert payload === nothing
73+
(payload === nothing) || throw(ArgumentError("payload must be nothing if jwt is provided"))
7474
parts = split(jwt, ".")
7575
if length(parts) == 3
7676
new(parts[2], parts[1], parts[3], false, nothing)
7777
else
7878
new("", nothing, nothing, true, false)
7979
end
8080
else
81-
@assert payload !== nothing
81+
(payload !== nothing) || throw(ArgumentError("payload must be provided if jwt is not"))
8282
new(isa(payload, String) ? payload : urlenc(base64encode(JSON.json(payload))), nothing, nothing, false, nothing)
8383
end
8484
end
@@ -109,10 +109,10 @@ isvalid(jwt::JWT) = jwt.valid
109109
110110
Get the key id from the JWT header, or `nothing` if the `kid` parameter is not included in the JWT header.
111111
112-
The JWT must be signed. An `AssertionError` is thrown otherwise.
112+
The JWT must be signed. An exception is thrown otherwise.
113113
"""
114114
function kid(jwt::JWT)::String
115-
@assert issigned(jwt)
115+
issigned(jwt) || throw(ArgumentError("jwt is not signed"))
116116
get(decodepart(jwt.header), "kid", nothing)
117117
end
118118

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

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

168168
data = jwt.header * "." * jwt.payload
169169
sigbytes = base64decode(urldec(jwt.signature))

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ function test_signing_keys(keyset, signingkeyset)
7575
for d in test_payload_data
7676
jwt = JWT(; payload=d)
7777
@test claims(jwt) == d
78-
@test_throws AssertionError JWTs.alg(jwt)
79-
@test_throws AssertionError kid(jwt)
78+
@test_throws ArgumentError JWTs.alg(jwt)
79+
@test_throws ArgumentError kid(jwt)
8080
@test !issigned(jwt)
8181
sign!(jwt, signingkeyset, k)
8282
@test issigned(jwt)

0 commit comments

Comments
 (0)