Skip to content

Commit

Permalink
chore: made evaluationPolicy and authenticationStrategy of LocalAuthe…
Browse files Browse the repository at this point in the history
…nticationOptions as optional
  • Loading branch information
desusai7 committed Aug 5, 2024
1 parent eaa61eb commit af120f9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 14 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ const credentials = await auth0.credentialsManager.getCredentials();

> :warning: The `requireLocalAuthentication` method is no longer available as part of the `CredentialsManager` class or the `useAuth0` Hook from v4 of the SDK.
> ℹ️ You need to use at least version `0.59.0` of React Native, as it uses `FragmentActivity` as the base activity, which is required for biometric authentication to work.
You can enable an additional level of user authentication before retrieving credentials using the local authentication supported by the device, for example PIN or fingerprint on Android, and Face ID or Touch ID on iOS.

Refer to the instructions below to understand how to enable authentication before retrieving credentials based on your setup:
Expand Down Expand Up @@ -466,16 +468,16 @@ The options for configuring the display of local authentication prompt, authenti

**Properties:**

| Property | Type | Description | Applicable Platforms |
| -------------------------- | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------- | -------------------- |
| `title` | `String` | The title of the authentication prompt. | Android, iOS |
| `subtitle` | `String` (optional) | The subtitle of the authentication prompt. | Android |
| `description` | `String` (optional) | The description of the authentication prompt. | Android |
| `cancelTitle` | `String` (optional) | The cancel button title of the authentication prompt. | Android, iOS |
| `evaluationPolicy` | `LocalAuthenticationStrategy` | The evaluation policy to use when prompting the user for authentication. Defaults to `deviceOwnerWithBiometrics`. | iOS |
| `fallbackTitle` | `String` (optional) | The fallback button title of the authentication prompt. | iOS |
| `authenticationLevel` | `LocalAuthenticationLevel` | The authentication level to use when prompting the user for authentication. Defaults to `strong`. | Android |
| `deviceCredentialFallback` | `Boolean` (optional) | Should the user be given the option to authenticate with their device PIN, pattern, or password instead of a biometric. | Android |
| Property | Type | Description | Applicable Platforms |
| -------------------------- | ---------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | -------------------- |
| `title` | `String` | The title of the authentication prompt. | Android, iOS |
| `subtitle` | `String` (optional) | The subtitle of the authentication prompt. | Android |
| `description` | `String` (optional) | The description of the authentication prompt. | Android |
| `cancelTitle` | `String` (optional) | The cancel button title of the authentication prompt. | Android, iOS |
| `evaluationPolicy` | `LocalAuthenticationStrategy` (optional) | The evaluation policy to use when prompting the user for authentication. Defaults to `deviceOwnerWithBiometrics`. | iOS |
| `fallbackTitle` | `String` (optional) | The fallback button title of the authentication prompt. | iOS |
| `authenticationLevel` | `LocalAuthenticationLevel` (optional) | The authentication level to use when prompting the user for authentication. Defaults to `strong`. | Android |
| `deviceCredentialFallback` | `Boolean` (optional) | Should the user be given the option to authenticate with their device PIN, pattern, or password instead of a biometric. | Android |

> :warning: You need a real device to test Local Authentication for iOS. Local Authentication is not available in simulators.
Expand Down
8 changes: 6 additions & 2 deletions src/auth0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Users from './management/users';
import { Telemetry } from './networking/telemetry';
import WebAuth from './webauth';
import LocalAuthenticationOptions from './credentials-manager/localAuthenticationOptions';
import addDefaultLocalAuthOptions from './utils/addDefaultLocalAuthOptions';

