Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow additional options for authorize and support custom LAPolicy #526

Merged
merged 31 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d8ff128
Update auth0-provider.js
chrismcleod Oct 9, 2022
3473808
Update auth0-provider.js
chrismcleod Oct 9, 2022
77824b7
Merge branch 'patch-1' of github.com:chrismcleod/react-native-auth0 i…
chrismcleod Oct 15, 2022
36d8c48
Merge branch 'master' into patch-1
chrismcleod Oct 15, 2022
d393aba
no message
chrismcleod Oct 15, 2022
e9b75f1
no message
chrismcleod Oct 15, 2022
e19da52
Merge branch 'master' into patch-1
chrismcleod Oct 17, 2022
3569318
Merge branch 'master' into patch-1
chrismcleod Oct 28, 2022
96662dc
Code review changes and rename LAPolicy to Security Level
poovamraj Jan 25, 2023
c60512f
Removed unsupported policies from conversion
poovamraj Jan 25, 2023
3dfc48b
Merge branch 'master' into patch-1
poovamraj Jan 25, 2023
aa4e5a2
Fixed compilation error
poovamraj Jan 25, 2023
ff2d366
Update ios/CredentialsManagerBridge.swift
poovamraj Jan 25, 2023
0122385
Update ios/CredentialsManagerBridge.swift
poovamraj Jan 26, 2023
d475bc0
Update ios/CredentialsManagerBridge.swift
poovamraj Jan 26, 2023
c2c61e5
Update ios/CredentialsManagerBridge.swift
poovamraj Jan 26, 2023
06b100b
Refactor name to LAPolicy
poovamraj Jan 26, 2023
fb3c6fa
Use NSInteger
poovamraj Jan 26, 2023
09582dd
Merge branch 'patch-1' of https://github.com/chrismcleod/react-native…
poovamraj Jan 26, 2023
7d56ab2
Defaults to deviceOwnerWithBiometrics
poovamraj Jan 26, 2023
0364a67
Start enum from 0
poovamraj Jan 26, 2023
ffda7cc
Revert "Start enum from 0"
poovamraj Jan 26, 2023
3693b4e
Refactor SecurityLevel to LAPolicy
poovamraj Jan 26, 2023
2bc1650
Merge branch 'master' into patch-1
poovamraj Jan 26, 2023
a7fb458
Refactor LAPolicy to LocalAuthenticationStrategy
poovamraj Jan 31, 2023
30ac621
Merge branch 'patch-1' of https://github.com/chrismcleod/react-native…
poovamraj Jan 31, 2023
518018e
Update src/credentials-manager/index.js
poovamraj Jan 31, 2023
dd18f57
Update src/credentials-manager/index.js
poovamraj Jan 31, 2023
cfd8430
Trigger Build
poovamraj Jan 31, 2023
69199f9
Merge branch 'patch-1' of https://github.com/chrismcleod/react-native…
poovamraj Jan 31, 2023
9c4407c
Fix broken tests
poovamraj Jan 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {TimeoutError} from './src/utils/fetchWithTimeout';
export {default as useAuth0} from './src/hooks/use-auth0';
export {default as Auth0Provider} from './src/hooks/auth0-provider';
export {default as LocalAuthenticationStrategy} from './src/credentials-manager/localAuthenticationStrategy';

import Auth0 from './src/auth0';
export default Auth0;
4 changes: 2 additions & 2 deletions ios/A0Auth0.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ - (dispatch_queue_t)methodQueue
[self.credentialsManagerBridge clearCredentialsWithResolve:resolve reject:reject];
}

