diff --git a/README.md b/README.md index ab4a9190..473a5ec3 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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. diff --git a/src/auth0.ts b/src/auth0.ts index bf4031b4..16e0e9e6 100644 --- a/src/auth0.ts +++ b/src/auth0.ts @@ -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 @@ -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; } diff --git a/src/credentials-manager/localAuthenticationOptions.ts b/src/credentials-manager/localAuthenticationOptions.ts index 3a846006..ce56a84b 100644 --- a/src/credentials-manager/localAuthenticationOptions.ts +++ b/src/credentials-manager/localAuthenticationOptions.ts @@ -25,7 +25,7 @@ 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.** */ @@ -33,7 +33,7 @@ interface LocalAuthenticationOptions { /** * 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.** */ diff --git a/src/utils/__tests__/addDefaultLocalAuthOptions.spec.js b/src/utils/__tests__/addDefaultLocalAuthOptions.spec.js new file mode 100644 index 00000000..b4dbcf32 --- /dev/null +++ b/src/utils/__tests__/addDefaultLocalAuthOptions.spec.js @@ -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, + }); + }); +}); diff --git a/src/utils/addDefaultLocalAuthOptions.ts b/src/utils/addDefaultLocalAuthOptions.ts new file mode 100644 index 00000000..7c80fef5 --- /dev/null +++ b/src/utils/addDefaultLocalAuthOptions.ts @@ -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;