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

MFA beta #4823

Merged
merged 21 commits into from
Mar 10, 2020
60 changes: 27 additions & 33 deletions Example/Auth/ApiTests/PhoneMultiFactorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ class PhoneMultiFactorTests: FIRAuthApiTestsBase {
let enrollExpectation = self.expectation(description: "Enroll phone multi factor finished.")
let unenrollExpectation = self.expectation(description: "Unenroll phone multi factor finished.")
Auth.auth().signIn(withEmail: kNoSecondFactorUserEmail, password: kNoSecondFactorUserPassword) { (result, error) in
renkelvin marked this conversation as resolved.
Show resolved Hide resolved
if (error != nil) {
XCTFail(String(format: "User normal sign in failed. Error: %@", error!.localizedDescription))
}
XCTAssertNil(String(format: "User normal sign in failed. Error: %@", error!.localizedDescription))

// Enroll
var user = result?.user
user?.multiFactor.getSessionWithCompletion({ (session, error) in
if (error != nil) {
XCTFail(String(format: "Get multi factor session failed. Error: %@", error!.localizedDescription))
}
guard let user = result?.user else {
XCTFail("No valid user after attempted sign-in.")
}
user.multiFactor.getSessionWithCompletion({ (session, error) in
XCTAssertNil(String(format: "Get multi factor session failed. Error: %@", error!.localizedDescription))
renkelvin marked this conversation as resolved.
Show resolved Hide resolved
PhoneAuthProvider.provider().verifyPhoneNumber(
kPhoneSecondFactorPhoneNumber,
uiDelegate: nil,
Expand All @@ -63,9 +61,7 @@ class PhoneMultiFactorTests: FIRAuthApiTestsBase {
// Unenroll
user = Auth.auth().currentUser
user?.multiFactor.unenroll(with: (user?.multiFactor.enrolledFactors.first)!, completion: { (error) in
if (error != nil) {
XCTFail(String(format: "Phone multi factor unenroll failed. Error: %@", error!.localizedDescription))
}
XCTAssertNil(String(format: "Phone multi factor unenroll failed. Error: %@", error!.localizedDescription))
XCTAssertEqual(Auth.auth().currentUser?.multiFactor.enrolledFactors.count, 0)
unenrollExpectation.fulfill()
})
Expand All @@ -81,34 +77,32 @@ class PhoneMultiFactorTests: FIRAuthApiTestsBase {
}
}

func testSignIn() {
func testSignInWithSecondFactor() {
let signInExpectation = self.expectation(description: "Sign in with phone multi factor finished.")
Auth.auth().signIn(withEmail: kOneSecondFactorUserEmail, password: kOneSecondFactorUserPassword) { (result, error) in
// SignIn
let authError = error as NSError?
if (authError == nil || authError!.code != AuthErrorCode.secondFactorRequired.rawValue) {
guard let error = error, error.code == AuthErrorCode.secondFactorRequired.rawValue else {
XCTFail(String(format: "User sign in returns wrong error. Error: %@", error!.localizedDescription))
} else {
let resolver = authError!.userInfo["FIRAuthErrorUserInfoMultiFactorResolverKey"] as! MultiFactorResolver
let hint = resolver.hints.first as! PhoneMultiFactorInfo
PhoneAuthProvider.provider().verifyPhoneNumber(
with: hint,
uiDelegate: nil,
multiFactorSession: resolver.session) { (verificationId, error) in
}
let resolver = authError!.userInfo["FIRAuthErrorUserInfoMultiFactorResolverKey"] as! MultiFactorResolver
renkelvin marked this conversation as resolved.
Show resolved Hide resolved
let hint = resolver.hints.first as! PhoneMultiFactorInfo
PhoneAuthProvider.provider().verifyPhoneNumber(
with: hint,
uiDelegate: nil,
multiFactorSession: resolver.session) { (verificationId, error) in
if error != nil {
renkelvin marked this conversation as resolved.
Show resolved Hide resolved
XCTFail(String(format: "Failed to verify phone number. Error: %@", error!.localizedDescription))
}
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationId!,
verificationCode: kPhoneSecondFactorVerificationCode)
let assertion = PhoneMultiFactorGenerator.assertion(with: credential);
resolver.resolveSignIn(with: assertion) { (authResult, error) in
if error != nil {
XCTFail(String(format: "Failed to verify phone number. Error: %@", error!.localizedDescription))
XCTFail(String(format: "Failed to sign in with phone multi factor. Error: %@", error!.localizedDescription))
}
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationId!,
verificationCode: kPhoneSecondFactorVerificationCode)
let assertion = PhoneMultiFactorGenerator.assertion(with: credential);
resolver.resolveSignIn(with: assertion) { (authResult, error) in
if error != nil {
XCTFail(String(format: "Failed to sign in with phone multi factor. Error: %@", error!.localizedDescription))
}
signInExpectation.fulfill()
}
}
signInExpectation.fulfill()
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+App.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+Auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "FIRAuth.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+AutoTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+Custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+Email.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "FIRAuth.h"
Expand Down
4 changes: 2 additions & 2 deletions Example/Auth/Sample/MainViewController+Email.m
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ - (void)signInEmailPassword {
FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
[resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
if (error) {
[self logFailure:@"Multi factor finanlize sign in failed." error:error];
[self logFailure:@"Multi factor finalize sign in failed." error:error];
} else {
[self logSuccess:@"Multi factor finanlize sign in succeeded."];
[self logSuccess:@"Multi factor finalize sign in succeeded."];
}
}];
}];
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+Facebook.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"
Expand Down
1 change: 1 addition & 0 deletions Example/Auth/Sample/MainViewController+GameCenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

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

#import "MainViewController.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+Google.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"
Expand Down
4 changes: 2 additions & 2 deletions Example/Auth/Sample/MainViewController+Google.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ - (void)signInGoogle {
FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
[resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
if (error) {
[self logFailure:@"Multi factor finanlize sign in failed." error:error];
[self logFailure:@"Multi factor finalize sign in failed." error:error];
} else {
[self logSuccess:@"Multi factor finanlize sign in succeeded."];
[self logSuccess:@"Multi factor finalize sign in succeeded."];
}
}];
}];
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "FirebaseAuth.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+MultiFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"
renkelvin marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
60 changes: 30 additions & 30 deletions Example/Auth/Sample/MainViewController+MultiFactor.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,56 @@ - (StaticContentTableViewSection *)multiFactorSection {
__weak typeof(self) weakSelf = self;
return [StaticContentTableViewSection sectionWithTitle:@"Multi Factor" cells:@[
[StaticContentTableViewCell cellWithTitle:@"Phone Enroll"
action:^{ [weakSelf phoneEnroll]; }],
action:^{ [weakSelf phoneEnroll]; }],
[StaticContentTableViewCell cellWithTitle:@"Phone Unenroll"
action:^{ [weakSelf phoneUnenroll]; }],
]];
action:^{ [weakSelf phoneUnenroll]; }],
]];
}

- (void)phoneEnroll {
FIRUser *user = FIRAuth.auth.currentUser;
if (!user) {
[self logFailure:@"Please sign in first." error:nil];
} else {
[self showTextInputPromptWithMessage:@"Phone Number"
completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
return;
}
[self showTextInputPromptWithMessage:@"Phone Number"
completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
[user.multiFactor
getSessionWithCompletion:^(FIRMultiFactorSession *_Nullable session, NSError *_Nullable error) {
getSessionWithCompletion:^(FIRMultiFactorSession *_Nullable session, NSError *_Nullable error) {
[FIRPhoneAuthProvider.provider verifyPhoneNumber:phoneNumber
UIDelegate:nil
multiFactorSession:session
completion:^(NSString * _Nullable verificationID,
NSError * _Nullable error) {
if (error) {
[self logFailure:@"Multi factor start enroll failed." error:error];
} else {
[self showTextInputPromptWithMessage:@"Verification code"
if (error) {
[self logFailure:@"Multi factor start enroll failed." error:error];
} else {
[self showTextInputPromptWithMessage:@"Verification code"
completionBlock:^(BOOL userPressedOK,
NSString *_Nullable verificationCode) {
FIRPhoneAuthCredential *credential =
[[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID
verificationCode:verificationCode];
FIRMultiFactorAssertion *assertion =
[FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
[self showTextInputPromptWithMessage:@"Display name"
completionBlock:^(BOOL userPressedOK,
NSString *_Nullable verificationCode) {
FIRPhoneAuthCredential *credential =
[[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID
verificationCode:verificationCode];
FIRMultiFactorAssertion *assertion =
[FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
[self showTextInputPromptWithMessage:@"Display name"
completionBlock:^(BOOL userPressedOK,
NSString *_Nullable displayName) {
NSString *_Nullable displayName) {
[user.multiFactor enrollWithAssertion:assertion
displayName:displayName
completion:^(NSError *_Nullable error) {
if (error) {
[self logFailure:@"Multi factor finanlize enroll failed." error:error];
} else {
[self logSuccess:@"Multi factor finanlize enroll succeeded."];
}
}];
if (error) {
[self logFailure:@"Multi factor finalize enroll failed." error:error];
} else {
[self logSuccess:@"Multi factor finalize enroll succeeded."];
}
}];
}];
}
}];
}];
}
}];
}];
}
}];
}

- (void)phoneUnenroll {
Expand All @@ -97,7 +97,7 @@ - (void)phoneUnenroll {
}
}
[FIRAuth.auth.currentUser.multiFactor unenrollWithInfo:factorInfo
completion:^(NSError * _Nullable error) {
completion:^(NSError * _Nullable error) {
if (error) {
[self logFailure:@"Multi factor unenroll failed." error:error];
} else {
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+OAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+OOB.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "FirebaseAuth.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+Phone.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "MainViewController+Internal.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+User.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "MainViewController+Internal.h"
Expand Down
3 changes: 1 addition & 2 deletions Example/Auth/Sample/MainViewController+User.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ - (void)setDisplayName {
} else {
[FIRAuth.auth.currentUser getIDTokenResultWithCompletion:^(FIRAuthTokenResult *_Nullable tokenResult,
NSError *_Nullable error) {
;
[self logSuccess:@"set display name succeeded."];
}];
[self logSuccess:@"set display name succeeded."];
}
[self showTypicalUIForUserUpdateResultsWithTitle:@"Set Display Name" error:error];
}];
Expand Down
Loading