| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #import "ios/chrome/browser/ui/settings/reauthentication_module.h" | 4 #import "ios/chrome/browser/ui/settings/reauthentication_module.h" |
| 5 | 5 |
| 6 #import <LocalAuthentication/LocalAuthentication.h> | 6 #import <LocalAuthentication/LocalAuthentication.h> |
| 7 | 7 |
| 8 #import "base/ios/weak_nsobject.h" | 8 #import "base/logging.h" |
| 9 #import "base/mac/scoped_nsobject.h" | 9 |
| 10 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 11 #error "This file requires ARC support." |
| 12 #endif |
| 10 | 13 |
| 11 @implementation ReauthenticationModule { | 14 @implementation ReauthenticationModule { |
| 12 // Authentication context on which the authentication policy is evaluated. | 15 // Authentication context on which the authentication policy is evaluated. |
| 13 base::scoped_nsobject<LAContext> _context; | 16 LAContext* _context; |
| 14 | 17 |
| 15 // Accessor allowing the module to request the update of the time when the | 18 // Accessor allowing the module to request the update of the time when the |
| 16 // successful re-authentication was performed and to get the time of the last | 19 // successful re-authentication was performed and to get the time of the last |
| 17 // successful re-authentication. | 20 // successful re-authentication. |
| 18 base::WeakNSProtocol<id<SuccessfulReauthTimeAccessor>> | 21 __weak id<SuccessfulReauthTimeAccessor> _successfulReauthTimeAccessor; |
| 19 _successfulReauthTimeAccessor; | |
| 20 } | 22 } |
| 21 | 23 |
| 22 - (instancetype)initWithSuccessfulReauthTimeAccessor: | 24 - (instancetype)initWithSuccessfulReauthTimeAccessor: |
| 23 (id<SuccessfulReauthTimeAccessor>)successfulReauthTimeAccessor { | 25 (id<SuccessfulReauthTimeAccessor>)successfulReauthTimeAccessor { |
| 24 DCHECK(successfulReauthTimeAccessor); | 26 DCHECK(successfulReauthTimeAccessor); |
| 25 self = [super init]; | 27 self = [super init]; |
| 26 if (self) { | 28 if (self) { |
| 27 _context.reset([[LAContext alloc] init]); | 29 _context = [[LAContext alloc] init]; |
| 28 _successfulReauthTimeAccessor.reset(successfulReauthTimeAccessor); | 30 _successfulReauthTimeAccessor = successfulReauthTimeAccessor; |
| 29 } | 31 } |
| 30 return self; | 32 return self; |
| 31 } | 33 } |
| 32 | 34 |
| 33 - (BOOL)canAttemptReauth { | 35 - (BOOL)canAttemptReauth { |
| 34 // The authentication method is Touch ID or passcode. | 36 // The authentication method is Touch ID or passcode. |
| 35 return | 37 return |
| 36 [_context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]; | 38 [_context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]; |
| 37 } | 39 } |
| 38 | 40 |
| 39 - (void)attemptReauthWithLocalizedReason:(NSString*)localizedReason | 41 - (void)attemptReauthWithLocalizedReason:(NSString*)localizedReason |
| 40 handler:(void (^)(BOOL success))handler { | 42 handler:(void (^)(BOOL success))handler { |
| 41 if ([self isPreviousAuthValid]) { | 43 if ([self isPreviousAuthValid]) { |
| 42 handler(YES); | 44 handler(YES); |
| 43 return; | 45 return; |
| 44 } | 46 } |
| 45 | 47 |
| 46 _context.reset([[LAContext alloc] init]); | 48 _context = [[LAContext alloc] init]; |
| 47 | 49 |
| 48 // No fallback option is provided. | 50 // No fallback option is provided. |
| 49 _context.get().localizedFallbackTitle = @""; | 51 _context.localizedFallbackTitle = @""; |
| 50 | 52 |
| 51 base::WeakNSObject<ReauthenticationModule> weakSelf(self); | 53 __weak ReauthenticationModule* weakSelf = self; |
| 52 void (^replyBlock)(BOOL, NSError*) = ^(BOOL success, NSError* error) { | 54 void (^replyBlock)(BOOL, NSError*) = ^(BOOL success, NSError* error) { |
| 53 dispatch_async(dispatch_get_main_queue(), ^{ | 55 dispatch_async(dispatch_get_main_queue(), ^{ |
| 54 base::scoped_nsobject<ReauthenticationModule> strongSelf( | 56 ReauthenticationModule* strongSelf = weakSelf; |
| 55 [weakSelf retain]); | |
| 56 if (!strongSelf) | 57 if (!strongSelf) |
| 57 return; | 58 return; |
| 58 if (success) { | 59 if (success) { |
| 59 [strongSelf.get() | 60 [strongSelf->_successfulReauthTimeAccessor updateSuccessfulReauthTime]; |
| 60 ->_successfulReauthTimeAccessor updateSuccessfulReauthTime]; | |
| 61 } | 61 } |
| 62 handler(success); | 62 handler(success); |
| 63 }); | 63 }); |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 [_context evaluatePolicy:LAPolicyDeviceOwnerAuthentication | 66 [_context evaluatePolicy:LAPolicyDeviceOwnerAuthentication |
| 67 localizedReason:localizedReason | 67 localizedReason:localizedReason |
| 68 reply:replyBlock]; | 68 reply:replyBlock]; |
| 69 } | 69 } |
| 70 | 70 |
| 71 - (BOOL)isPreviousAuthValid { | 71 - (BOOL)isPreviousAuthValid { |
| 72 BOOL previousAuthValid = NO; | 72 BOOL previousAuthValid = NO; |
| 73 const int kIntervalForValidAuthInSeconds = 60; | 73 const int kIntervalForValidAuthInSeconds = 60; |
| 74 NSDate* lastSuccessfulReauthTime = | 74 NSDate* lastSuccessfulReauthTime = |
| 75 [_successfulReauthTimeAccessor lastSuccessfulReauthTime]; | 75 [_successfulReauthTimeAccessor lastSuccessfulReauthTime]; |
| 76 if (lastSuccessfulReauthTime) { | 76 if (lastSuccessfulReauthTime) { |
| 77 NSDate* currentTime = [NSDate date]; | 77 NSDate* currentTime = [NSDate date]; |
| 78 NSTimeInterval timeSincePreviousSuccessfulAuth = | 78 NSTimeInterval timeSincePreviousSuccessfulAuth = |
| 79 [currentTime timeIntervalSinceDate:lastSuccessfulReauthTime]; | 79 [currentTime timeIntervalSinceDate:lastSuccessfulReauthTime]; |
| 80 if (timeSincePreviousSuccessfulAuth < kIntervalForValidAuthInSeconds) { | 80 if (timeSincePreviousSuccessfulAuth < kIntervalForValidAuthInSeconds) { |
| 81 previousAuthValid = YES; | 81 previousAuthValid = YES; |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 return previousAuthValid; | 84 return previousAuthValid; |
| 85 } | 85 } |
| 86 | 86 |
| 87 @end | 87 @end |
| OLD | NEW |