diff --git a/EXAMPLES.md b/EXAMPLES.md index a789e7c3..638e94c8 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -44,6 +44,11 @@ - [Using MRRT with Hooks](#using-mrrt-with-hooks) - [Using MRRT with Auth0 Class](#using-mrrt-with-auth0-class) - [Web Platform Configuration](#web-platform-configuration) +- [Custom Token Exchange (RFC 8693)](#custom-token-exchange-rfc-8693) + - [Using Custom Token Exchange with Hooks](#using-custom-token-exchange-with-hooks) + - [Using Custom Token Exchange with Auth0 Class](#using-custom-token-exchange-with-auth0-class) + - [With Organization Context](#with-organization-context) + - [Subject Token Type Requirements](#subject-token-type-requirements) - [Native to Web SSO (Early Access)](#native-to-web-sso-early-access) - [Overview](#native-to-web-sso-overview) - [Prerequisites](#native-to-web-sso-prerequisites) @@ -758,6 +763,250 @@ function App() { } ``` +## Custom Token Exchange (RFC 8693) + +Custom Token Exchange allows you to exchange external identity provider tokens for Auth0 tokens using the [RFC 8693 OAuth 2.0 Token Exchange](https://datatracker.ietf.org/doc/html/rfc8693) specification. This enables scenarios where users authenticate with an external system and that token needs to be exchanged for Auth0 tokens. + +> ⚠️ **Important**: The external token must be validated in Auth0 Actions using cryptographic verification. See the [Auth0 Custom Token Exchange documentation](https://auth0.com/docs/authenticate/custom-token-exchange) for setup instructions. + +### Using Custom Token Exchange with Hooks + +```typescript +import React from 'react'; +import { Button, Alert } from 'react-native'; +import { + useAuth0, + AuthenticationException, + AuthenticationErrorCodes, +} from 'react-native-auth0'; + +function TokenExchangeScreen() { + const { customTokenExchange, user, error } = useAuth0(); + + const handleExchange = async () => { + try { + // Exchange an external token for Auth0 tokens + const credentials = await customTokenExchange({ + subjectToken: 'token-from-external-provider', + subjectTokenType: 'urn:acme:legacy-system-token', + scope: 'openid profile email', + audience: 'https://api.example.com', + }); + + Alert.alert('Success', `Logged in as ${user?.name}`); + } catch (e) { + if (e instanceof AuthenticationException) { + switch (e.type) { + case AuthenticationErrorCodes.INVALID_SUBJECT_TOKEN: + Alert.alert('Error', 'The external token is invalid or expired'); + break; + case AuthenticationErrorCodes.UNSUPPORTED_TOKEN_TYPE: + Alert.alert('Error', 'The token type is not supported'); + break; + case AuthenticationErrorCodes.TOKEN_EXCHANGE_NOT_CONFIGURED: + Alert.alert( + 'Error', + 'Custom Token Exchange is not configured for this tenant' + ); + break; + case AuthenticationErrorCodes.TOKEN_VALIDATION_FAILED: + Alert.alert('Error', 'Token validation failed in Auth0 Action'); + break; + case AuthenticationErrorCodes.NETWORK_ERROR: + Alert.alert('Error', 'Network error. Please check your connection.'); + break; + default: + Alert.alert('Error', e.message); + } + } else { + console.error('Token exchange failed:', e); + } + } + }; + + return