Skip to content

Commit

Permalink
Fixes uncovered testing keychain decoding across versions and instanc…
Browse files Browse the repository at this point in the history
…es (#12257)
  • Loading branch information
paulb777 authored Jan 10, 2024
1 parent 65990ba commit 013fbb2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private let kFiveMinutes = 5 * 60.0
@brief A class represents a credential that proves the identity of the app.
*/
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
@objc(FIRSecureTokenService) // objc Needed for decoding old versions
class SecureTokenService: NSObject, NSSecureCoding {
/** @property requestConfiguration
@brief The configuration for making requests to server.
Expand Down Expand Up @@ -126,7 +127,7 @@ class SecureTokenService: NSObject, NSSecureCoding {
let accessTokenExpirationDate = coder.decodeObject(
of: [NSDate.self], forKey: Self.kAccessTokenExpirationDateKey
) as? Date
// TODO: the nil matches the ObjC implementation, but doesn't seem right.
// requestConfiguration is filled in after User is set by Auth.protectedDataInitialization.
self.init(withRequestConfiguration: nil,
accessToken: accessToken,
accessTokenExpirationDate: accessTokenExpirationDate,
Expand Down
27 changes: 20 additions & 7 deletions FirebaseAuth/Sources/Swift/User/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1369,10 +1369,21 @@ extension User: NSSecureCoding {}
*/
var tokenService: SecureTokenService

private weak var _auth: Auth?
/** @property auth
@brief A weak reference to a FIRAuth instance associated with this instance.
*/
weak var auth: Auth?
weak var auth: Auth? {
set {
_auth = newValue
guard let requestConfiguration = auth?.requestConfiguration else {
fatalError("Firebase Auth Internal Error: nil requestConfiguration when initializing User")
}
tokenService.requestConfiguration = requestConfiguration
self.requestConfiguration = requestConfiguration
}
get { return _auth }
}

// MARK: Private functions

Expand Down Expand Up @@ -2102,8 +2113,8 @@ extension User: NSSecureCoding {}
of: NSString.self,
forKey: kFirebaseAppIDCodingKey
) as? String,
let tokenService = coder.decodeObject(forKey: kTokenServiceCodingKey)
as? SecureTokenService else {
let tokenService = coder.decodeObject(of: SecureTokenService.self,
forKey: kTokenServiceCodingKey) else {
return nil
}
let anonymous = coder.decodeBool(forKey: kAnonymousCodingKey)
Expand All @@ -2112,18 +2123,20 @@ extension User: NSSecureCoding {}
of: NSString.self,
forKey: kDisplayNameCodingKey
) as? String
let photoURL = coder.decodeObject(forKey: kPhotoURLCodingKey) as? URL
let photoURL = coder.decodeObject(of: NSURL.self, forKey: kPhotoURLCodingKey) as? URL
let email = coder.decodeObject(of: NSString.self, forKey: kEmailCodingKey) as? String
let phoneNumber = coder.decodeObject(
of: NSString.self,
forKey: kPhoneNumberCodingKey
) as? String
let emailVerified = coder.decodeBool(forKey: kEmailVerifiedCodingKey)
let providerData = coder.decodeObject(forKey: kProviderDataKey) as? [String: UserInfoImpl]
let metadata = coder.decodeObject(forKey: kMetadataCodingKey) as? UserMetadata
let classes = [NSDictionary.self, NSString.self, UserInfoImpl.self]
let providerData = coder.decodeObject(of: classes, forKey: kProviderDataKey)
as? [String: UserInfoImpl]
let metadata = coder.decodeObject(of: UserMetadata.self, forKey: kMetadataCodingKey)
let tenantID = coder.decodeObject(of: NSString.self, forKey: kTenantIDCodingKey) as? String
#if os(iOS)
let multiFactor = coder.decodeObject(forKey: kMultiFactorCodingKey) as? MultiFactor
let multiFactor = coder.decodeObject(of: MultiFactor.self, forKey: kMultiFactorCodingKey)
#endif
self.tokenService = tokenService
uid = userID
Expand Down
7 changes: 4 additions & 3 deletions FirebaseAuth/Sources/Swift/User/UserMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Foundation
@brief A data class representing the metadata corresponding to a Firebase user.
*/

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
@objc(FIRUserMetadata) open class UserMetadata: NSObject, NSSecureCoding {
/** @property lastSignInDate
@brief Stores the last sign in date for the corresponding Firebase user.
Expand Down Expand Up @@ -50,9 +51,9 @@ import Foundation

public required convenience init?(coder: NSCoder) {
let creationDate = coder.decodeObject(of: [NSDate.self],
forKey: UserMetadata.kCreationDateCodingKey) as? Date
forKey: UserMetadata.kCreationDateCodingKey)
let lastSignInDate = coder.decodeObject(of: [NSDate.self],
forKey: UserMetadata.kLastSignInDateCodingKey) as? Date
self.init(withCreationDate: creationDate, lastSignInDate: lastSignInDate)
forKey: UserMetadata.kLastSignInDateCodingKey)
self.init(withCreationDate: creationDate as? Date, lastSignInDate: lastSignInDate as? Date)
}
}

0 comments on commit 013fbb2

Please sign in to comment.