Skip to content

Commit

Permalink
[auth-swift] Property Swift optionality tests (#11915)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 authored Oct 10, 2023
1 parent abcbae7 commit f964d34
Show file tree
Hide file tree
Showing 21 changed files with 427 additions and 151 deletions.
169 changes: 169 additions & 0 deletions FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <AvailabilityMacros.h>
#import <Foundation/Foundation.h>

@class FIRActionCodeSettings;
@class FIRApp;
@class FIRAuth;
@class FIRAuthCredential;
@class FIRAuthDataResult;
@class FIRAuthSettings;
@class FIRUser;
@protocol FIRAuthUIDelegate;
@protocol FIRFederatedAuthProvider;

NS_ASSUME_NONNULL_BEGIN

/** @typedef FIRUserUpdateCallback
@brief The type of block invoked when a request to update the current user is completed.
*/
typedef void (^FIRUserUpdateCallback)(NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRAuthStateDidChangeListenerHandle
@brief The type of handle returned by `Auth.addAuthStateDidChangeListener(_:)`.
*/
// clang-format off
// clang-format12 merges the next two lines.
typedef id<NSObject> FIRAuthStateDidChangeListenerHandle
NS_SWIFT_NAME(AuthStateDidChangeListenerHandle);
// clang-format on

/** @typedef FIRAuthStateDidChangeListenerBlock
@brief The type of block which can be registered as a listener for auth state did change events.
@param auth The Auth object on which state changes occurred.
@param user Optionally; the current signed in user, if any.
*/
typedef void (^FIRAuthStateDidChangeListenerBlock)(FIRAuth *auth, FIRUser *_Nullable user)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRIDTokenDidChangeListenerHandle
@brief The type of handle returned by `Auth.addIDTokenDidChangeListener(_:)`.
*/
// clang-format off
// clang-format12 merges the next two lines.
typedef id<NSObject> FIRIDTokenDidChangeListenerHandle
NS_SWIFT_NAME(IDTokenDidChangeListenerHandle);
// clang-format on

/** @typedef FIRIDTokenDidChangeListenerBlock
@brief The type of block which can be registered as a listener for ID token did change events.
@param auth The Auth object on which ID token changes occurred.
@param user Optionally; the current signed in user, if any.
*/
typedef void (^FIRIDTokenDidChangeListenerBlock)(FIRAuth *auth, FIRUser *_Nullable user)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRAuthDataResultCallback
@brief The type of block invoked when sign-in related events complete.
@param authResult Optionally; Result of sign-in request containing both the user and
the additional user info associated with the user.
@param error Optionally; the error which occurred - or nil if the request was successful.
*/
typedef void (^FIRAuthDataResultCallback)(FIRAuthDataResult *_Nullable authResult,
NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
/**
@brief The name of the `NSNotificationCenter` notification which is posted when the auth state
changes (for example, a new token has been produced, a user signs in or signs out). The
object parameter of the notification is the sender `Auth` instance.
*/
extern const NSNotificationName FIRAuthStateDidChangeNotification NS_SWIFT_NAME(AuthStateDidChange);

/** @typedef FIRAuthResultCallback
@brief The type of block invoked when sign-in related events complete.
@param user Optionally; the signed in user, if any.
@param error Optionally; if an error occurs, this is the NSError object that describes the
problem. Set to nil otherwise.
*/
typedef void (^FIRAuthResultCallback)(FIRUser *_Nullable user, NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRProviderQueryCallback
@brief The type of block invoked when a list of identity providers for a given email address is
requested.
@param providers Optionally; a list of provider identifiers, if any.
@see GoogleAuthProviderID etc.
@param error Optionally; if an error occurs, this is the NSError object that describes the
problem. Set to nil otherwise.
*/
typedef void (^FIRProviderQueryCallback)(NSArray<NSString *> *_Nullable providers,
NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRSignInMethodQueryCallback
@brief The type of block invoked when a list of sign-in methods for a given email address is
requested.
*/
typedef void (^FIRSignInMethodQueryCallback)(NSArray<NSString *> *_Nullable, NSError *_Nullable)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRSendPasswordResetCallback
@brief The type of block invoked when sending a password reset email.
@param error Optionally; if an error occurs, this is the NSError object that describes the
problem. Set to nil otherwise.
*/
typedef void (^FIRSendPasswordResetCallback)(NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRSendSignInLinkToEmailCallback
@brief The type of block invoked when sending an email sign-in link email.
*/
typedef void (^FIRSendSignInLinkToEmailCallback)(NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRConfirmPasswordResetCallback
@brief The type of block invoked when performing a password reset.
@param error Optionally; if an error occurs, this is the NSError object that describes the
problem. Set to nil otherwise.
*/
typedef void (^FIRConfirmPasswordResetCallback)(NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRVerifyPasswordResetCodeCallback
@brief The type of block invoked when verifying that an out of band code should be used to
perform password reset.
@param email Optionally; the email address of the user for which the out of band code applies.
@param error Optionally; if an error occurs, this is the NSError object that describes the
problem. Set to nil otherwise.
*/
typedef void (^FIRVerifyPasswordResetCodeCallback)(NSString *_Nullable email,
NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

/** @typedef FIRApplyActionCodeCallback
@brief The type of block invoked when applying an action code.
@param error Optionally; if an error occurs, this is the NSError object that describes the
problem. Set to nil otherwise.
*/
typedef void (^FIRApplyActionCodeCallback)(NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

typedef void (^FIRAuthVoidErrorCallback)(NSError *_Nullable)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions FirebaseAuth/Sources/Public/FirebaseAuth/FirebaseAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
#import <Foundation/Foundation.h>

// TODO: restore other headers with typedefs and string constants for CocoaPods.
#import "FIRAuth.h"
#import "FIRMultiFactor.h"
7 changes: 2 additions & 5 deletions FirebaseAuth/Sources/Swift/ActionCode/ActionCodeInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ import Foundation
@brief The email address to which the code was sent. The new email address in the case of
`ActionCodeOperationRecoverEmail`.
*/
@objc public let email: String
@objc public let email: String?

/** @property previousEmail
@brief The email that is being recovered in the case of `ActionCodeOperationRecoverEmail`.
*/
@objc public let previousEmail: String?

// TODO: Below here change to internal.

@objc public init(withOperation operation: ActionCodeOperation, email: String,
newEmail: String?) {
init(withOperation operation: ActionCodeOperation, email: String, newEmail: String?) {
self.operation = operation
if let newEmail {
self.email = newEmail
Expand Down
2 changes: 1 addition & 1 deletion FirebaseAuth/Sources/Swift/ActionCode/ActionCodeURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Foundation
/** @property APIKey
@brief Returns the API key from the link. nil, if not provided.
*/
@objc public let apiKey: String?
@objc(APIKey) public let apiKey: String?

/** @property operation
@brief Returns the mode of oob action. The property will be of `FIRActionCodeOperation` type.
Expand Down
19 changes: 15 additions & 4 deletions FirebaseAuth/Sources/Swift/Auth/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,8 @@ extension Auth: AuthInterop {
refreshToken: response.refreshToken,
anonymous: true
)
let additionalUserInfo = AdditionalUserInfo(providerID: nil,
// TODO: The ObjC implementation passed a nil providerID to the nonnull providerID
let additionalUserInfo = AdditionalUserInfo(providerID: "",
profile: nil,
username: nil,
isNewUser: true)
Expand Down Expand Up @@ -844,7 +845,8 @@ extension Auth: AuthInterop {
refreshToken: response.refreshToken,
anonymous: false
)
let additionalUserInfo = AdditionalUserInfo(providerID: nil,
// TODO: The ObjC implementation passed a nil providerID to the nonnull providerID
let additionalUserInfo = AdditionalUserInfo(providerID: "",
profile: nil,
username: nil,
isNewUser: response.isNewUser)
Expand Down Expand Up @@ -1486,7 +1488,7 @@ extension Auth: AuthInterop {
kAuthGlobalWorkQueue.sync {
self.requestConfiguration.emulatorHostAndPort = "\(formattedHost):\(port)"
#if os(iOS)
self.settings?.appVerificationDisabledForTesting = true
self.settings?.isAppVerificationDisabledForTesting = true
#endif
}
}
Expand Down Expand Up @@ -1591,6 +1593,12 @@ extension Auth: AuthInterop {
}

#if os(iOS)
@objc(APNSToken) public var apnsToken: Data? {
kAuthGlobalWorkQueue.sync {
self.tokenManager.token?.data
}
}

@objc public func setAPNSToken(_ token: Data, type: AuthAPNSTokenType) {
kAuthGlobalWorkQueue.sync {
self.tokenManager.token = AuthAPNSToken(withData: token, type: type)
Expand Down Expand Up @@ -2122,7 +2130,10 @@ extension Auth: AuthInterop {
.approximateExpirationDate,
refreshToken: response.refreshToken,
anonymous: false)
let additionalUserInfo = AdditionalUserInfo.userInfo(verifyAssertionResponse: response)
let additionalUserInfo = AdditionalUserInfo(providerID: providerID,
profile: response.profile,
username: response.username,
isNewUser: response.isNewUser)
let updatedOAuthCredential = OAuthCredential(withVerifyAssertionResponse: response)
return AuthDataResult(withUser: user,
additionalUserInfo: additionalUserInfo,
Expand Down
10 changes: 3 additions & 7 deletions FirebaseAuth/Sources/Swift/Auth/AuthSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,17 @@ import Foundation
/** @property appVerificationDisabledForTesting
@brief Flag to determine whether app verification should be disabled for testing or not.
*/
@objc public var appVerificationDisabledForTesting: Bool

@objc var isAppVerificationDisabledForTesting: Bool {
return appVerificationDisabledForTesting
}
@objc public var isAppVerificationDisabledForTesting: Bool

override init() {
appVerificationDisabledForTesting = false
isAppVerificationDisabledForTesting = false
}

// MARK: NSCopying

public func copy(with zone: NSZone? = nil) -> Any {
let settings = AuthSettings()
settings.appVerificationDisabledForTesting = appVerificationDisabledForTesting
settings.isAppVerificationDisabledForTesting = isAppVerificationDisabledForTesting
return settings
}
}
4 changes: 2 additions & 2 deletions FirebaseAuth/Sources/Swift/Auth/AuthTokenResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private let kClaimsKey = "claims"
/** @property signInSecondFactor
@brief Stores sign-in second factor through which the token was obtained.
*/
@objc public var signInSecondFactor: String?
@objc public var signInSecondFactor: String

/** @property claims
@brief Stores the entire payload of claims found on the ID token. This includes the standard
Expand Down Expand Up @@ -165,7 +165,7 @@ private let kClaimsKey = "claims"
self.authDate = authDate
self.issuedAtDate = issuedAtDate
self.signInProvider = signInProvider
self.signInSecondFactor = signInSecondFactor
self.signInSecondFactor = signInSecondFactor ?? ""
self.claims = claims
}
}
Expand Down
8 changes: 4 additions & 4 deletions FirebaseAuth/Sources/Swift/AuthProvider/OAuthProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import CommonCrypto
/** @property scopes
@brief Array used to configure the OAuth scopes.
*/
@objc public var scopes: [String]
@objc public var scopes: [String]?

/** @property customParameters
@brief Dictionary used to configure the OAuth custom parameters.
*/
@objc public var customParameters: [String: String]
@objc public var customParameters: [String: String]?

/** @property providerID
@brief The provider ID indicating the specific OAuth provider this OAuthProvider instance
Expand Down Expand Up @@ -363,10 +363,10 @@ import CommonCrypto
if let tenantID {
urlArguments["tid"] = tenantID
}
if scopes.count > 0 {
if let scopes, scopes.count > 0 {
urlArguments["scopes"] = scopes.joined(separator: ",")
}
if customParameters.count > 0 {
if let customParameters, customParameters.count > 0 {
do {
let customParametersJSONData = try JSONSerialization
.data(withJSONObject: customParameters)
Expand Down
8 changes: 5 additions & 3 deletions FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Foundation
*/
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
@objc(FIRMultiFactor) public class MultiFactor: NSObject, NSSecureCoding {
@objc public var enrolledFactors: [MultiFactorInfo]?
@objc public var enrolledFactors: [MultiFactorInfo]

/** @fn getSessionWithCompletion:
@brief Get a session for a second factor enrollment operation.
Expand Down Expand Up @@ -280,7 +280,9 @@ import Foundation
enrolledFactors = multiFactorInfoArray
}

override init() {}
override init() {
enrolledFactors = []
}

// MARK: - NSSecureCoding

Expand All @@ -300,7 +302,7 @@ import Foundation
TOTPMultiFactorInfo.self]
let enrolledFactors = coder
.decodeObject(of: classes, forKey: kEnrolledFactorsCodingKey) as? [MultiFactorInfo]
self.enrolledFactors = enrolledFactors
self.enrolledFactors = enrolledFactors ?? []
// Do not decode `user` weak property.
}
}
Expand Down
Loading

0 comments on commit f964d34

Please sign in to comment.