session-ios/Signal/src/ViewControllers/OWS2FARegistrationViewController.m

172 lines
5.8 KiB
Mathematica
Raw Normal View History

2018-03-01 20:42:54 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "OWS2FARegistrationViewController.h"
#import "PinEntryView.h"
2018-03-01 20:42:54 +01:00
#import "ProfileViewController.h"
#import "Signal-Swift.h"
#import <PromiseKit/AnyPromise.h>
#import <SignalMessaging/SignalMessaging-Swift.h>
2018-03-01 20:42:54 +01:00
#import <SignalMessaging/UIViewController+OWS.h>
#import <SignalServiceKit/OWS2FAManager.h>
NS_ASSUME_NONNULL_BEGIN
@interface OWS2FARegistrationViewController () <PinEntryViewDelegate>
2018-03-01 20:42:54 +01:00
@property (nonatomic, readonly) AccountManager *accountManager;
@property (nonatomic) PinEntryView *entryView;
2018-03-01 20:42:54 +01:00
@end
#pragma mark -
@implementation OWS2FARegistrationViewController
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (!self) {
return self;
}
_accountManager = SignalApp.sharedApp.accountManager;
return self;
}
- (instancetype)init
{
self = [super init];
if (!self) {
return self;
}
_accountManager = SignalApp.sharedApp.accountManager;
return self;
}
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// The navigation bar is hidden in the registration workflow.
if (self.navigationController.navigationBarHidden) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
self.navigationItem.hidesBackButton = YES;
self.title = NSLocalizedString(@"REGISTRATION_ENTER_LOCK_PIN_NAV_TITLE",
@"Navigation title shown when user is re-registering after having enabled registration lock");
2018-03-01 20:42:54 +01:00
self.view.backgroundColor = UIColor.whiteColor;
PinEntryView *entryView = [PinEntryView new];
self.entryView = entryView;
entryView.delegate = self;
[self.view addSubview:entryView];
entryView.instructionsText = NSLocalizedString(
@"REGISTER_2FA_INSTRUCTIONS", @"Instructions to enter the 'two-factor auth pin' in the 2FA registration view.");
// Layout
[entryView autoPinToTopLayoutGuideOfViewController:self withInset:0];
[entryView autoPinEdgeToSuperviewMargin:ALEdgeLeft];
[entryView autoPinEdgeToSuperviewMargin:ALEdgeRight];
[entryView autoPinToBottomLayoutGuideOfViewController:self withInset:0];
2018-03-01 20:42:54 +01:00
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.entryView makePinTextFieldFirstResponder];
2018-03-01 20:42:54 +01:00
}
#pragma mark - PinEntryViewDelegate
2018-03-01 20:42:54 +01:00
- (void)pinEntryView:(PinEntryView *)entryView submittedPinCode:(NSString *)pinCode
2018-03-01 20:42:54 +01:00
{
OWSAssert(self.entryView.hasValidPin);
2018-03-01 20:42:54 +01:00
[self tryToRegisterWithPinCode:pinCode];
2018-03-01 20:42:54 +01:00
}
- (void)pinEntryViewForgotPinLinkTapped:(PinEntryView *)entryView
2018-03-01 20:42:54 +01:00
{
NSString *alertBody = NSLocalizedString(@"REGISTER_2FA_FORGOT_PIN_ALERT_MESSAGE",
@"Alert message explaining what happens if you forget your 'two-factor auth pin'.");
[OWSAlerts showAlertWithTitle:nil message:alertBody];
2018-03-01 20:42:54 +01:00
}
#pragma mark - Registration
2018-03-01 20:42:54 +01:00
- (void)tryToRegisterWithPinCode:(NSString *)pinCode
2018-03-01 20:42:54 +01:00
{
OWSAssert(self.entryView.hasValidPin);
2018-03-01 20:42:54 +01:00
OWSAssert(self.verificationCode.length > 0);
OWSAssert(pinCode.length > 0);
2018-03-01 20:42:54 +01:00
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
__weak OWS2FARegistrationViewController *weakSelf = self;
[ModalActivityIndicatorViewController
presentFromViewController:self
canCancel:NO
backgroundBlock:^(ModalActivityIndicatorViewController *modalActivityIndicator) {
OWSProdInfo([OWSAnalyticsEvents registrationRegisteringCode]);
[self.accountManager registerWithVerificationCode:self.verificationCode pin:pinCode]
2018-03-01 20:42:54 +01:00
.then(^{
OWSAssertIsOnMainThread();
OWSProdInfo([OWSAnalyticsEvents registrationRegisteringSubmittedCode]);
[[OWS2FAManager sharedManager] mark2FAAsEnabledWithPin:pinCode];
2018-03-01 20:42:54 +01:00
DDLogInfo(@"%@ Successfully registered Signal account.", weakSelf.logTag);
dispatch_async(dispatch_get_main_queue(), ^{
[modalActivityIndicator dismissWithCompletion:^{
OWSAssertIsOnMainThread();
2018-03-02 17:10:09 +01:00
[weakSelf verificationWasCompleted];
2018-03-01 20:42:54 +01:00
}];
});
})
.catch(^(NSError *error) {
OWSAssertIsOnMainThread();
OWSProdInfo([OWSAnalyticsEvents registrationRegistrationFailed]);
DDLogError(@"%@ error verifying challenge: %@", weakSelf.logTag, error);
dispatch_async(dispatch_get_main_queue(), ^{
[modalActivityIndicator dismissWithCompletion:^{
OWSAssertIsOnMainThread();
2018-03-02 17:10:09 +01:00
[OWSAlerts showAlertWithTitle:NSLocalizedString(
@"REGISTER_2FA_REGISTRATION_FAILED_ALERT_TITLE",
@"Title for alert indicating that attempt to "
@"register with 'two-factor auth' failed.")
message:error.localizedDescription];
2018-03-01 20:42:54 +01:00
[weakSelf.entryView makePinTextFieldFirstResponder];
2018-03-01 20:42:54 +01:00
}];
});
});
}];
}
2018-03-02 17:10:09 +01:00
- (void)verificationWasCompleted
2018-03-01 20:42:54 +01:00
{
[ProfileViewController presentForRegistration:self.navigationController];
}
@end
NS_ASSUME_NONNULL_END