Skip to content

Commit 658992a

Browse files
renkelvinryanwilson
authored andcommitted
MFA beta (#4823)
1 parent 54a1db1 commit 658992a

File tree

127 files changed

+4365
-249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+4365
-249
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import XCTest
18+
19+
let kNoSecondFactorUserEmail = "iosapitests+no_second_factor@gmail.com"
20+
let kNoSecondFactorUserPassword = "aaaaaa"
21+
22+
let kPhoneSecondFactorPhoneNumber = "+16509999999"
23+
let kPhoneSecondFactorVerificationCode = "123456"
24+
let kPhoneSecondFactorDisplayName = "phone1"
25+
26+
let kOneSecondFactorUserEmail = "iosapitests+one_phone_second_factor@gmail.com"
27+
let kOneSecondFactorUserPassword = "aaaaaa"
28+
29+
class PhoneMultiFactorTests: FIRAuthApiTestsBase {
30+
31+
func testEnrollUnenroll() {
32+
let enrollExpectation = self.expectation(description: "Enroll phone multi factor finished.")
33+
let unenrollExpectation = self.expectation(description: "Unenroll phone multi factor finished.")
34+
Auth.auth().signIn(withEmail: kNoSecondFactorUserEmail, password: kNoSecondFactorUserPassword) { (result, error) in
35+
XCTAssertNil(error, "User normal sign in failed. Error: \(error!.localizedDescription)")
36+
37+
// Enroll
38+
guard let user = result?.user else {
39+
XCTFail("No valid user after attempted sign-in.")
40+
}
41+
user.multiFactor.getSessionWithCompletion({ (session, error) in
42+
XCTAssertNil(error, "Get multi factor session failed. Error: \(error!.localizedDescription)")
43+
PhoneAuthProvider.provider().verifyPhoneNumber(
44+
kPhoneSecondFactorPhoneNumber,
45+
uiDelegate: nil,
46+
multiFactorSession: session) { (verificationId, error) in
47+
XCTAssertNil(error, "Verify phone number failed. Error: \(error!.localizedDescription)")
48+
let credential = PhoneAuthProvider.provider().credential(
49+
withVerificationID: verificationId!,
50+
verificationCode: kPhoneSecondFactorVerificationCode)
51+
let assertion = PhoneMultiFactorGenerator.assertion(with: credential);
52+
user?.multiFactor.enroll(with: assertion, displayName: kPhoneSecondFactorDisplayName) { (error) in
53+
XCTAssertNil(error, "Phone multi factor enroll failed. Error: \(error!.localizedDescription)")
54+
XCTAssertEqual(Auth.auth().currentUser?.multiFactor.enrolledFactors.first?.displayName, kPhoneSecondFactorDisplayName)
55+
enrollExpectation.fulfill()
56+
57+
// Unenroll
58+
user = Auth.auth().currentUser
59+
user?.multiFactor.unenroll(with: (user?.multiFactor.enrolledFactors.first)!, completion: { (error) in
60+
XCTAssertNil(error, "Phone multi factor unenroll failed. Error: \(error!.localizedDescription)")
61+
XCTAssertEqual(Auth.auth().currentUser?.multiFactor.enrolledFactors.count, 0)
62+
unenrollExpectation.fulfill()
63+
})
64+
}
65+
}
66+
})
67+
}
68+
69+
self.waitForExpectations(timeout: 30) { error in
70+
XCTAssertNil(error, "Failed to wait for enroll and unenroll phone multi factor finished. Error: \(error!.localizedDescription)")
71+
}
72+
}
73+
74+
func testSignInWithSecondFactor() {
75+
let signInExpectation = self.expectation(description: "Sign in with phone multi factor finished.")
76+
Auth.auth().signIn(withEmail: kOneSecondFactorUserEmail, password: kOneSecondFactorUserPassword) { (result, error) in
77+
// SignIn
78+
guard let error = error, error.code == AuthErrorCode.secondFactorRequired.rawValue else {
79+
XCTFail("User sign in returns wrong error. Error: \(error!.localizedDescription)")
80+
}
81+
let resolver = error.userInfo["FIRAuthErrorUserInfoMultiFactorResolverKey"] as! MultiFactorResolver
82+
let hint = resolver.hints.first as! PhoneMultiFactorInfo
83+
PhoneAuthProvider.provider().verifyPhoneNumber(
84+
with: hint,
85+
uiDelegate: nil,
86+
multiFactorSession: resolver.session) { (verificationId, error) in
87+
XCTAssertNil(error, "Failed to verify phone number. Error: \(error!.localizedDescription)")
88+
let credential = PhoneAuthProvider.provider().credential(
89+
withVerificationID: verificationId!,
90+
verificationCode: kPhoneSecondFactorVerificationCode)
91+
let assertion = PhoneMultiFactorGenerator.assertion(with: credential);
92+
resolver.resolveSignIn(with: assertion) { (authResult, error) in
93+
XCTAssertNil(error, "Failed to sign in with phone multi factor. Error: \(error!.localizedDescription)")
94+
signInExpectation.fulfill()
95+
}
96+
}
97+
}
98+
99+
self.waitForExpectations(timeout: 300) { error in
100+
XCTAssertNil(error, "Failed to wait for enroll and unenroll phone multi factor finished. Error: \(error!.localizedDescription)")
101+
}
102+
}
103+
}

Example/Auth/AuthSample/AuthSample.xcodeproj/project.pbxproj

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
400283EA23EA254B0006A298 /* MainViewController+MultiFactor.m in Sources */ = {isa = PBXBuildFile; fileRef = 400283E923EA254A0006A298 /* MainViewController+MultiFactor.m */; };
1011
DE800B4722A2F8AF00AC9A23 /* MainViewController+Custom.m in Sources */ = {isa = PBXBuildFile; fileRef = DE800B1122A2F8AF00AC9A23 /* MainViewController+Custom.m */; };
1112
DE800B4822A2F8AF00AC9A23 /* SettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DE800B1222A2F8AF00AC9A23 /* SettingsViewController.m */; };
1213
DE800B4922A2F8AF00AC9A23 /* MainViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = DE800B1322A2F8AF00AC9A23 /* MainViewController.xib */; };
@@ -69,6 +70,8 @@
6970
/* End PBXContainerItemProxy section */
7071