RCT_EXPORT_METHOD(enableLocalAuthentication:(NSString *)title cancelTitle:(NSString *)cancelTitle fallbackTitle:(NSString *)fallbackTitle) {
[self.credentialsManagerBridge enableLocalAuthenticationWithTitle:title cancelTitle:title fallbackTitle:title];
RCT_EXPORT_METHOD(enableLocalAuthentication:(NSString *)title cancelTitle:(NSString *)cancelTitle fallbackTitle:(NSString *)fallbackTitle evaluationPolicy:(NSInteger)evaluationPolicy) {
[self.credentialsManagerBridge enableLocalAuthenticationWithTitle:title cancelTitle:cancelTitle fallbackTitle:fallbackTitle evaluationPolicy: evaluationPolicy];
}

RCT_EXPORT_METHOD(showUrl:(NSString *)urlString
Expand Down
13 changes: 11 additions & 2 deletions ios/CredentialsManagerBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Auth0
import Foundation
import LocalAuthentication

@objc
public class CredentialsManagerBridge: NSObject {
Expand Down Expand Up @@ -82,9 +83,17 @@ public class CredentialsManagerBridge: NSObject {
resolve(credentialsManager.clear())
}

@objc public func enableLocalAuthentication(title: String?, cancelTitle: String?, fallbackTitle: String?) {
@objc public func enableLocalAuthentication(title: String?, cancelTitle: String?, fallbackTitle: String?, evaluationPolicy: Int) {
let titleValue = title ?? "Please authenticate to continue"
self.credentialsManager.enableBiometrics(withTitle: titleValue, cancelTitle: cancelTitle, fallbackTitle: fallbackTitle)
let policyValue = self.convert(policyInt: evaluationPolicy)
self.credentialsManager.enableBiometrics(withTitle: titleValue, cancelTitle: cancelTitle, fallbackTitle: fallbackTitle, evaluationPolicy: policyValue)
}

func convert(policyInt: Int) -> LAPolicy {
Widcket marked this conversation as resolved.
Show resolved Hide resolved
if (policyInt == 2) {
return LAPolicy.deviceOwnerAuthentication
}
return LAPolicy.deviceOwnerAuthenticationWithBiometrics
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/credentials-manager/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Platform, NativeModules} from 'react-native';
import CredentialsManagerError from './credentialsManagerError';
import LocalAuthenticationStrategy from './localAuthenticationStrategy';

class CredentialsManager {
/**
Expand Down Expand Up @@ -83,13 +84,15 @@ class CredentialsManager {
* @param {String} description Android Only - optional - the text to use as description in the authentication screen. On some Android versions it might not be shown. Passing null will result in using the OS's default value.
* @param {String} cancelTitle iOS Only - optional - the cancel message to display on the local authentication prompt.
* @param {String} fallbackTitle iOS Only - optional - the fallback message to display on the local authentication prompt after a failed match.
* @param {String} strategy iOS Only - optional - the evaluation policy to use when accessing the credentials. Defaults to LocalAuthenticationStrategy.deviceOwnerWithBiometrics.
* @returns {Promise}
*/
async requireLocalAuthentication(
title,
description,
cancelTitle,
fallbackTitle,
strategy = LocalAuthenticationStrategy.deviceOwnerWithBiometrics,
) {
try {
await this._ensureCredentialManagerIsInitialized();
Expand All @@ -98,6 +101,7 @@ class CredentialsManager {
title,
cancelTitle,
fallbackTitle,
strategy
);
} else {
await this.Auth0Module.enableLocalAuthentication(title, description);
Expand Down
6 changes: 6 additions & 0 deletions src/credentials-manager/localAuthenticationStrategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const LocalAuthenticationStrategy = {
deviceOwnerWithBiometrics: 1,
deviceOwner: 2,
}

export default LocalAuthenticationStrategy;
9 changes: 7 additions & 2 deletions src/hooks/__tests__/use-auth0.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as React from 'react';
import {renderHook} from '@testing-library/react-hooks';
import Auth0Provider from '../auth0-provider';
import useAuth0 from '../use-auth0';
import LocalAuthenticationStrategy from '../../credentials-manager/localAuthenticationStrategy';
import {act} from 'react-dom/test-utils';

function makeJwt(claims) {
Expand Down Expand Up @@ -168,6 +169,8 @@ describe('The useAuth0 hook', () => {
scope: 'custom-scope',
audience: 'http://my-api',
customParam: '1234',
}, {
ephemeralSession: true
});

await waitForNextUpdate();
Expand All @@ -177,8 +180,9 @@ describe('The useAuth0 hook', () => {
scope: 'custom-scope openid profile email',
audience: 'http://my-api',
customParam: '1234',
}, {
ephemeralSession: true
},
{},
);
});

Expand Down Expand Up @@ -449,11 +453,12 @@ describe('The useAuth0 hook', () => {
'description',
'cancel',
'fallback',
LocalAuthenticationStrategy.deviceOwner
);

expect(
mockAuth0.credentialsManager.requireLocalAuthentication,
).toHaveBeenCalledWith('title', 'description', 'cancel', 'fallback');
).toHaveBeenCalledWith('title', 'description', 'cancel', 'fallback', LocalAuthenticationStrategy.deviceOwner);
});

it('dispatches an error when requireLocalAuthentication fails', async () => {
Expand Down