/**
* Auth0 for React Native client
Expand Down Expand Up @@ -33,12 +34,15 @@ class Auth0 {
localAuthenticationOptions?: LocalAuthenticationOptions;
}) {
const { domain, clientId, ...extras } = options;
const localAuthenticationOptions = options.localAuthenticationOptions
? addDefaultLocalAuthOptions(options.localAuthenticationOptions)
: undefined;
this.auth = new Auth({ baseUrl: domain, clientId, ...extras });
this.webAuth = new WebAuth(this.auth, options.localAuthenticationOptions);
this.webAuth = new WebAuth(this.auth, localAuthenticationOptions);
this.credentialsManager = new CredentialsManager(
domain,
clientId,
options.localAuthenticationOptions
localAuthenticationOptions
);
this.options = options;
}
Expand Down
4 changes: 2 additions & 2 deletions src/credentials-manager/localAuthenticationOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ interface LocalAuthenticationOptions {
/**
* The evaluation policy to use when prompting the user for authentication. Defaults to LocalAuthenticationStrategy.deviceOwnerWithBiometrics. **Applicable for iOS only.**
*/
evaluationPolicy: LocalAuthenticationStrategy;
evaluationPolicy?: LocalAuthenticationStrategy;
/**
* The fallback button title of the authentication prompt. **Applicable for iOS only.**
*/
fallbackTitle?: String;
/**
* The authentication level to use when prompting the user for authentication. Defaults to LocalAuthenticationLevel.strong. **Applicable for Android only.**
*/
authenticationLevel: LocalAuthenticationLevel;
authenticationLevel?: LocalAuthenticationLevel;
/**
* Should the user be given the option to authenticate with their device PIN, pattern, or password instead of a biometric. **Applicable for Android only.**
*/
Expand Down
38 changes: 38 additions & 0 deletions src/utils/__tests__/addDefaultLocalAuthOptions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import LocalAuthenticationLevel from '../../credentials-manager/localAuthenticationLevel';
import addDefaultLocalAuthOptions from '../addDefaultLocalAuthOptions';
import LocalAuthenticationStrategy from '../../credentials-manager/localAuthenticationStrategy';

describe('addDefaultLocalAuthenticationOptions', () => {
it('should return default options when no options are provided', () => {
const localAuthOptions = { title: 'Please authenticate' };
const result = addDefaultLocalAuthOptions(localAuthOptions);
expect(result).toEqual({
title: 'Please authenticate',
authenticationLevel: LocalAuthenticationLevel.strong,
evaluationPolicy: LocalAuthenticationStrategy.deviceOwnerWithBiometrics,
});
});

it('should override default options with provided options', () => {
const localAuthOptions = {
title: 'Please authenticate',
authenticationLevel: LocalAuthenticationLevel.deviceCredential,
evaluationPolicy: LocalAuthenticationStrategy.deviceOwner,
};
const result = addDefaultLocalAuthOptions(localAuthOptions);
expect(result).toEqual(localAuthOptions);
});

it('should merge default options with partially provided options', () => {
const options = {
title: 'Please authenticate',
authenticationLevel: LocalAuthenticationLevel.deviceCredential,
};
const result = addDefaultLocalAuthOptions(options);
expect(result).toEqual({
title: 'Please authenticate',
authenticationLevel: LocalAuthenticationLevel.deviceCredential,
evaluationPolicy: LocalAuthenticationStrategy.deviceOwnerWithBiometrics,
});
});
});
19 changes: 19 additions & 0 deletions src/utils/addDefaultLocalAuthOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import LocalAuthenticationOptions from '../credentials-manager/localAuthenticationOptions';
import LocalAuthenticationStrategy from '../credentials-manager/localAuthenticationStrategy';
import LocalAuthenticationLevel from '../credentials-manager/localAuthenticationLevel';

const defaultLocalAuthOptions = {
evaluationPolicy: LocalAuthenticationStrategy.deviceOwnerWithBiometrics,
authenticationLevel: LocalAuthenticationLevel.strong,
};

function addDefaultLocalAuthOptions(
localAuthenticationOptions: LocalAuthenticationOptions
): LocalAuthenticationOptions {
return {
...defaultLocalAuthOptions,
...localAuthenticationOptions,
};
}

export default addDefaultLocalAuthOptions;

0 comments on commit af120f9

Please sign in to comment.