Skip to content

Commit

Permalink
[auth-swift] Consistent NSSecureCoding decode
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 committed Jan 11, 2024
1 parent 013fbb2 commit fa3c1e7
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 126 deletions.
27 changes: 15 additions & 12 deletions FirebaseAuth/Sources/Swift/Auth/AuthDataResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@

import Foundation

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
extension AuthDataResult: NSSecureCoding {}

/** @class AuthDataResult
@brief Helper object that contains the result of a successful sign-in, link and reauthenticate
action. It contains references to a `User` instance and a `AdditionalUserInfo` instance.
*/
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
@objc(FIRAuthDataResult) open class AuthDataResult: NSObject, NSSecureCoding {
@objc(FIRAuthDataResult) open class AuthDataResult: NSObject {
/** @property user
@brief The signed in user.
*/
Expand All @@ -37,10 +40,6 @@ import Foundation
*/
@objc public let credential: OAuthCredential?

private let kAdditionalUserInfoCodingKey = "additionalUserInfo"
private let kUserCodingKey = "user"
private let kCredentialCodingKey = "credential"

/** @fn initWithUser:additionalUserInfo:
@brief Designated initializer.
@param user The signed in user reference.
Expand All @@ -55,9 +54,13 @@ import Foundation
self.credential = credential
}

public static var supportsSecureCoding: Bool {
return true
}
// MARK: Secure Coding

private let kAdditionalUserInfoCodingKey = "additionalUserInfo"
private let kUserCodingKey = "user"
private let kCredentialCodingKey = "credential"

public static var supportsSecureCoding = true

public func encode(with coder: NSCoder) {
coder.encode(user, forKey: kUserCodingKey)
Expand All @@ -66,12 +69,12 @@ import Foundation
}

public required init?(coder: NSCoder) {
guard let user = coder.decodeObject(forKey: kUserCodingKey) as? User else {
guard let user = coder.decodeObject(of: User.self, forKey: kUserCodingKey) else {
return nil
}
self.user = user
additionalUserInfo = coder.decodeObject(forKey: kAdditionalUserInfoCodingKey)
as? AdditionalUserInfo
credential = coder.decodeObject(forKey: kCredentialCodingKey) as? OAuthCredential
additionalUserInfo = coder.decodeObject(of: AdditionalUserInfo.self,
forKey: kAdditionalUserInfoCodingKey)
credential = coder.decodeObject(of: OAuthCredential.self, forKey: kCredentialCodingKey)
}
}
62 changes: 0 additions & 62 deletions FirebaseAuth/Sources/Swift/Auth/AuthTokenResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,6 @@

import Foundation

/** @var kExpirationDateKey
@brief The key used to encode the expirationDate property for NSSecureCoding.
*/
// XXX TODO: TYPO IN ORIGINAL KEY. TO FIX OR NOT?
private let kExpirationDateKey = "expiratinDate"

/** @var kTokenKey
@brief The key used to encode the token property for NSSecureCoding.
*/
private let kTokenKey = "token"

/** @var kAuthDateKey
@brief The key used to encode the authDate property for NSSecureCoding.
*/
private let kAuthDateKey = "authDate"

/** @var kIssuedDateKey
@brief The key used to encode the issuedDate property for NSSecureCoding.
*/
private let kIssuedDateKey = "issuedDate"

/** @var kSignInProviderKey
@brief The key used to encode the signInProvider property for NSSecureCoding.
*/
private let kSignInProviderKey = "signInProvider"

/** @var kSignInSecondFactorKey
@brief The key used to encode the signInSecondFactor property for NSSecureCoding.
*/
private let kSignInSecondFactorKey = "signInSecondFactor"

/** @var kClaimsKey
@brief The key used to encode the claims property for NSSecureCoding.
*/
private let kClaimsKey = "claims"

/** @class FIRAuthTokenResult
@brief A data class containing the ID token JWT string and other properties associated with the
token including the decoded payload claims.
Expand Down Expand Up @@ -181,29 +145,3 @@ struct JWT: Decodable {
let iat: Date
let firebase: FirebasePayload
}

/*
@implementation FIRAuthTokenResult

+ (nullable FIRAuthTokenResult *)tokenResultWithToken:(NSString *)token {

}

#pragma mark - NSSecureCoding

+ (BOOL)supportsSecureCoding {
return YES;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSString *token = [aDecoder decodeObjectOfClass:[NSDate class] forKey:kTokenKey];
return [FIRAuthTokenResult tokenResultWithToken:token];
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_token forKey:kTokenKey];
}

@end

*/
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import Foundation
}

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
class EmailAuthCredential: AuthCredential, NSSecureCoding {
@objc(FIREmailPasswordAuthCredential) class EmailAuthCredential: AuthCredential, NSSecureCoding {
let email: String

enum EmailType {
Expand All @@ -67,6 +67,8 @@ class EmailAuthCredential: AuthCredential, NSSecureCoding {
super.init(provider: EmailAuthProvider.id)
}

// MARK: Secure Coding

public static var supportsSecureCoding = true

public func encode(with coder: NSCoder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import Foundation
}

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
@objc class FacebookAuthCredential: AuthCredential, NSSecureCoding {
@objc(FIRFacebookAuthCredential) class FacebookAuthCredential: AuthCredential, NSSecureCoding {
let accessToken: String

init(withAccessToken accessToken: String) {
Expand All @@ -50,7 +50,9 @@ import Foundation
request.providerAccessToken = accessToken
}

static var supportsSecureCoding = true
// MARK: Secure Coding

public static var supportsSecureCoding = true

func encode(with coder: NSCoder) {
coder.encode(accessToken, forKey: "accessToken")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@
}

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
class GameCenterAuthCredential: AuthCredential, NSSecureCoding {
@objc(FIRGameCenterAuthCredential) class GameCenterAuthCredential: AuthCredential,
NSSecureCoding {
let playerID: String
let teamPlayerID: String?
let gamePlayerID: String?
Expand Down Expand Up @@ -168,7 +169,9 @@
super.init(provider: GameCenterAuthProvider.id)
}

static var supportsSecureCoding = true
// MARK: Secure Coding

public static var supportsSecureCoding = true

func encode(with coder: NSCoder) {
coder.encode(playerID, forKey: "playerID")
Expand All @@ -191,7 +194,7 @@
of: NSString.self,
forKey: "gamePlayerID"
) as? String,
let timestamp = coder.decodeObject(forKey: "timestamp") as? UInt64,
let timestamp = coder.decodeObject(of: NSNumber.self, forKey: "timestamp") as? UInt64,
let displayName = coder.decodeObject(
of: NSString.self,
forKey: "displayName"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import Foundation
}

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
class GitHubAuthCredential: AuthCredential, NSSecureCoding {
@objc(FIRGitHubAuthCredential) class GitHubAuthCredential: AuthCredential, NSSecureCoding {
let token: String

init(withToken token: String) {
Expand All @@ -50,7 +50,9 @@ class GitHubAuthCredential: AuthCredential, NSSecureCoding {
request.providerAccessToken = token
}

static var supportsSecureCoding = true
// MARK: Secure Coding

public static var supportsSecureCoding = true

func encode(with coder: NSCoder) {
coder.encode(token, forKey: "token")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ import Foundation
request.providerAccessToken = accessToken
}

static var supportsSecureCoding = true
// MARK: Secure Coding

public static var supportsSecureCoding = true

func encode(with coder: NSCoder) {
coder.encode(idToken, forKey: "idToken")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ import Foundation
accessToken = coder.decodeObject(of: NSString.self, forKey: "accessToken") as? String
pendingToken = coder.decodeObject(of: NSString.self, forKey: "pendingToken") as? String
secret = coder.decodeObject(of: NSString.self, forKey: "secret") as? String
fullName = coder.decodeObject(forKey: "fullName") as? PersonNameComponents
fullName = coder.decodeObject(of: NSPersonNameComponents.self, forKey: "fullName")
as? PersonNameComponents
OAuthResponseURLString = nil
sessionID = nil
super.init(provider: OAuthProvider.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import Foundation
super.init(provider: providerID)
}

// MARK: Secure Coding

public static var supportsSecureCoding = true

public func encode(with coder: NSCoder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import Foundation
}

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
class TwitterAuthCredential: AuthCredential, NSSecureCoding {
@objc(FIRTwitterAuthCredential) class TwitterAuthCredential: AuthCredential, NSSecureCoding {
let token: String
let secret: String

Expand All @@ -54,7 +54,9 @@ class TwitterAuthCredential: AuthCredential, NSSecureCoding {
request.providerOAuthTokenSecret = secret
}

static var supportsSecureCoding = true
// MARK: Secure Coding

public static var supportsSecureCoding = true

func encode(with coder: NSCoder) {
coder.encode(token, forKey: "token")
Expand Down
4 changes: 3 additions & 1 deletion FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import Foundation

#if os(iOS)

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
extension MultiFactor: NSSecureCoding {}
/** @class FIRMultiFactor
@brief The interface defining the multi factor related properties and operations pertaining to a
user.
This class is available on iOS only.
*/
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
@objc(FIRMultiFactor) open class MultiFactor: NSObject, NSSecureCoding {
@objc(FIRMultiFactor) open class MultiFactor: NSObject {
@objc open var enrolledFactors: [MultiFactorInfo]

/** @fn getSessionWithCompletion:
Expand Down
3 changes: 0 additions & 3 deletions FirebaseAuth/Sources/Swift/MultiFactor/MultiFactorInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ import Foundation

#if os(iOS)
private let kUIDCodingKey = "uid"

private let kDisplayNameCodingKey = "displayName"

private let kEnrollmentDateCodingKey = "enrollmentDate"

private let kFactorIDCodingKey = "factorID"

/** @class FIRMultiFactorInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import Foundation

private let kPhoneNumberCodingKey = "phoneNumber"

// Workaround Cannot override mutable property with read-only property 'supportsSecureCoding'
private static var secureCodingWorkaround = true
override open class var supportsSecureCoding: Bool { return secureCodingWorkaround }

Expand Down
31 changes: 13 additions & 18 deletions FirebaseAuth/Sources/Swift/SystemService/AuthAppCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,10 @@

import Foundation

/** @var kReceiptKey
@brief The key used to encode the receipt property for NSSecureCoding.
*/
private let kReceiptKey = "receipt"

/** @var kSecretKey
@brief The key used to encode the secret property for NSSecureCoding.
*/
private let kSecretKey = "secret"

/** @class FIRAuthAppCredential
@brief A class represents a credential that proves the identity of the app.
*/
class AuthAppCredential: NSObject, NSSecureCoding {
@objc(FIRAuthAppCredential) class AuthAppCredential: NSObject, NSSecureCoding {
/** @property receipt
@brief The server acknowledgement of receiving client's claim of identity.
*/
Expand All @@ -51,21 +41,26 @@ class AuthAppCredential: NSObject, NSSecureCoding {

// MARK: NSSecureCoding

static var supportsSecureCoding: Bool {
private static let kReceiptKey = "receipt"
private static let kSecretKey = "secret"

public static var supportsSecureCoding: Bool {
true
}

required convenience init?(coder: NSCoder) {
guard let receipt = coder.decodeObject(of: [NSString.self], forKey: kReceiptKey) as? String
public required convenience init?(coder: NSCoder) {
guard let receipt = coder.decodeObject(of: NSString.self,
forKey: AuthAppCredential.kReceiptKey) as? String
else {
return nil
}
let secret = coder.decodeObject(of: [NSString.self], forKey: kSecretKey) as? String
let secret = coder.decodeObject(of: NSString.self,
forKey: AuthAppCredential.kSecretKey) as? String
self.init(receipt: receipt, secret: secret)
}

func encode(with coder: NSCoder) {
coder.encode(receipt, forKey: kReceiptKey)
coder.encode(secret, forKey: kSecretKey)
public func encode(with coder: NSCoder) {
coder.encode(receipt, forKey: AuthAppCredential.kReceiptKey)
coder.encode(secret, forKey: AuthAppCredential.kSecretKey)
}
}
10 changes: 0 additions & 10 deletions FirebaseAuth/Sources/Swift/SystemService/SecureTokenService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@

import Foundation

/** @var kReceiptKey
@brief The key used to encode the receipt property for NSSecureCoding.
*/
private let kReceiptKey = "receipt"

/** @var kSecretKey
@brief The key used to encode the secret property for NSSecureCoding.
*/
private let kSecretKey = "secret"

private let kFiveMinutes = 5 * 60.0

/** @class FIRAuthAppCredential
Expand Down
Loading

0 comments on commit fa3c1e7

Please sign in to comment.