-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
635ec1b
commit c87a128
Showing
4 changed files
with
82 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// auth.h | ||
#ifndef AUTH_H | ||
#define AUTH_H | ||
|
||
#include <stdbool.h> | ||
|
||
struct AuthResult { | ||
bool success; // true for success, false for failure | ||
char* error_msg; // Error message if any | ||
int error_code; // Error code if any | ||
}; | ||
|
||
struct AuthResult Authenticate(char const* reason); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// auth.m | ||
#import <LocalAuthentication/LocalAuthentication.h> | ||
#include "auth.h" | ||
|
||
struct AuthResult Authenticate(char const* reason) { | ||
struct AuthResult authResult; | ||
LAContext *myContext = [[LAContext alloc] init]; | ||
NSError *authError = nil; | ||
dispatch_semaphore_t sema = dispatch_semaphore_create(0); | ||
NSString *nsReason = [NSString stringWithUTF8String:reason]; | ||
__block bool success = false; | ||
__block NSString *errorMessage = nil; | ||
__block int errorCode = 0; | ||
|
||
// Use LAPolicyDeviceOwnerAuthentication to allow biometrics and password fallback | ||
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&authError]) { | ||
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthentication | ||
localizedReason:nsReason | ||
reply:^(BOOL policySuccess, NSError *error) { | ||
if (policySuccess) { | ||
success = true; // Authentication successful | ||
} else { | ||
success = false; | ||
errorCode = (int)[error code]; | ||
errorMessage = [error localizedDescription]; | ||
if (error.code == LAErrorUserFallback || error.code == LAErrorAuthenticationFailed) { | ||
// Prompting for password | ||
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthentication | ||
localizedReason:@"Please enter your password" | ||
reply:^(BOOL pwdSuccess, NSError *error) { | ||
if (pwdSuccess) { | ||
success = true; | ||
} else { | ||
success = false; | ||
errorCode = (int)[error code]; | ||
errorMessage = [error localizedDescription]; | ||
} | ||
dispatch_semaphore_signal(sema); | ||
}]; | ||
} else { | ||
errorCode = (int)[error code]; | ||
errorMessage = [error localizedDescription]; | ||
} | ||
} | ||
dispatch_semaphore_signal(sema); | ||
}]; | ||
} else { | ||
success = false; // Cannot evaluate policy | ||
errorCode = (int)[authError code]; | ||
errorMessage = [authError localizedDescription]; | ||
} | ||
|
||
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); | ||
dispatch_release(sema); | ||
|
||
authResult.success = success; | ||
authResult.error_code = errorCode; | ||
if (errorMessage != nil) { | ||
authResult.error_msg = strdup([errorMessage UTF8String]); // Copy error message to C string | ||
} else { | ||
authResult.error_msg = NULL; | ||
} | ||
|
||
return authResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters