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