2014-10-29 21:58:58 +01:00
|
|
|
//
|
|
|
|
// FingerprintViewController.m
|
|
|
|
// Signal
|
|
|
|
//
|
|
|
|
// Created by Dylan Bourgeois on 02/11/14.
|
|
|
|
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "FingerprintViewController.h"
|
2015-01-14 22:30:01 +01:00
|
|
|
#import "DJWActionSheet+OWS.h"
|
2016-09-21 19:59:45 +02:00
|
|
|
#import <SignalServiceKit/NSDate+millisecondTimeStamp.h>
|
2016-09-11 22:53:12 +02:00
|
|
|
#import <SignalServiceKit/OWSFingerprint.h>
|
2016-09-21 19:59:45 +02:00
|
|
|
#import <SignalServiceKit/TSInfoMessage.h>
|
2016-09-11 22:53:12 +02:00
|
|
|
#import <SignalServiceKit/TSStorageManager+IdentityKeyStore.h>
|
|
|
|
#import <SignalServiceKit/TSStorageManager+SessionStore.h>
|
|
|
|
#import <SignalServiceKit/TSStorageManager+keyingMaterial.h>
|
2016-09-21 19:59:45 +02:00
|
|
|
#import <SignalServiceKit/TSThread.h>
|
2014-10-29 21:58:58 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
2014-12-11 00:05:41 +01:00
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
@interface FingerprintViewController ()
|
2016-09-11 22:53:12 +02:00
|
|
|
|
|
|
|
@property (strong, nonatomic) TSStorageManager *storageManager;
|
2015-01-27 21:17:49 +01:00
|
|
|
@property (nonatomic) BOOL isPresentingDialog;
|
2016-09-21 19:59:45 +02:00
|
|
|
@property (strong, nonatomic) TSThread *thread;
|
|
|
|
@property (strong, nonatomic) OWSFingerprint *fingerprint;
|
|
|
|
@property (strong, nonatomic) NSString *contactName;
|
2016-09-11 22:53:12 +02:00
|
|
|
@property (strong, nonatomic) OWSQRCodeScanningViewController *qrScanningController;
|
|
|
|
|
|
|
|
@property (strong, nonatomic) IBOutlet UIView *qrScanningView;
|
|
|
|
@property (strong, nonatomic) IBOutlet UIView *scanningContainer;
|
|
|
|
@property (strong, nonatomic) IBOutlet UIView *instructionsContainer;
|
|
|
|
@property (strong, nonatomic) IBOutlet UIView *qrContainer;
|
|
|
|
@property (strong, nonatomic) IBOutlet UIView *privacyVerificationQRCodeFrame;
|
|
|
|
@property (strong, nonatomic) IBOutlet UIImageView *privacyVerificationQRCode;
|
|
|
|
@property (strong, nonatomic) IBOutlet UILabel *privacyVerificationFingerprint;
|
|
|
|
@property (strong, nonatomic) IBOutlet UILabel *instructionsLabel;
|
|
|
|
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
|
|
|
|
@property (strong, nonatomic) IBOutlet UIButton *scanButton;
|
|
|
|
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *qrCodeCenterConstraint;
|
2014-10-29 21:58:58 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
@end
|
2015-01-27 21:17:49 +01:00
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
@implementation FingerprintViewController
|
|
|
|
|
2016-09-21 19:59:45 +02:00
|
|
|
- (void)configureWithThread:(TSThread *)thread
|
|
|
|
fingerprint:(OWSFingerprint *)fingerprint
|
|
|
|
contactName:(NSString *)contactName
|
2016-09-11 22:53:12 +02:00
|
|
|
{
|
2016-09-21 19:59:45 +02:00
|
|
|
self.thread = thread;
|
2016-09-11 22:53:12 +02:00
|
|
|
self.fingerprint = fingerprint;
|
|
|
|
self.contactName = contactName;
|
2014-12-04 00:23:36 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
2014-10-29 21:58:58 +01:00
|
|
|
[super viewDidLoad];
|
2016-09-11 22:53:12 +02:00
|
|
|
self.storageManager = [TSStorageManager sharedManager];
|
|
|
|
self.qrScanningView.hidden = YES;
|
|
|
|
|
|
|
|
// HACK to get full width preview layer
|
|
|
|
CGRect oldFrame = self.qrScanningView.frame;
|
|
|
|
CGRect newFrame = CGRectMake(oldFrame.origin.x,
|
|
|
|
oldFrame.origin.y,
|
|
|
|
self.view.frame.size.width,
|
|
|
|
self.view.frame.size.height / 2.0f - oldFrame.origin.y);
|
|
|
|
self.qrScanningView.frame = newFrame;
|
|
|
|
// END HACK to get full width preview layer
|
|
|
|
|
|
|
|
self.titleLabel.text = NSLocalizedString(@"PRIVACY_VERIFICATION_TITLE", @"Navbar title");
|
|
|
|
NSString *instructionsFormat = NSLocalizedString(@"PRIVACY_VERIFICATION_INSTRUCTIONS",
|
|
|
|
@"Paragraph(s) shown alongside keying material when verifying privacy with {{contact name}}");
|
|
|
|
self.instructionsLabel.text = [NSString stringWithFormat:instructionsFormat, self.contactName];
|
|
|
|
|
|
|
|
self.scanButton.titleLabel.text = NSLocalizedString(@"SCAN_CODE_ACTION",
|
|
|
|
@"Button label presented with camera icon while verifying privacy credentials. Shows the camera interface.");
|
|
|
|
|
|
|
|
// Safety numbers and QR Code
|
|
|
|
self.privacyVerificationFingerprint.text = self.fingerprint.displayableText;
|
|
|
|
self.privacyVerificationQRCode.image = self.fingerprint.image;
|
|
|
|
|
|
|
|
// Don't antialias QRCode
|
|
|
|
self.privacyVerificationQRCode.layer.magnificationFilter = kCAFilterNearest;
|
|
|
|
|
|
|
|
// Add session reset action.
|
2015-12-22 12:45:09 +01:00
|
|
|
UILongPressGestureRecognizer *longpressToResetSession =
|
2016-09-11 22:53:12 +02:00
|
|
|
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongpressToResetSession:)];
|
2015-01-27 21:17:49 +01:00
|
|
|
longpressToResetSession.minimumPressDuration = 1.0;
|
|
|
|
[self.view addGestureRecognizer:longpressToResetSession];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (void)viewDidLayoutSubviews
|
|
|
|
{
|
|
|
|
[super viewDidLayoutSubviews];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
self.privacyVerificationQRCodeFrame.layer.masksToBounds = YES;
|
|
|
|
self.privacyVerificationQRCodeFrame.layer.cornerRadius = self.privacyVerificationQRCodeFrame.frame.size.height / 2;
|
2014-11-21 14:38:37 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender
|
|
|
|
{
|
|
|
|
if ([segue.identifier isEqualToString:@"embedIdentityQRScanner"]) {
|
|
|
|
OWSQRCodeScanningViewController *qrScanningController
|
|
|
|
= (OWSQRCodeScanningViewController *)segue.destinationViewController;
|
|
|
|
self.qrScanningController = qrScanningController;
|
|
|
|
qrScanningController.scanDelegate = self;
|
2015-01-14 22:30:01 +01:00
|
|
|
}
|
2014-12-27 01:08:05 +01:00
|
|
|
}
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
#pragma mark - Action
|
2016-09-11 22:53:12 +02:00
|
|
|
- (IBAction)closeButtonAction:(id)sender
|
|
|
|
{
|
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (IBAction)didTouchUpInsideScanButton:(id)sender
|
|
|
|
{
|
|
|
|
[self showScanner];
|
|
|
|
}
|
2014-12-12 17:15:00 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (IBAction)didLongpressToResetSession:(id)sender
|
|
|
|
{
|
2015-12-22 12:45:09 +01:00
|
|
|
if (!_isPresentingDialog) {
|
2015-01-27 21:17:49 +01:00
|
|
|
_isPresentingDialog = YES;
|
2015-12-22 12:45:09 +01:00
|
|
|
[DJWActionSheet showInView:self.view
|
|
|
|
withTitle:NSLocalizedString(@"FINGERPRINT_SHRED_KEYMATERIAL_CONFIRMATION", @"")
|
|
|
|
cancelButtonTitle:NSLocalizedString(@"TXT_CANCEL_TITLE", @"")
|
|
|
|
destructiveButtonTitle:nil
|
|
|
|
otherButtonTitles:@[ NSLocalizedString(@"FINGERPRINT_SHRED_KEYMATERIAL_BUTTON", @"") ]
|
2015-01-27 21:17:49 +01:00
|
|
|
tapBlock:^(DJWActionSheet *actionSheet, NSInteger tappedButtonIndex) {
|
2015-12-22 12:45:09 +01:00
|
|
|
_isPresentingDialog = NO;
|
|
|
|
if (tappedButtonIndex == actionSheet.cancelButtonIndex) {
|
|
|
|
DDLogDebug(@"User Cancelled");
|
|
|
|
} else if (tappedButtonIndex == actionSheet.destructiveButtonIndex) {
|
|
|
|
DDLogDebug(@"Destructive button tapped");
|
|
|
|
} else {
|
|
|
|
switch (tappedButtonIndex) {
|
|
|
|
case 0:
|
2016-09-11 22:53:12 +02:00
|
|
|
[self resetSession];
|
2015-12-22 12:45:09 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 21:17:49 +01:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (void)showScanner
|
|
|
|
{
|
|
|
|
DDLogInfo(@"%@ Showing Scanner", self.tag);
|
|
|
|
self.qrScanningView.hidden = NO;
|
2015-01-27 21:17:49 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
// Recommended before animating a constraint.
|
|
|
|
[self.view layoutIfNeeded];
|
2015-01-27 21:17:49 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
// Shift QRCode up within it's own frame, while shifting it's whole
|
|
|
|
// frame down.
|
|
|
|
self.qrCodeCenterConstraint.constant = 0.0f;
|
|
|
|
[UIView animateWithDuration:0.4
|
|
|
|
delay:0.0
|
|
|
|
options:UIViewAnimationOptionCurveEaseInOut
|
|
|
|
animations:^{
|
2015-01-27 21:17:49 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
self.scanningContainer.frame = self.qrContainer.frame;
|
|
|
|
self.qrContainer.frame = self.instructionsContainer.frame;
|
|
|
|
self.instructionsContainer.alpha = 0.0f;
|
|
|
|
// animate constraint smoothly
|
|
|
|
[self.view layoutIfNeeded];
|
|
|
|
}
|
|
|
|
completion:nil];
|
2014-10-29 21:58:58 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
[self.qrScanningController startCapture];
|
2014-12-06 22:27:43 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (void)resetSession
|
|
|
|
{
|
2016-09-21 19:59:45 +02:00
|
|
|
DDLogInfo(@"%@ local user reset session", self.tag);
|
2016-09-11 22:53:12 +02:00
|
|
|
[self.storageManager removeIdentityKeyForRecipient:self.fingerprint.theirStableId];
|
|
|
|
[self.storageManager deleteAllSessionsForContact:self.fingerprint.theirStableId];
|
2016-09-21 19:59:45 +02:00
|
|
|
|
|
|
|
[[[TSInfoMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
inThread:self.thread
|
|
|
|
messageType:TSInfoMessageTypeSessionDidEnd] save];
|
|
|
|
|
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
2014-12-06 22:27:43 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
// pragma mark - OWSQRScannerDelegate
|
|
|
|
- (void)controller:(OWSQRCodeScanningViewController *)controller didDetectQRCodeWithData:(NSData *)data;
|
|
|
|
{
|
|
|
|
[self verifyCombinedFingerprintData:data];
|
|
|
|
}
|
2014-12-06 22:27:43 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (void)verifyCombinedFingerprintData:(NSData *)combinedFingerprintData
|
|
|
|
{
|
|
|
|
NSError *error;
|
|
|
|
if ([self.fingerprint matchesCombinedFingerprintData:combinedFingerprintData error:&error]) {
|
|
|
|
DDLogInfo(@"%@ Successfully verified privacy.", self.tag);
|
|
|
|
NSString *successTitle = NSLocalizedString(@"SUCCESSFUL_VERIFICATION_TITLE", nil);
|
|
|
|
NSString *dismissText = NSLocalizedString(@"DISMISS_BUTTON_TEXT", nil);
|
|
|
|
NSString *descriptionFormat = NSLocalizedString(
|
|
|
|
@"SUCCESSFUL_VERIFICATION_DESCRIPTION", @"Alert body after verifying privacy with {{other user's name}}");
|
|
|
|
NSString *successDescription = [NSString stringWithFormat:descriptionFormat, self.contactName];
|
|
|
|
UIAlertController *successAlertController =
|
|
|
|
[UIAlertController alertControllerWithTitle:successTitle
|
|
|
|
message:successDescription
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
UIAlertAction *dismissAction =
|
|
|
|
[UIAlertAction actionWithTitle:dismissText
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *_Nonnull action) {
|
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}];
|
|
|
|
[successAlertController addAction:dismissAction];
|
|
|
|
|
|
|
|
[self presentViewController:successAlertController animated:YES completion:nil];
|
|
|
|
} else {
|
|
|
|
[self failVerificationWithError:error];
|
|
|
|
}
|
2014-12-06 22:27:43 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (void)failVerificationWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
NSString *failureTitle = NSLocalizedString(@"FAILED_VERIFICATION_TITLE", @"alert title");
|
|
|
|
UIAlertController *failureAlertController =
|
|
|
|
[UIAlertController alertControllerWithTitle:failureTitle
|
|
|
|
message:error.localizedDescription
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
|
|
|
NSString *cancelText = NSLocalizedString(@"TXT_CANCEL_TITLE", nil);
|
|
|
|
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelText
|
|
|
|
style:UIAlertActionStyleCancel
|
|
|
|
handler:^(UIAlertAction *_Nonnull action) {
|
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}];
|
|
|
|
[failureAlertController addAction:cancelAction];
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// NSString retryText = NSLocalizedString(@"RETRY_BUTTON_TEXT", nil);
|
|
|
|
// UIAlertAction *retryAction = [UIAlertAction actionWithTitle:retryText style:UIAlertActionStyleDefault
|
|
|
|
// handler:^(UIAlertAction * _Nonnull action) {
|
|
|
|
//
|
|
|
|
// }];
|
|
|
|
// [failureAlertController addAction:retryAction];
|
|
|
|
[self presentViewController:failureAlertController animated:YES completion:nil];
|
|
|
|
|
|
|
|
DDLogWarn(@"%@ Identity verification failed with error: %@", self.tag, error);
|
|
|
|
}
|
2014-10-29 21:58:58 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(nullable void (^)(void))completion
|
|
|
|
{
|
|
|
|
self.qrScanningView.hidden = YES;
|
|
|
|
[super dismissViewControllerAnimated:flag completion:completion];
|
2014-12-27 01:08:05 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
+ (NSString *)tag
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
|
|
}
|
2015-01-22 10:49:19 +01:00
|
|
|
|
2016-09-11 22:53:12 +02:00
|
|
|
- (NSString *)tag
|
|
|
|
{
|
|
|
|
return self.class.tag;
|
2014-10-29 21:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2016-09-11 22:53:12 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|