From a08d1c77868639d8e787794eb5aac6b5d63c334b Mon Sep 17 00:00:00 2001 From: Reda Lemeden Date: Tue, 2 Feb 2021 14:05:59 +0100 Subject: [PATCH] Add support for boolean claims (#121) --- JWTDecode/JWTDecode.swift | 17 ++++++---- .../JWTDecode.swiftTests/JWTDecodeSpec.swift | 33 ++++++++++++++++--- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/JWTDecode/JWTDecode.swift b/JWTDecode/JWTDecode.swift index 16cfb2a..3b1b926 100644 --- a/JWTDecode/JWTDecode.swift +++ b/JWTDecode/JWTDecode.swift @@ -89,12 +89,17 @@ 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 @@ -102,12 +107,12 @@ public struct Claim { /// 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 @@ -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 { diff --git a/Tests/JWTDecode.swiftTests/JWTDecodeSpec.swift b/Tests/JWTDecode.swiftTests/JWTDecodeSpec.swift index f57239d..2f8cb36 100644 --- a/Tests/JWTDecode.swiftTests/JWTDecodeSpec.swift +++ b/Tests/JWTDecode.swiftTests/JWTDecodeSpec.swift @@ -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") { @@ -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") { @@ -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") { @@ -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") {