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

Replacing NSCoder with Swift's Codable #868

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 11 additions & 39 deletions Auth0/Credentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,44 +142,16 @@ extension Credentials: Codable {
scope: scope,
recoveryCode: recoveryCode)
}

}

// MARK: - NSSecureCoding

extension Credentials: NSSecureCoding {

/// `NSSecureCoding` decoding initializer.
public convenience init?(coder aDecoder: NSCoder) {
let accessToken = aDecoder.decodeObject(of: NSString.self, forKey: "accessToken")
let tokenType = aDecoder.decodeObject(of: NSString.self, forKey: "tokenType")
let idToken = aDecoder.decodeObject(of: NSString.self, forKey: "idToken")
let refreshToken = aDecoder.decodeObject(of: NSString.self, forKey: "refreshToken")
let expiresIn = aDecoder.decodeObject(of: NSDate.self, forKey: "expiresIn")
let scope = aDecoder.decodeObject(of: NSString.self, forKey: "scope")
let recoveryCode = aDecoder.decodeObject(of: NSString.self, forKey: "recoveryCode")

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

/// `Encodable` function.
public func encode(to encoder: any Encoder) throws {
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(accessToken, forKey: .accessToken)
try values.encode(tokenType, forKey: .tokenType)
try values.encode(idToken, forKey: .idToken)
try values.encode(refreshToken, forKey: .refreshToken)
try values.encode(scope, forKey: .scope)
try values.encode(recoveryCode, forKey: .recoveryCode)
try values.encode(expiresIn.timeIntervalSinceNow, forKey: .expiresIn)
}

/// `NSSecureCoding` encoding method.
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.accessToken as NSString, forKey: "accessToken")
aCoder.encode(self.tokenType as NSString, forKey: "tokenType")
aCoder.encode(self.idToken as NSString, forKey: "idToken")
aCoder.encode(self.refreshToken as NSString?, forKey: "refreshToken")
aCoder.encode(self.expiresIn as NSDate, forKey: "expiresIn")
aCoder.encode(self.scope as NSString?, forKey: "scope")
aCoder.encode(self.recoveryCode as NSString?, forKey: "recoveryCode")
}

