2017-06-09 00:10:02 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "FingerprintViewScanController.h"
|
|
|
|
#import "Environment.h"
|
|
|
|
#import "OWSContactsManager.h"
|
|
|
|
#import "OWSQRCodeScanningViewController.h"
|
|
|
|
#import "Signal-Swift.h"
|
|
|
|
#import "UIColor+OWS.h"
|
|
|
|
#import "UIFont+OWS.h"
|
|
|
|
#import "UIUtil.h"
|
|
|
|
#import "UIView+OWS.h"
|
|
|
|
#import "UIViewController+CameraPermissions.h"
|
|
|
|
#import <SignalServiceKit/OWSError.h>
|
|
|
|
#import <SignalServiceKit/OWSFingerprint.h>
|
|
|
|
#import <SignalServiceKit/OWSFingerprintBuilder.h>
|
2017-06-09 15:33:38 +02:00
|
|
|
#import <SignalServiceKit/OWSIdentityManager.h>
|
2017-06-09 00:10:02 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
@interface FingerprintViewScanController () <OWSQRScannerDelegate>
|
|
|
|
|
2017-08-02 21:15:31 +02:00
|
|
|
@property (nonatomic) TSAccountManager *accountManager;
|
2017-06-09 15:33:38 +02:00
|
|
|
@property (nonatomic) NSString *recipientId;
|
|
|
|
@property (nonatomic) NSData *identityKey;
|
2017-06-09 00:10:02 +02:00
|
|
|
@property (nonatomic) OWSFingerprint *fingerprint;
|
|
|
|
@property (nonatomic) NSString *contactName;
|
|
|
|
@property (nonatomic) OWSQRCodeScanningViewController *qrScanningController;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-06-09 17:47:46 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-06-09 00:10:02 +02:00
|
|
|
@implementation FingerprintViewScanController
|
|
|
|
|
|
|
|
- (void)configureWithRecipientId:(NSString *)recipientId
|
|
|
|
{
|
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
|
2017-06-09 15:33:38 +02:00
|
|
|
self.recipientId = recipientId;
|
2017-08-02 21:15:31 +02:00
|
|
|
self.accountManager = [TSAccountManager sharedInstance];
|
2017-06-09 00:10:02 +02:00
|
|
|
|
|
|
|
OWSContactsManager *contactsManager = [Environment getCurrent].contactsManager;
|
|
|
|
self.contactName = [contactsManager displayNameForPhoneIdentifier:recipientId];
|
|
|
|
|
|
|
|
OWSRecipientIdentity *_Nullable recipientIdentity =
|
|
|
|
[[OWSIdentityManager sharedManager] recipientIdentityForRecipientId:recipientId];
|
|
|
|
OWSAssert(recipientIdentity);
|
2017-06-09 17:56:37 +02:00
|
|
|
// By capturing the identity key when we enter these views, we prevent the edge case
|
|
|
|
// where the user verifies a key that we learned about while this view was open.
|
2017-06-09 15:33:38 +02:00
|
|
|
self.identityKey = recipientIdentity.identityKey;
|
2017-06-09 00:10:02 +02:00
|
|
|
|
|
|
|
OWSFingerprintBuilder *builder =
|
2017-08-02 21:15:31 +02:00
|
|
|
[[OWSFingerprintBuilder alloc] initWithAccountManager:self.accountManager contactsManager:contactsManager];
|
2017-06-09 00:10:02 +02:00
|
|
|
self.fingerprint =
|
|
|
|
[builder fingerprintWithTheirSignalId:recipientId theirIdentityKey:recipientIdentity.identityKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadView
|
|
|
|
{
|
|
|
|
[super loadView];
|
|
|
|
|
|
|
|
self.title = NSLocalizedString(@"SCAN_QR_CODE_VIEW_TITLE", @"Title for the 'scan QR code' view.");
|
|
|
|
|
|
|
|
[self createViews];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)createViews
|
|
|
|
{
|
|
|
|
UIColor *darkGrey = [UIColor colorWithRGBHex:0x404040];
|
|
|
|
|
|
|
|
self.view.backgroundColor = [UIColor blackColor];
|
|
|
|
|
|
|
|
self.qrScanningController = [OWSQRCodeScanningViewController new];
|
|
|
|
self.qrScanningController.scanDelegate = self;
|
|
|
|
[self.view addSubview:self.qrScanningController.view];
|
|
|
|
[self.qrScanningController.view autoPinWidthToSuperview];
|
|
|
|
[self.qrScanningController.view autoPinToTopLayoutGuideOfViewController:self withInset:0];
|
|
|
|
|
|
|
|
UIView *footer = [UIView new];
|
|
|
|
footer.backgroundColor = darkGrey;
|
|
|
|
[self.view addSubview:footer];
|
|
|
|
[footer autoPinWidthToSuperview];
|
|
|
|
[footer autoPinToBottomLayoutGuideOfViewController:self withInset:0];
|
|
|
|
[footer autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.qrScanningController.view];
|
|
|
|
|
|
|
|
UILabel *cameraInstructionLabel = [UILabel new];
|
|
|
|
cameraInstructionLabel.text
|
|
|
|
= NSLocalizedString(@"SCAN_CODE_INSTRUCTIONS", @"label presented once scanning (camera) view is visible.");
|
2017-06-09 13:48:12 +02:00
|
|
|
cameraInstructionLabel.font = [UIFont ows_regularFontWithSize:ScaleFromIPhone5To7Plus(14.f, 18.f)];
|
2017-06-09 00:10:02 +02:00
|
|
|
cameraInstructionLabel.textColor = [UIColor whiteColor];
|
|
|
|
cameraInstructionLabel.textAlignment = NSTextAlignmentCenter;
|
|
|
|
cameraInstructionLabel.numberOfLines = 0;
|
|
|
|
cameraInstructionLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
|
|
|
[footer addSubview:cameraInstructionLabel];
|
2017-06-09 13:48:12 +02:00
|
|
|
[cameraInstructionLabel autoPinWidthToSuperviewWithMargin:ScaleFromIPhone5To7Plus(16.f, 30.f)];
|
|
|
|
CGFloat instructionsVMargin = ScaleFromIPhone5To7Plus(10.f, 20.f);
|
|
|
|
[cameraInstructionLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:instructionsVMargin];
|
|
|
|
[cameraInstructionLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:instructionsVMargin];
|
2017-06-09 00:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
// In case we're returning from activity view that needed default system styles.
|
|
|
|
[UIUtil applySignalAppearence];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillDisappear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillDisappear:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Action
|
|
|
|
|
|
|
|
|
|
|
|
- (void)viewDidAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewDidAppear:animated];
|
|
|
|
|
|
|
|
[self ows_askForCameraPermissions:^{
|
|
|
|
|
|
|
|
// Camera stops capturing when "sharing" while in capture mode.
|
|
|
|
// Also, it's less obvious whats being "shared" at this point,
|
|
|
|
// so just disable sharing when in capture mode.
|
|
|
|
|
2017-11-08 20:04:51 +01:00
|
|
|
DDLogInfo(@"%@ Showing Scanner", self.logTag);
|
2017-06-09 00:10:02 +02:00
|
|
|
|
|
|
|
[self.qrScanningController startCapture];
|
|
|
|
}
|
|
|
|
failureCallback:^{
|
|
|
|
[self.navigationController popViewControllerAnimated:YES];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - OWSQRScannerDelegate
|
|
|
|
|
|
|
|
- (void)controller:(OWSQRCodeScanningViewController *)controller didDetectQRCodeWithData:(NSData *)data
|
|
|
|
{
|
|
|
|
[self verifyCombinedFingerprintData:data];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)verifyCombinedFingerprintData:(NSData *)combinedFingerprintData
|
|
|
|
{
|
|
|
|
NSError *error;
|
|
|
|
if ([self.fingerprint matchesLogicalFingerprintsData:combinedFingerprintData error:&error]) {
|
|
|
|
[self showVerificationSucceeded];
|
|
|
|
} else {
|
|
|
|
[self showVerificationFailedWithError:error];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)showVerificationSucceeded
|
|
|
|
{
|
2017-06-09 15:33:38 +02:00
|
|
|
[self.class showVerificationSucceeded:self
|
|
|
|
identityKey:self.identityKey
|
|
|
|
recipientId:self.recipientId
|
|
|
|
contactName:self.contactName
|
2017-11-08 20:04:51 +01:00
|
|
|
tag:self.logTag];
|
2017-06-09 15:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)showVerificationFailedWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
|
|
|
|
[self.class showVerificationFailedWithError:error
|
|
|
|
viewController:self
|
|
|
|
retryBlock:^{
|
|
|
|
[self.qrScanningController startCapture];
|
|
|
|
}
|
|
|
|
cancelBlock:^{
|
|
|
|
[self.navigationController popViewControllerAnimated:YES];
|
|
|
|
}
|
2017-11-08 20:04:51 +01:00
|
|
|
tag:self.logTag];
|
2017-06-09 15:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)showVerificationSucceeded:(UIViewController *)viewController
|
|
|
|
identityKey:(NSData *)identityKey
|
|
|
|
recipientId:(NSString *)recipientId
|
|
|
|
contactName:(NSString *)contactName
|
|
|
|
tag:(NSString *)tag
|
|
|
|
{
|
|
|
|
OWSAssert(viewController);
|
|
|
|
OWSAssert(identityKey.length > 0);
|
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
OWSAssert(contactName.length > 0);
|
|
|
|
OWSAssert(tag.length > 0);
|
|
|
|
|
|
|
|
DDLogInfo(@"%@ Successfully verified safety numbers.", tag);
|
|
|
|
|
2017-06-09 00:10:02 +02:00
|
|
|
NSString *successTitle = NSLocalizedString(@"SUCCESSFUL_VERIFICATION_TITLE", nil);
|
|
|
|
NSString *descriptionFormat = NSLocalizedString(
|
|
|
|
@"SUCCESSFUL_VERIFICATION_DESCRIPTION", @"Alert body after verifying privacy with {{other user's name}}");
|
2017-06-09 15:33:38 +02:00
|
|
|
NSString *successDescription = [NSString stringWithFormat:descriptionFormat, contactName];
|
|
|
|
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:successTitle
|
|
|
|
message:successDescription
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
[alertController
|
2017-06-16 16:52:19 +02:00
|
|
|
addAction:[UIAlertAction
|
|
|
|
actionWithTitle:NSLocalizedString(@"FINGERPRINT_SCAN_VERIFY_BUTTON",
|
|
|
|
@"Button that marks user as verified after a successful fingerprint scan.")
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
[OWSIdentityManager.sharedManager setVerificationState:OWSVerificationStateVerified
|
|
|
|
identityKey:identityKey
|
|
|
|
recipientId:recipientId
|
2017-06-15 21:19:33 +02:00
|
|
|
isUserInitiatedChange:YES];
|
2017-06-16 16:52:19 +02:00
|
|
|
[viewController dismissViewControllerAnimated:true completion:nil];
|
|
|
|
}]];
|
2017-06-09 15:33:38 +02:00
|
|
|
UIAlertAction *dismissAction =
|
2017-07-13 20:53:24 +02:00
|
|
|
[UIAlertAction actionWithTitle:CommonStrings.dismissButton
|
2017-06-09 15:33:38 +02:00
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
[viewController dismissViewControllerAnimated:true completion:nil];
|
|
|
|
}];
|
|
|
|
[alertController addAction:dismissAction];
|
2017-06-09 00:10:02 +02:00
|
|
|
|
2017-06-09 15:33:38 +02:00
|
|
|
[viewController presentViewController:alertController animated:YES completion:nil];
|
2017-06-09 00:10:02 +02:00
|
|
|
}
|
|
|
|
|
2017-06-09 15:33:38 +02:00
|
|
|
+ (void)showVerificationFailedWithError:(NSError *)error
|
|
|
|
viewController:(UIViewController *)viewController
|
2017-11-08 20:04:51 +01:00
|
|
|
retryBlock:(void (^_Nullable)(void))retryBlock
|
|
|
|
cancelBlock:(void (^_Nonnull)(void))cancelBlock
|
2017-06-09 15:33:38 +02:00
|
|
|
tag:(NSString *)tag
|
2017-06-09 00:10:02 +02:00
|
|
|
{
|
2017-06-09 15:33:38 +02:00
|
|
|
OWSAssert(viewController);
|
|
|
|
OWSAssert(cancelBlock);
|
|
|
|
OWSAssert(tag.length > 0);
|
|
|
|
|
|
|
|
DDLogInfo(@"%@ Failed to verify safety numbers.", tag);
|
|
|
|
|
2017-06-09 00:10:02 +02:00
|
|
|
NSString *_Nullable failureTitle;
|
|
|
|
if (error.code != OWSErrorCodeUserError) {
|
|
|
|
failureTitle = NSLocalizedString(@"FAILED_VERIFICATION_TITLE", @"alert title");
|
|
|
|
} // else no title. We don't want to show a big scary "VERIFICATION FAILED" when it's just user error.
|
|
|
|
|
|
|
|
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:failureTitle
|
|
|
|
message:error.localizedDescription
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
2017-06-09 15:33:38 +02:00
|
|
|
if (retryBlock) {
|
2017-10-19 23:46:50 +02:00
|
|
|
[alertController addAction:[UIAlertAction actionWithTitle:[CommonStrings retryButton]
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
retryBlock();
|
|
|
|
}]];
|
2017-06-09 15:33:38 +02:00
|
|
|
}
|
2017-06-09 00:10:02 +02:00
|
|
|
|
2017-08-21 22:58:26 +02:00
|
|
|
[alertController addAction:[OWSAlerts cancelAction]];
|
2017-06-09 00:10:02 +02:00
|
|
|
|
2017-06-09 15:33:38 +02:00
|
|
|
[viewController presentViewController:alertController animated:YES completion:nil];
|
2017-06-09 00:10:02 +02:00
|
|
|
|
2017-06-09 15:33:38 +02:00
|
|
|
DDLogWarn(@"%@ Identity verification failed with error: %@", tag, error);
|
2017-06-09 00:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dismissViewControllerAnimated:(BOOL)animated completion:(nullable void (^)(void))completion
|
|
|
|
{
|
|
|
|
self.qrScanningController.view.hidden = YES;
|
|
|
|
|
|
|
|
[super dismissViewControllerAnimated:animated completion:completion];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|