diff --git a/Auth0/Credentials.swift b/Auth0/Credentials.swift index 728473ac..413dd3bb 100644 --- a/Auth0/Credentials.swift +++ b/Auth0/Credentials.swift @@ -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 } - } diff --git a/Auth0/CredentialsManager.swift b/Auth0/CredentialsManager.swift index 5a97ba3a..bf634cf3 100644 --- a/Auth0/CredentialsManager.swift +++ b/Auth0/CredentialsManager.swift @@ -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 } @@ -160,7 +160,7 @@ public struct CredentialsManager { public func revoke(headers: [String: String] = [:], _ callback: @escaping (CredentialsManagerResult) -> 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(())) @@ -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 diff --git a/Auth0Tests/CredentialsManagerSpec.swift b/Auth0Tests/CredentialsManagerSpec.swift index 9a847ed6..08cd3132 100644 --- a/Auth0Tests/CredentialsManagerSpec.swift +++ b/Auth0Tests/CredentialsManagerSpec.swift @@ -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 @@ -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 { @@ -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 @@ -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 { diff --git a/Auth0Tests/CredentialsSpec.swift b/Auth0Tests/CredentialsSpec.swift index 9f6682d6..62f6a129 100644 --- a/Auth0Tests/CredentialsSpec.swift +++ b/Auth0Tests/CredentialsSpec.swift @@ -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()) } @@ -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 @@ -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 @@ -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) == ""