Skip to content

Commit

Permalink
Add support for boolean claims (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaishin authored Feb 2, 2021
1 parent 9902e86 commit a08d1c7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
17 changes: 11 additions & 6 deletions JWTDecode/JWTDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,30 @@ public struct Claim {
return self.value as? String
}

/// value of the claim as `Bool`
public var boolean: Bool? {
return self.value as? Bool
}

/// value of the claim as `Double`
public var double: Double? {
let double: Double?
var double: Double?
if let string = self.string {
double = Double(string)
} else {
} else if self.boolean == nil {
double = self.value as? Double
}
return double
}

/// value of the claim as `Int`
public var integer: Int? {
let integer: Int?
var integer: Int?
if let string = self.string {
integer = Int(string)
} else if let double = self.value as? Double {
} else if let double = self.double {
integer = Int(double)
} else {
} else if self.boolean == nil {
integer = self.value as? Int
}
return integer
Expand All @@ -121,7 +126,7 @@ public struct Claim {

/// value of the claim as `[String]`
public var array: [String]? {
if let array = value as? [String] {
if let array = self.value as? [String] {
return array
}
if let value = self.string {
Expand Down
33 changes: 29 additions & 4 deletions Tests/JWTDecode.swiftTests/JWTDecodeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,17 @@ class JWTDecodeSpec: QuickSpec {
describe("custom claim") {

beforeEach {
token = jwt(withBody: ["sub": UUID().uuidString, "custom_claim": "Shawarma Friday!", "custom_integer_claim": 10, "custom_double_claim": 3.4, "custom_double_string_claim": "1.3"])
token = jwt(withBody: ["sub": UUID().uuidString, "custom_string_claim": "Shawarma Friday!", "custom_integer_claim": 10, "custom_double_claim": 3.4, "custom_double_string_claim": "1.3", "custom_true_boolean_claim": true, "custom_false_boolean_claim": false])
}

it("should return string claim") {
let claim = token.claim(name: "custom_claim")
let claim = token.claim(name: "custom_string_claim")
expect(claim.string) == "Shawarma Friday!"
expect(claim.array) == ["Shawarma Friday!"]
expect(claim.integer).to(beNil())
expect(claim.date).to(beNil())
expect(claim.double).to(beNil())
expect(claim.date).to(beNil())
expect(claim.boolean).to(beNil())
}

it("should return integer claim") {
Expand All @@ -182,6 +183,7 @@ class JWTDecodeSpec: QuickSpec {
expect(claim.integer) == 10
expect(claim.double) == 10.0
expect(claim.date) == Date(timeIntervalSince1970: 10)
expect(claim.boolean).to(beNil())
}

it("should return double claim") {
Expand All @@ -191,6 +193,7 @@ class JWTDecodeSpec: QuickSpec {
expect(claim.integer) == 3
expect(claim.double) == 3.4
expect(claim.date) == Date(timeIntervalSince1970: 3.4)
expect(claim.boolean).to(beNil())
}

it("should return double as string claim") {
Expand All @@ -200,15 +203,37 @@ class JWTDecodeSpec: QuickSpec {
expect(claim.integer).to(beNil())
expect(claim.double) == 1.3
expect(claim.date) == Date(timeIntervalSince1970: 1.3)
expect(claim.boolean).to(beNil())
}

it("should return true boolean claim") {
let claim = token.claim(name: "custom_true_boolean_claim")
expect(claim.string).to(beNil())
expect(claim.array).to(beNil())
expect(claim.integer).to(beNil())
expect(claim.double).to(beNil())
expect(claim.date).to(beNil())
expect(claim.boolean) == true
}

it("should return false boolean claim") {
let claim = token.claim(name: "custom_false_boolean_claim")
expect(claim.string).to(beNil())
expect(claim.array).to(beNil())
expect(claim.integer).to(beNil())
expect(claim.double).to(beNil())
expect(claim.date).to(beNil())
expect(claim.boolean) == false
}

it("should return no value when clain is not present") {
it("should return no value when claim is not present") {
let unknownClaim = token.claim(name: "missing_claim")
expect(unknownClaim.array).to(beNil())
expect(unknownClaim.string).to(beNil())
expect(unknownClaim.integer).to(beNil())
expect(unknownClaim.double).to(beNil())
expect(unknownClaim.date).to(beNil())
expect(unknownClaim.boolean).to(beNil())
}

context("raw claim") {
Expand Down

0 comments on commit a08d1c7

Please sign in to comment.