/// Property that enables secure coding. Equals to `true`.
public static var supportsSecureCoding: Bool { return true }

}
6 changes: 3 additions & 3 deletions Auth0/CredentialsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public struct CredentialsManager {
/// - Parameter credentials: Credentials instance to store.
/// - Returns: If the credentials were stored.
public func store(credentials: Credentials) -> Bool {
guard let data = try? NSKeyedArchiver.archivedData(withRootObject: credentials, requiringSecureCoding: true) else {
guard let data = try? JSONEncoder().encode(credentials) else {
return false
}

Expand Down Expand Up @@ -160,7 +160,7 @@ public struct CredentialsManager {
public func revoke(headers: [String: String] = [:],
_ callback: @escaping (CredentialsManagerResult<Void>) -> Void) {
guard let data = self.storage.getEntry(forKey: self.storeKey),
let credentials = try? NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: data),
let credentials = try? JSONDecoder().decode(Credentials.self, from: data),
let refreshToken = credentials.refreshToken else {
_ = self.clear()
return callback(.success(()))
Expand Down Expand Up @@ -362,7 +362,7 @@ public struct CredentialsManager {

private func retrieveCredentials() -> Credentials? {
guard let data = self.storage.getEntry(forKey: self.storeKey) else { return nil }
return try? NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: data)
return try? JSONDecoder().decode(Credentials.self, from: data)
}

// swiftlint:disable:next function_body_length
Expand Down
10 changes: 4 additions & 6 deletions Auth0Tests/CredentialsManagerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ class CredentialsManagerSpec: QuickSpec {
waitUntil(timeout: Timeout) { done in
credentialsManager.credentials { result in
expect(result).to(beSuccessful())
let storedCredentials = try? NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: store.data(forKey: "credentials"))
let storedCredentials = try? JSONDecoder().decode(Credentials.self, from: store.data(forKey: "credentials"))
expect(storedCredentials?.accessToken) == NewAccessToken
expect(storedCredentials?.idToken) == NewIdToken
expect(storedCredentials?.refreshToken) == RefreshToken
Expand Down Expand Up @@ -561,8 +561,7 @@ class CredentialsManagerSpec: QuickSpec {
idToken: IdToken,
refreshToken: RefreshToken,
expiresIn: Date(timeIntervalSinceNow: -ExpiresIn))
let data = try? NSKeyedArchiver.archivedData(withRootObject: credentials,
requiringSecureCoding: true)
let data = try? JSONEncoder().encode(credentials)
return data
}
func setEntry(_ data: Data, forKey: String) -> Bool {
Expand Down Expand Up @@ -840,7 +839,7 @@ class CredentialsManagerSpec: QuickSpec {
waitUntil(timeout: Timeout) { done in
credentialsManager.renew { result in
expect(result).to(beSuccessful())
let storedCredentials = try? NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: store.data(forKey: "credentials"))
let storedCredentials = try? JSONDecoder().decode(Credentials.self, from: store.data(forKey: "credentials"))
expect(storedCredentials?.accessToken) == NewAccessToken
expect(storedCredentials?.idToken) == NewIdToken
expect(storedCredentials?.refreshToken) == NewRefreshToken
Expand All @@ -867,8 +866,7 @@ class CredentialsManagerSpec: QuickSpec {
class MockStore: CredentialsStorage {
func getEntry(forKey: String) -> Data? {
let credentials = Credentials(accessToken: AccessToken, idToken: IdToken, refreshToken: RefreshToken)
let data = try? NSKeyedArchiver.archivedData(withRootObject: credentials,
requiringSecureCoding: true)
let data = try? JSONEncoder().encode(credentials)
return data
}
func setEntry(_ data: Data, forKey: String) -> Bool {
Expand Down
16 changes: 8 additions & 8 deletions Auth0Tests/CredentialsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ class CredentialsSpec: QuickSpec {
expiresIn: ExpiresInDate,
scope: Scope,
recoveryCode: RecoveryCode)
let data = try NSKeyedArchiver.archivedData(withRootObject: original, requiringSecureCoding: true)
let credentials = try NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: data)!
let data = try JSONEncoder().encode(original)
let credentials = try JSONDecoder().decode(Credentials.self, from: data)
expect(credentials).toNot(beNil())
}

Expand All @@ -143,8 +143,8 @@ class CredentialsSpec: QuickSpec {
expiresIn: ExpiresInDate,
scope: Scope,
recoveryCode: RecoveryCode)
let data = try NSKeyedArchiver.archivedData(withRootObject: original, requiringSecureCoding: true)
let credentials = try NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: data)!
let data = try JSONEncoder().encode(original)
let credentials = try JSONDecoder().decode(Credentials.self, from: data)
expect(credentials.accessToken) == AccessToken
expect(credentials.tokenType) == Bearer
expect(credentials.idToken) == IdToken
Expand All @@ -158,8 +158,8 @@ class CredentialsSpec: QuickSpec {
tokenType: Bearer,
idToken: IdToken,
expiresIn: ExpiresInDate)
let data = try NSKeyedArchiver.archivedData(withRootObject: original, requiringSecureCoding: true)
let credentials = try NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: data)!
let data = try JSONEncoder().encode(original)
let credentials = try JSONDecoder().decode(Credentials.self, from: data)
expect(credentials.accessToken) == AccessToken
expect(credentials.tokenType) == Bearer
expect(credentials.idToken) == IdToken
Expand All @@ -171,8 +171,8 @@ class CredentialsSpec: QuickSpec {

it("should have only the default values") {
let original = Credentials()
let data = try NSKeyedArchiver.archivedData(withRootObject: original, requiringSecureCoding: true)
let credentials = try NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: data)!
let data = try JSONEncoder().encode(original)
let credentials = try JSONDecoder().decode(Credentials.self, from: data)
expect(credentials.accessToken) == ""
expect(credentials.tokenType) == ""
expect(credentials.idToken) == ""
Expand Down
Loading