Skip to content

Commit

Permalink
chore: cleanup some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jun 4, 2024
1 parent 0d2c32e commit d7aea5a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 50 deletions.
83 changes: 38 additions & 45 deletions auth.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,51 +44,44 @@
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
env, Napi::Function::New(env, NoOp), "authCallback", 0, 1);

if (@available(macOS 10.12.2, *)) {

LAContext *context = [[LAContext alloc] init];

// The app-provided reason for requesting authentication
NSString *request_reason = [NSString stringWithUTF8String:reason.c_str()];

// Authenticate with biometry.
LAPolicy policy = LAPolicyDeviceOwnerAuthenticationWithBiometrics;

// Optionally set the duration for which Touch ID authentication reuse is
// allowable
if (reuse_duration > 0)
[context setTouchIDAuthenticationAllowableReuseDuration:reuse_duration];

__block Napi::ThreadSafeFunction tsfn = ts_fn;
[context
evaluatePolicy:policy
localizedReason:request_reason
reply:^(BOOL success, NSError *error) {
// Promise resolution callback
auto resolve_cb = [=](Napi::Env env,
Napi::Function noop_cb) {
deferred.Resolve(env.Null());
};

// Promise rejection callback
auto reject_cb = [=](Napi::Env env, Napi::Function noop_cb,
const char *error) {
deferred.Reject(Napi::String::New(env, error));
};

if (error) {
const char *err_str =
[error.localizedDescription UTF8String];
tsfn.BlockingCall(err_str, reject_cb);
} else {
tsfn.BlockingCall(resolve_cb);
};
tsfn.Release();
}];
} else {
ts_fn.Release();
deferred.Resolve(env.Null());
}
LAContext *context = [[LAContext alloc] init];

// The app-provided reason for requesting authentication
NSString *request_reason = [NSString stringWithUTF8String:reason.c_str()];

// Authenticate with biometry.
LAPolicy policy = LAPolicyDeviceOwnerAuthenticationWithBiometrics;

// Optionally set the duration for which Touch ID authentication reuse is
// allowable
if (reuse_duration > 0)
[context setTouchIDAuthenticationAllowableReuseDuration:reuse_duration];

__block Napi::ThreadSafeFunction tsfn = ts_fn;
[context
evaluatePolicy:policy
localizedReason:request_reason
reply:^(BOOL success, NSError *error) {
// Promise resolution callback
auto resolve_cb = [=](Napi::Env env, Napi::Function noop_cb) {
deferred.Resolve(env.Null());
};

// Promise rejection callback
auto reject_cb = [=](Napi::Env env, Napi::Function noop_cb,
const char *error) {
deferred.Reject(Napi::String::New(env, error));
};

if (error) {
const char *err_str =
[error.localizedDescription UTF8String];
tsfn.BlockingCall(err_str, reject_cb);
} else {
tsfn.BlockingCall(resolve_cb);
};
tsfn.Release();
}];

return deferred.Promise();
}
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const auth = require('bindings')('auth.node')

function promptTouchID(options) {
if (!options) {
if (!options || typeof options !== 'object') {
throw new Error('Options object is required.')
}

Expand Down
16 changes: 12 additions & 4 deletions test/module.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const { promptTouchID, canPromptTouchID } = require('../index')
describe('node-mac-auth', () => {
describe('canPromptTouchID()', () => {
it('should not throw', () => {
expect(() => { canPromptTouchID() }).to.not.throw()
expect(() => {
canPromptTouchID()
}).to.not.throw()
})

it('should return a boolean', () => {
Expand All @@ -23,15 +25,21 @@ describe('node-mac-auth', () => {
it('should throw if no reason is passed', () => {
expect(() => {
promptTouchID({})
}).to.throw(/Reason parameter is required./)
}).to.throw(/Reason parameter must be a string./)
})

it('should throw if reason is not a string', () => {
expect(() => {
promptTouchID({ reason: 1 })
}).to.throw(/Reason parameter must be a string./)
})


it('should throw if reuseDuration is not a number', () => {
expect(() => {
promptTouchID({ reason: 'i want to', reuseDuration: 'not-a-number' })
}).to.throw(/reuseDuration parameter must be a number./)
})

it('should not throw if no reuseDuration is passed', () => {
expect(() => {
promptTouchID({ reason: 'i want to' }, () => {})
Expand All @@ -41,4 +49,4 @@ describe('node-mac-auth', () => {
process.exit(0)
})
})
})
})

0 comments on commit d7aea5a

Please sign in to comment.