Skip to content

Commit

Permalink
put c in own file
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Pickett committed Sep 23, 2024
1 parent 635ec1b commit c87a128
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 71 deletions.
2 changes: 1 addition & 1 deletion ee/localserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func New(ctx context.Context, k types.Knapsack) (*localServer, error) {
// curl localhost:40978/acceleratecontrol --data '{"interval":"250ms", "duration":"1s"}'
// mux.Handle("/acceleratecontrol", ls.requestAccelerateControlHandler())
// curl localhost:40978/id
mux.Handle("/id", ls.requestIdHandler())
// mux.Handle("/id", ls.requestIdHandler())

srv := &http.Server{
Handler: otelhttp.NewHandler(
Expand Down
15 changes: 15 additions & 0 deletions ee/presencedetection/auth.h
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
65 changes: 65 additions & 0 deletions ee/presencedetection/auth.m
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;
}
71 changes: 1 addition & 70 deletions ee/presencedetection/presencedetection_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,7 @@ package presencedetection
#cgo CFLAGS: -x objective-c -fmodules -fblocks
#cgo LDFLAGS: -framework CoreFoundation -framework LocalAuthentication -framework Foundation
#include <stdlib.h>
#include <stdio.h>
#import <LocalAuthentication/LocalAuthentication.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) {
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;
}
#include "auth.h"
*/
import "C"
import (
Expand Down

0 comments on commit c87a128

Please sign in to comment.