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 scope property to Credentials #120

Merged
merged 1 commit into from
Jun 12, 2017
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
11 changes: 8 additions & 3 deletions Auth0/Credentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ public class Credentials: NSObject, JSONObjectPayload, NSSecureCoding {
public let refreshToken: String?
// Token that details the user identity after authentication
public let idToken: String?
// Granted scopes, only populated when a requested scope or scopes was not granted and Auth is OIDC Conformant
public let scope: String?

public init(accessToken: String? = nil, tokenType: String? = nil, idToken: String? = nil, refreshToken: String? = nil, expiresIn: Date? = nil) {
public init(accessToken: String? = nil, tokenType: String? = nil, idToken: String? = nil, refreshToken: String? = nil, expiresIn: Date? = nil, scope: String? = nil) {
self.accessToken = accessToken
self.tokenType = tokenType
self.idToken = idToken
self.refreshToken = refreshToken
self.expiresIn = expiresIn
self.scope = scope
}

convenience required public init(json: [String: Any]) {
Expand All @@ -62,7 +65,7 @@ public class Credentials: NSObject, JSONObjectPayload, NSSecureCoding {
default:
expiresIn = nil
}
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)
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)
}

// MARK: - NSSecureCoding
Expand All @@ -73,8 +76,9 @@ public class Credentials: NSObject, JSONObjectPayload, NSSecureCoding {
let idToken = aDecoder.decodeObject(forKey: "idToken")
let refreshToken = aDecoder.decodeObject(forKey: "refreshToken")
let expiresIn = aDecoder.decodeObject(forKey: "expiresIn")
let scope = aDecoder.decodeObject(forKey: "scope")

self.init(accessToken: accessToken as? String, tokenType: tokenType as? String, idToken: idToken as? String, refreshToken: refreshToken as? String, expiresIn: expiresIn as? Date)
self.init(accessToken: accessToken as? String, tokenType: tokenType as? String, idToken: idToken as? String, refreshToken: refreshToken as? String, expiresIn: expiresIn as? Date, scope: scope as? String)
}

public func encode(with aCoder: NSCoder) {
Expand All @@ -83,6 +87,7 @@ public class Credentials: NSObject, JSONObjectPayload, NSSecureCoding {
aCoder.encode(self.idToken, forKey: "idToken")
aCoder.encode(self.refreshToken, forKey: "refreshToken")
aCoder.encode(self.expiresIn, forKey: "expiresIn")
aCoder.encode(self.scope, forKey: "scope")
}

public static var supportsSecureCoding: Bool = true
Expand Down
12 changes: 9 additions & 3 deletions Auth0Tests/CredentialsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ private let Bearer = "bearer"
private let IdToken = UUID().uuidString.replacingOccurrences(of: "-", with: "")
private let RefreshToken = UUID().uuidString.replacingOccurrences(of: "-", with: "")
private let expiresIn: TimeInterval = 3600
private let Scope = "openid"

class CredentialsSpec: QuickSpec {
override func spec() {

describe("init from json") {

it("should have all tokens and token_type") {
let credentials = Credentials(json: ["access_token": AccessToken, "token_type": Bearer, "id_token": IdToken, "refresh_token": RefreshToken, "expires_in" : expiresIn])
let credentials = Credentials(json: ["access_token": AccessToken, "token_type": Bearer, "id_token": IdToken, "refresh_token": RefreshToken, "expires_in" : expiresIn, "scope": Scope])
expect(credentials).toNot(beNil())
expect(credentials.accessToken) == AccessToken
expect(credentials.tokenType) == Bearer
expect(credentials.idToken) == IdToken
expect(credentials.refreshToken) == RefreshToken
expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5))
expect(credentials.scope) == Scope
}

it("should have only access_token and token_type") {
Expand All @@ -52,6 +54,7 @@ class CredentialsSpec: QuickSpec {
expect(credentials.tokenType) == Bearer
expect(credentials.idToken).to(beNil())
expect(credentials.expiresIn).to(beNil())
expect(credentials.scope).to(beNil())
}

it("should have id_token") {
Expand Down Expand Up @@ -97,19 +100,20 @@ class CredentialsSpec: QuickSpec {
describe("secure coding") {

it("should unarchive as credentials type") {
let credentialsOrig = Credentials(json: ["access_token": AccessToken, "token_type": Bearer, "id_token": IdToken, "refresh_token": RefreshToken, "expires_in" : expiresIn])
let credentialsOrig = Credentials(json: ["access_token": AccessToken, "token_type": Bearer, "id_token": IdToken, "refresh_token": RefreshToken, "expires_in" : expiresIn, "scope" : Scope])
let saveData = NSKeyedArchiver.archivedData(withRootObject: credentialsOrig)
let credentials = NSKeyedUnarchiver.unarchiveObject(with: saveData)
expect(credentials as? Credentials).toNot(beNil())
}

it("should have all properties") {
let credentialsOrig = Credentials(json: ["access_token": AccessToken, "token_type": Bearer, "id_token": IdToken, "refresh_token": RefreshToken, "expires_in" : expiresIn])
let credentialsOrig = Credentials(json: ["access_token": AccessToken, "token_type": Bearer, "id_token": IdToken, "refresh_token": RefreshToken, "expires_in" : expiresIn, "scope" : Scope])
let saveData = NSKeyedArchiver.archivedData(withRootObject: credentialsOrig)
let credentials = NSKeyedUnarchiver.unarchiveObject(with: saveData) as! Credentials
expect(credentials.accessToken) == AccessToken
expect(credentials.tokenType) == Bearer
expect(credentials.idToken) == IdToken
expect(credentials.scope) == Scope
expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5))
}

Expand All @@ -121,6 +125,7 @@ class CredentialsSpec: QuickSpec {
expect(credentials.tokenType).to(beNil())
expect(credentials.idToken).to(beNil())
expect(credentials.expiresIn).to(beNil())
expect(credentials.scope).to(beNil())
}

it("should have refresh_token and expires_in only") {
Expand All @@ -132,6 +137,7 @@ class CredentialsSpec: QuickSpec {
expect(credentials.tokenType).to(beNil())
expect(credentials.idToken).to(beNil())
expect(credentials.expiresIn).to(beCloseTo(Date(timeIntervalSinceNow: expiresIn), within: 5))
expect(credentials.scope).to(beNil())
}

}
Expand Down