Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for a wider variety of primitive types for the Credentials expiration date #440

Merged
merged 3 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions Auth0/Credentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
16 changes: 13 additions & 3 deletions Auth0Tests/CredentialsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,30 @@ 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 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 expiresIn 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"])
Expand Down