Skip to content

Commit

Permalink
Update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
greyblake committed Jan 25, 2016
1 parent 359db77 commit c773145
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
@@ -1,4 +1,4 @@
require "../spec_helper"
require "../../spec_helper"

describe JWT do
secret_key = "$ecretKey"
Expand Down
@@ -1,4 +1,4 @@
require "../spec_helper"
require "../../spec_helper"

describe "none algorithm" do
alg = "none"
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/claims/aud_spec.cr
Expand Up @@ -13,7 +13,7 @@ describe "aud claim" do
context "aud option is passed" do
it "raises InvalidAudienceError" do
token = JWT.encode({"foo" => "bar"}, "key", "HS256")
expect_raises(JWT::InvalidAudienceError, "Invalid audience. Expected sergey, got nothing") do
expect_raises(JWT::InvalidAudienceError, "Invalid audience (aud). Expected \"sergey\", received nothing") do
JWT.decode(token, "key", "HS256", {:aud => "sergey"})
end
end
Expand All @@ -40,7 +40,7 @@ describe "aud claim" do
context "aud does not match" do
it "raises InvalidAudienceError" do
token = JWT.encode({"foo" => "bar", "aud" => "sergey"}, "key", "HS256")
expect_raises(JWT::InvalidAudienceError, "Invalid audience. Expected julia, got sergey") do
expect_raises(JWT::InvalidAudienceError, "Invalid audience (aud). Expected \"julia\", received \"sergey\"") do
JWT.decode(token, "key", "HS256", {aud: "julia"})
end
end
Expand Down Expand Up @@ -72,7 +72,7 @@ describe "aud claim" do
it "raises InvalidAudienceError" do
token = JWT.encode({"foo" => "bar", "aud" => ["sergey", "julia"]}, "key", "HS256")

expect_raises(JWT::InvalidAudienceError, "Invalid audience. Expected max, got [\"sergey\", \"julia\"]") do
expect_raises(JWT::InvalidAudienceError, "Invalid audience (aud). Expected \"max\", received [\"sergey\", \"julia\"]") do
JWT.decode(token, "key", "HS256", {aud: "max"})
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/claims/iss_spec.cr
Expand Up @@ -5,7 +5,7 @@ describe "iss claim" do
context ":iss option is passed to .decode" do
it "raises InvalidIssuerError" do
token = JWT.encode({"foo" => "bar"}, "key", "HS256")
expect_raises(JWT::InvalidIssuerError, "Invalid issuer. Expected \"TEJO\", received nothing") do
expect_raises(JWT::InvalidIssuerError, "Invalid issuer (iss). Expected \"TEJO\", received nothing") do
JWT.decode(token, "key", "HS256", {iss: "TEJO"})
end
end
Expand Down Expand Up @@ -33,7 +33,7 @@ describe "iss claim" do
context "iss does not match" do
it "raises InvalidIssuerError" do
token = JWT.encode({"iss" => "TEJO"}, "key", "HS256")
expect_raises(JWT::InvalidIssuerError, "Invalid issuer. Expected \"UEA\", received \"TEJO\"") do
expect_raises(JWT::InvalidIssuerError, "Invalid issuer (iss). Expected \"UEA\", received \"TEJO\"") do
JWT.decode(token, "key", "HS256", {iss: "UEA"})
end
end
Expand Down
10 changes: 5 additions & 5 deletions src/jwt.cr
Expand Up @@ -90,16 +90,16 @@ module JWT

private def validate_aud!(payload, aud)
if !payload["aud"]?
raise InvalidAudienceError.new("Invalid audience. Expected #{aud}, got nothing")
raise InvalidAudienceError.new("Invalid audience (aud). Expected #{aud.inspect}, received nothing")
elsif payload["aud"].is_a?(String)
if aud != payload["aud"]
raise InvalidAudienceError.new("Invalid audience. Expected #{aud}, got #{payload["aud"]}")
raise InvalidAudienceError.new("Invalid audience (aud). Expected #{aud.inspect}, received #{payload["aud"].inspect}")
end
elsif payload["aud"].is_a?(Array)
# to prevent compile-time error
auds = payload["aud"] as Array
if !auds.includes?(aud)
msg = "Invalid audience. Expected #{aud}, got #{payload["aud"].inspect}"
msg = "Invalid audience (aud). Expected #{aud.inspect}, received #{payload["aud"].inspect}"
raise InvalidAudienceError.new(msg)
end
else
Expand All @@ -109,9 +109,9 @@ module JWT

private def validate_iss!(payload, iss)
if !payload["iss"]?
raise InvalidIssuerError.new("Invalid issuer. Expected #{iss.inspect}, received nothing")
raise InvalidIssuerError.new("Invalid issuer (iss). Expected #{iss.inspect}, received nothing")
elsif payload["iss"] != iss
raise InvalidIssuerError.new("Invalid issuer. Expected #{iss.inspect}, received #{payload["iss"].inspect}")
raise InvalidIssuerError.new("Invalid issuer (iss). Expected #{iss.inspect}, received #{payload["iss"].inspect}")
end
end
end

0 comments on commit c773145

Please sign in to comment.