From 01ac2fefb0f9bad3ad3c94f46a65ddb82c41371a Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Tue, 19 Jan 2021 10:59:46 -0800 Subject: [PATCH 1/2] Fix & tests for 439 --- Auth0/Credentials.swift | 19 ++++++++----------- Auth0Tests/CredentialsSpec.swift | 10 ++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Auth0/Credentials.swift b/Auth0/Credentials.swift index f4d9bd4a..d3188b2c 100644 --- a/Auth0/Credentials.swift +++ b/Auth0/Credentials.swift @@ -53,18 +53,15 @@ public class Credentials: NSObject, JSONObjectPayload, NSSecureCoding { } convenience required public init(json: [String: Any]) { - var expiresIn: Date? - switch json["expires_in"] { - case let string as String: - guard let double = Double(string) else { break } - expiresIn = Date(timeIntervalSinceNow: double) - case let int as Int: - expiresIn = Date(timeIntervalSinceNow: Double(int)) - case let double as Double: - expiresIn = Date(timeIntervalSinceNow: double) - default: - expiresIn = nil + var expiresIn: Date? = nil + + if let value = json["expires_in"] { + let string = String(describing: value) + if let double = NumberFormatter().number(from: string)?.doubleValue { + expiresIn = Date(timeIntervalSinceNow: double) + } } + self.init(accessToken: json["access_token"] as? String, tokenType: json["token_type"] as? String, idToken: json["id_token"] as? String, refreshToken: json["refresh_token"] as? String, expiresIn: expiresIn, scope: json["scope"] as? String) } diff --git a/Auth0Tests/CredentialsSpec.swift b/Auth0Tests/CredentialsSpec.swift index d8089813..9ed81add 100644 --- a/Auth0Tests/CredentialsSpec.swift +++ b/Auth0Tests/CredentialsSpec.swift @@ -85,6 +85,16 @@ class CredentialsSpec: QuickSpec { let credentials = Credentials(json: ["expires_in": 3600.0]) expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5)) } + + it("should have valid exiresIn from Int64") { + let credentials = Credentials(json: ["expires_in": Int64(3600)]) + expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5)) + } + + it("should have valid exiresIn from float") { + let credentials = Credentials(json: ["expires_in": Float(3600.0)]) + expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5)) + } it("should be nil") { let credentials = Credentials(json: ["expires_in": "invalid"]) From 4356d04b146857239e93080237a48d53faac9529 Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Tue, 19 Jan 2021 11:16:07 -0800 Subject: [PATCH 2/2] Fixes typo --- Auth0Tests/CredentialsSpec.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Auth0Tests/CredentialsSpec.swift b/Auth0Tests/CredentialsSpec.swift index 9ed81add..6389795b 100644 --- a/Auth0Tests/CredentialsSpec.swift +++ b/Auth0Tests/CredentialsSpec.swift @@ -71,27 +71,27 @@ class CredentialsSpec: QuickSpec { context("expires_in responses") { - it("should have valid exiresIn from string") { + it("should have valid expiresIn from string") { let credentials = Credentials(json: ["expires_in": "3600"]) expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5)) } - it("should have valid exiresIn from int") { + it("should have valid expiresIn from int") { let credentials = Credentials(json: ["expires_in": 3600]) expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5)) } - it("should have valid exiresIn from double") { + it("should have valid expiresIn from double") { let credentials = Credentials(json: ["expires_in": 3600.0]) expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5)) } - it("should have valid exiresIn from Int64") { + it("should have valid expiresIn from Int64") { let credentials = Credentials(json: ["expires_in": Int64(3600)]) expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5)) } - it("should have valid exiresIn from float") { + it("should have valid expiresIn from float") { let credentials = Credentials(json: ["expires_in": Float(3600.0)]) expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5)) }