7172
/* Begin PBXFileReference section */
73+
400283E823EA254A0006A298 /* MainViewController+MultiFactor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MainViewController+MultiFactor.h"; sourceTree = "<group>"; };
74+
400283E923EA254A0006A298 /* MainViewController+MultiFactor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "MainViewController+MultiFactor.m"; sourceTree = "<group>"; };
7275
DE800AF422A2F87E00AC9A23 /* AuthSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AuthSample.app; sourceTree = BUILT_PRODUCTS_DIR; };
7376
DE800B0E22A2F8AF00AC9A23 /* AppManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppManager.h; sourceTree = "<group>"; };
7477
DE800B0F22A2F8AF00AC9A23 /* MainViewController+AutoTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MainViewController+AutoTests.h"; sourceTree = "<group>"; };
@@ -210,68 +213,70 @@
210213
DE800B0D22A2F8AF00AC9A23 /* Sample */ = {
211214
isa = PBXGroup;
212215
children = (
213-
DE800B6C22A2FFFF00AC9A23 /* AuthCredentials.h */,
214216
DE800B6922A2FF8700AC9A23 /* Application.plist */,
215-
DE800B6A22A2FF8700AC9A23 /* Sample.entitlements */,
217+
DE800B1F22A2F8AF00AC9A23 /* ApplicationDelegate.h */,
218+
DE800B3C22A2F8AF00AC9A23 /* ApplicationDelegate.m */,
219+
DE800B2722A2F8AF00AC9A23 /* ApplicationTemplate.plist */,
220+
DE800B0E22A2F8AF00AC9A23 /* AppManager.h */,
221+
DE800B2D22A2F8AF00AC9A23 /* AppManager.m */,
222+
DE800B6C22A2FFFF00AC9A23 /* AuthCredentials.h */,
223+
DE800B4622A2F8AF00AC9A23 /* AuthCredentialsTemplate.h */,
224+
DE800B2622A2F8AF00AC9A23 /* AuthProviders.h */,
225+
DE800B4522A2F8AF00AC9A23 /* AuthProviders.m */,
226+
DE800B1D22A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.h */,
227+
DE800B3D22A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.m */,
228+
DE800B1E22A2F8AF00AC9A23 /* FacebookAuthProvider.h */,
229+
DE800B3B22A2F8AF00AC9A23 /* FacebookAuthProvider.m */,
230+
DE800B3322A2F8AF00AC9A23 /* GoogleAuthProvider.h */,
231+
DE800B1422A2F8AF00AC9A23 /* GoogleAuthProvider.m */,
216232
DE800B6522A2FF7300AC9A23 /* GoogleService-Info_multi.plist */,
217233
DE800B6622A2FF7300AC9A23 /* GoogleService-Info.plist */,
218-
DE800B0E22A2F8AF00AC9A23 /* AppManager.h */,
219-
DE800B0F22A2F8AF00AC9A23 /* MainViewController+AutoTests.h */,
220-
DE800B1022A2F8AF00AC9A23 /* MainViewController+OOB.h */,
221-
DE800B1122A2F8AF00AC9A23 /* MainViewController+Custom.m */,
222-
DE800B1222A2F8AF00AC9A23 /* SettingsViewController.m */,
234+
DE800B3222A2F8AF00AC9A23 /* Images.xcassets */,
235+
DE800B2E22A2F8AF00AC9A23 /* main.m */,
236+
DE800B4422A2F8AF00AC9A23 /* MainViewController.h */,
237+
DE800B2922A2F8AF00AC9A23 /* MainViewController.m */,
223238
DE800B1322A2F8AF00AC9A23 /* MainViewController.xib */,
224-
DE800B1422A2F8AF00AC9A23 /* GoogleAuthProvider.m */,
225-
DE800B1522A2F8AF00AC9A23 /* UserInfoViewController.h */,
226-
DE800B1622A2F8AF00AC9A23 /* MainViewController+OAuth.h */,
227239
DE800B1722A2F8AF00AC9A23 /* MainViewController+App.h */,
228-
DE800B1822A2F8AF00AC9A23 /* MainViewController+User.m */,
229-
DE800B1922A2F8AF00AC9A23 /* MainViewController+GameCenter.m */,
230-
DE800B1A22A2F8AF00AC9A23 /* MainViewController+Phone.m */,
231-
DE800B1B22A2F8AF00AC9A23 /* UserTableViewCell.m */,
232-
DE800B1C22A2F8AF00AC9A23 /* MainViewController+Facebook.m */,
233-
DE800B1D22A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.h */,
234-
DE800B1E22A2F8AF00AC9A23 /* FacebookAuthProvider.h */,
235-
DE800B1F22A2F8AF00AC9A23 /* ApplicationDelegate.h */,
236-
DE800B2022A2F8AF00AC9A23 /* UIViewController+Alerts.m */,
237-
DE800B2122A2F8AF00AC9A23 /* MainViewController+Google.h */,
238-
DE800B2222A2F8AF00AC9A23 /* MainViewController+Email.m */,
240+
DE800B2F22A2F8AF00AC9A23 /* MainViewController+App.m */,
241+
DE800B3822A2F8AF00AC9A23 /* MainViewController+Auth.h */,
239242
DE800B2322A2F8AF00AC9A23 /* MainViewController+Auth.m */,
240-
DE800B2422A2F8AF00AC9A23 /* StaticContentTableViewManager.h */,
241-
DE800B2522A2F8AF00AC9A23 /* SampleTemplate.entitlements */,
242-
DE800B2622A2F8AF00AC9A23 /* AuthProviders.h */,
243-
DE800B2722A2F8AF00AC9A23 /* ApplicationTemplate.plist */,
244-
DE800B2822A2F8AF00AC9A23 /* UserInfoViewController.xib */,
245-
DE800B2922A2F8AF00AC9A23 /* MainViewController.m */,
246-
DE800B2A22A2F8AF00AC9A23 /* MainViewController+Custom.h */,
243+
DE800B0F22A2F8AF00AC9A23 /* MainViewController+AutoTests.h */,
247244
DE800B2B22A2F8AF00AC9A23 /* MainViewController+AutoTests.m */,
248-
DE800B2C22A2F8AF00AC9A23 /* MainViewController+OOB.m */,
249-
DE800B2D22A2F8AF00AC9A23 /* AppManager.m */,
250-
DE800B2E22A2F8AF00AC9A23 /* main.m */,
251-
DE800B2F22A2F8AF00AC9A23 /* MainViewController+App.m */,
245+
DE800B2A22A2F8AF00AC9A23 /* MainViewController+Custom.h */,
246+
DE800B1122A2F8AF00AC9A23 /* MainViewController+Custom.m */,
247+
DE800B3722A2F8AF00AC9A23 /* MainViewController+Email.h */,
248+
DE800B2222A2F8AF00AC9A23 /* MainViewController+Email.m */,
249+
DE800B3E22A2F8AF00AC9A23 /* MainViewController+Facebook.h */,
250+
DE800B1C22A2F8AF00AC9A23 /* MainViewController+Facebook.m */,
251+
DE800B4222A2F8AF00AC9A23 /* MainViewController+GameCenter.h */,
252+
DE800B1922A2F8AF00AC9A23 /* MainViewController+GameCenter.m */,
253+
DE800B2122A2F8AF00AC9A23 /* MainViewController+Google.h */,
254+
DE800B3922A2F8AF00AC9A23 /* MainViewController+Google.m */,
252255
DE800B3022A2F8AF00AC9A23 /* MainViewController+Internal.h */,
256+
400283E823EA254A0006A298 /* MainViewController+MultiFactor.h */,
257+
400283E923EA254A0006A298 /* MainViewController+MultiFactor.m */,
258+
DE800B1622A2F8AF00AC9A23 /* MainViewController+OAuth.h */,
253259
DE800B3122A2F8AF00AC9A23 /* MainViewController+OAuth.m */,
254-
DE800B3222A2F8AF00AC9A23 /* Images.xcassets */,
255-
DE800B3322A2F8AF00AC9A23 /* GoogleAuthProvider.h */,
256-
DE800B3422A2F8AF00AC9A23 /* UserInfoViewController.m */,
260+
DE800B1022A2F8AF00AC9A23 /* MainViewController+OOB.h */,
261+
DE800B2C22A2F8AF00AC9A23 /* MainViewController+OOB.m */,
262+
DE800B3F22A2F8AF00AC9A23 /* MainViewController+Phone.h */,
263+
DE800B1A22A2F8AF00AC9A23 /* MainViewController+Phone.m */,
264+
DE800B4322A2F8AF00AC9A23 /* MainViewController+User.h */,
265+
DE800B1822A2F8AF00AC9A23 /* MainViewController+User.m */,
266+
DE800B6A22A2FF8700AC9A23 /* Sample.entitlements */,
267+
DE800B2522A2F8AF00AC9A23 /* SampleTemplate.entitlements */,
257268
DE800B3522A2F8AF00AC9A23 /* SettingsViewController.h */,
269+
DE800B1222A2F8AF00AC9A23 /* SettingsViewController.m */,
270+
DE800B4122A2F8AF00AC9A23 /* SettingsViewController.xib */,
271+
DE800B2422A2F8AF00AC9A23 /* StaticContentTableViewManager.h */,
258272
DE800B3622A2F8AF00AC9A23 /* StaticContentTableViewManager.m */,
259-
DE800B3722A2F8AF00AC9A23 /* MainViewController+Email.h */,
260-
DE800B3822A2F8AF00AC9A23 /* MainViewController+Auth.h */,
261-
DE800B3922A2F8AF00AC9A23 /* MainViewController+Google.m */,
262273
DE800B3A22A2F8AF00AC9A23 /* UIViewController+Alerts.h */,
263-
DE800B3B22A2F8AF00AC9A23 /* FacebookAuthProvider.m */,
264-
DE800B3C22A2F8AF00AC9A23 /* ApplicationDelegate.m */,
265-
DE800B3D22A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.m */,
266-
DE800B3E22A2F8AF00AC9A23 /* MainViewController+Facebook.h */,
267-
DE800B3F22A2F8AF00AC9A23 /* MainViewController+Phone.h */,
274+
DE800B2022A2F8AF00AC9A23 /* UIViewController+Alerts.m */,
275+
DE800B1522A2F8AF00AC9A23 /* UserInfoViewController.h */,
276+
DE800B3422A2F8AF00AC9A23 /* UserInfoViewController.m */,
277+
DE800B2822A2F8AF00AC9A23 /* UserInfoViewController.xib */,
268278
DE800B4022A2F8AF00AC9A23 /* UserTableViewCell.h */,
269-
DE800B4122A2F8AF00AC9A23 /* SettingsViewController.xib */,
270-
DE800B4222A2F8AF00AC9A23 /* MainViewController+GameCenter.h */,
271-
DE800B4322A2F8AF00AC9A23 /* MainViewController+User.h */,
272-
DE800B4422A2F8AF00AC9A23 /* MainViewController.h */,
273-
DE800B4522A2F8AF00AC9A23 /* AuthProviders.m */,
274-
DE800B4622A2F8AF00AC9A23 /* AuthCredentialsTemplate.h */,
279+
DE800B1B22A2F8AF00AC9A23 /* UserTableViewCell.m */,
275280
);
276281
name = Sample;
277282
path = ../Sample;
@@ -449,6 +454,7 @@
449454
DE800B4F22A2F8AF00AC9A23 /* MainViewController+Facebook.m in Sources */,
450455
DE800B4D22A2F8AF00AC9A23 /* MainViewController+Phone.m in Sources */,
451456
DE800B4722A2F8AF00AC9A23 /* MainViewController+Custom.m in Sources */,
457+
400283EA23EA254B0006A298 /* MainViewController+MultiFactor.m in Sources */,
452458
DE800B5A22A2F8AF00AC9A23 /* MainViewController+App.m in Sources */,
453459
DE800B6222A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.m in Sources */,
454460
DE800B6122A2F8AF00AC9A23 /* ApplicationDelegate.m in Sources */,

Example/Auth/E2eTests/FIRAuthE2eTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ - (void)testSignInExistingUser {
3434
grey_allOf(grey_kindOfClass([UILabel class]), grey_accessibilityLabel(@"OK"), nil);
3535

3636
[[EarlGrey selectElementWithMatcher:
37-
#warning TODO Add accessibilityIdentifiers for the elements.
37+
// TODO: Add accessibilityIdentifiers for the elements.
3838
grey_kindOfClass(NSClassFromString(@"_UIAlertControllerView"))]
3939
performAction:grey_typeText(email)];
4040

Example/Auth/Sample/MainViewController+App.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import <Foundation/Foundation.h>
1818

1919
#import "MainViewController.h"
20+
2021
#import "StaticContentTableViewManager.h"
2122

2223
NS_ASSUME_NONNULL_BEGIN

Example/Auth/Sample/MainViewController+Auth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
#import <Foundation/Foundation.h>
18+
1719
#import "MainViewController.h"
1820

1921
#import "FIRAuth.h"

Example/Auth/Sample/MainViewController+AutoTests.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
#import <Foundation/Foundation.h>
18+
1719
#import "MainViewController.h"
1820

1921
#import "StaticContentTableViewManager.h"

Example/Auth/Sample/MainViewController+Custom.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
#import <Foundation/Foundation.h>
18+
1719
#import "MainViewController.h"
1820

1921
#import "StaticContentTableViewManager.h"

Example/Auth/Sample/MainViewController+Email.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19-
#import "FIRAuthCredential.h"
2019
#import "MainViewController.h"
20+
2121
#import "FIRAuth.h"
22+
#import "FIRAuthCredential.h"
2223
#import "StaticContentTableViewManager.h"
2324

2425
NS_ASSUME_NONNULL_BEGIN

0 commit comments

Comments
 (0)