getAuthenticationContext
The getAuthenticationContext function enables an app using the Beyond Identity SDK to get information associated with the current authentication request.
Dependencies​
The getAuthenticationContext function requires the Beyond Identity SDK.
- JavaScript
- Kotlin
- Swift
- React Native
- Flutter
yarn add @beyondidentity/bi-sdk-js
or
npm install @beyondidentity/bi-sdk-js
Requirements​
minSdkVersion26 or later.- Apps with Java sources that call the SDK must build with JDK 21. Kotlin-only apps are unaffected.
- Biometric prompts require a
FragmentActivity, such asAppCompatActivity.
Gradle​
To enable the retrieval of Cloudsmith hosted packages via Gradle, we need to add the Cloudsmith repository to
the root/build.gradle file.
repositories {
maven {
url "https://packages.beyondidentity.com/public/bi-sdk-android/maven/"
}
}
After the repository is added, we can specify the Beyond Identity dependencies.
dependencies {
implementation 'com.beyondidentity.android.sdk:embedded:[version]'
}
Requirements​
The SDK supports iOS 15.0 or later.
Swift Package Manager​
From Xcode​
- From the Xcode
Filemenu, selectAdd Packagesand add the following url:
https://github.com/gobeyondidentity/bi-sdk-swift
- Select a version and hit Next.
- Select a target matching the SDK you wish to use.
From Package.swift​
- With Swift Package Manager,
add the following
dependencyto yourPackage.swift:
dependencies: [
.package(url: "https://github.com/gobeyondidentity/bi-sdk-swift.git", from: [version])
]
- Run
swift build
Cocoapods​
Add the pod to your Podfile:
pod 'BeyondIdentityEmbedded'
And then run:
pod install
After installing import with
import BeyondIdentityEmbedded
Using react-native init or an expo app.​
- react-native init
- expo
The SDK requires React Native 0.88.0 or later.
Install the SDK with yarn or npm:
yarn add @beyondidentity/bi-sdk-react-native
npm install @beyondidentity/bi-sdk-react-native
Update native requirements in your ios and android folders:
iOS​
Make sure your ios/Podfile supports "minimum deployment target" 15.1 or later
platform :ios, '15.1'
Navigate to your ios folder and run:
cd ios && pod install
Android​
Make sure your android/build.gradle supports minSdkVersion 26 or later
buildscript {
ext {
minSdkVersion = 26
}
}
Add the following maven url to your repositories in your android/build.gradle
allprojects {
repositories {
maven {
url "https://packages.beyondidentity.com/public/bi-sdk-android/maven/"
}
}
}
For biometric prompts to work on Android, your Activity must extend FragmentActivity. The default ReactActivity already does.
This package requires custom native code and can be used with Development builds or prebuild and cannot be used with Expo Go.
npx expo install @beyondidentity/bi-sdk-react-native
Add the SDK config plugin to the plugins array of your app.{json,config.js,config.ts}:
{
"expo": {
"plugins": [["@beyondidentity/bi-sdk-react-native"]]
}
}
The SDK requires certain minimum native versions. Set these requirments with expo-build-properties.
npx expo install expo-build-properties
{
"expo": {
"plugins": [
["@beyondidentity/bi-sdk-react-native"],
[
"expo-build-properties",
{
"android": {
"minSdkVersion": 26
},
"ios": {
"deploymentTarget": "15.1"
}
}
]
]
}
}
Finally, rebuild your app as described in Expo's Adding custom native code guide.
The SDK requires Flutter 3.44.0 or later.
Pub.Dev​
Add the Beyond Identity Embedded SDK to your dependencies
dependencies:
bi_sdk_flutter: x.y.z
and run an implicit flutter pub get.
Update Android​
Please make sure your android/build.gradle supports minSdkVersion 26 or later.
buildscript {
ext {
minSdkVersion = 26
}
}
For biometric prompts to work on Android, your Activity must extend FragmentActivity. Use FlutterFragmentActivity instead of FlutterActivity:
import io.flutter.embedding.android.FlutterFragmentActivity
class MainActivity : FlutterFragmentActivity()
Update iOS​
Please make sure your project supports "minimum deployment target" 15.1 or later.
In your ios/Podfile set:
platform :ios, '15.1'
Prerequisites​
Before making a call to getAuthenticationContext, you must complete the following prerequisite calls:
- Import the required types and functions from the SDK.
- JavaScript
- Kotlin
- Swift
- React Native
- Flutter
import { Embedded } from '@beyondidentity/bi-sdk-js';
import com.beyondidentity.embedded.sdk.EmbeddedSdk
import BeyondIdentityEmbedded
import { Embedded } from '@beyondidentity/bi-sdk-react-native';
import 'package:bi_sdk_flutter/embeddedsdk.dart';
- Initialize the SDK.
- JavaScript
- Kotlin
- Swift
- React Native
- Flutter
// --- Initialize with required arguments
try {
const embedded = await Embedded.initialize();
console.log("Initialization successful", embedded);
} catch (error) {
console.error("Initialization failed:", error);
}
// --- Initialize with required and optional arguments
const config = {
allowedDomains: ["example.com", "another-example.com"],
logger: function (logType, message) {
console.log(`[${logType}] ${message}`);
},
};
try {
const embedded = await Embedded.initialize(config);
console.log("Initialization successful", embedded);
} catch (error) {
console.error("Initialization failed:", error);
}
// --- Initialize with required arguments
EmbeddedSdk.init(
app = this,
logger = { logMessage ->
Log.d("BeyondIdentityLog", logMessage)
}
)
// --- Initialize with required and optional arguments
EmbeddedSdk.init(
app = this,
biometricAskPrompt = "Please provide your biometric",
logger = { logMessage ->
Log.d("BeyondIdentityLog", logMessage)
},
allowedDomains = listOf("example.com", "another-example.com")
)
// --- Initialize from an Activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
EmbeddedSdk.init(
app = application,
logger = { logMessage ->
Log.d("BeyondIdentityLog", logMessage)
}
)
// Hand the SDK the Activity that called init()
EmbeddedSdk.setActivity(this)
}
}
// --- Initialize with required arguments
Embedded.initialize(
biometricAskPrompt: "Please provide your biometric"
) { result in
switch result {
case .success():
print("Initialization successful")
case .failure(let error):
print("Initialization failed: \(error)")
}
}
// --- Initialize with required and optional arguments
Embedded.initialize(
allowedDomains: ["example.com", "another-example.com"],
biometricAskPrompt: "Please provide your biometric",
logger: { (logType, message) in
print("\(logType): \(message)")
}
) { result in
switch result {
case .success():
print("Initialization successful")
case .failure(let error):
print("Initialization failed: \(error)")
}
}
// --- Initialize with required arguments
try {
const response = await Embedded.initialize("Please provide your biometric");
console.log(response);
} catch (error) {
console.error("Initialization failed:", error);
}
// --- Initialize with required and optional arguments
try {
const response = await Embedded.initialize(
"Please provide your biometric",
["example.com", "another-example.com"]
);
console.log(response);
Embedded.logEventEmitter.addListener(
"BeyondIdentityLogger",
(message: string) => {
console.log(message);
}
);
} catch (error) {
console.error("Initialization failed:", error);
}
// --- Initialize with required arguments
EmbeddedSdk.initialize('Please provide your biometric');
// --- Initialize with required and optional arguments
EmbeddedSdk.initialize(
'Please provide your biometric',
allowedDomains: ["example.com", "another-example.com"],
logger: EmbeddedSdk.enableLogger(),
).then((_) {
print('Initialization successful');
}).catchError((error) {
print('Initialization failed: $error');
});
- Use getAuthenticationContext to get information associated with the current authentication request (see example below).
Parameters​
- JavaScript
- Kotlin
- Swift
- React Native
- Flutter
| Parameter | Type | Description |
|---|---|---|
| url | string | Required. The authentication URL of the current transaction. This URL is generated by the Beyond Identity API's /authorize endpoint in response to a standard OpenID Connect request from your app (see example below). (/bi-authenticate?request=someToken) |
| Parameter | Type | Description |
|---|---|---|
| url | String | Required. The authentication URL of the current transaction. This URL is generated by the Beyond Identity API's /authorize endpoint in response to a standard OpenID Connect request from your app (see example below). (/bi-authenticate?request=someToken) |
| callback | (Result<AuthenticationContext>) -> Unit | Required. Called with the response on success, or a Throwable on failure. |
Alternatively, call the overload that takes a dispatcher instead of a callback. It returns a
Flow that emits the result once it is collected:| Parameter | Type | Description |
|---|---|---|
| url | String | Required. The authentication URL of the current transaction. This URL is generated by the Beyond Identity API's /authorize endpoint in response to a standard OpenID Connect request from your app (see example below). (/bi-authenticate?request=someToken) |
| dispatcher | CoroutineDispatcher | Optional. The coroutine dispatcher to run on. Defaults to Dispatchers.Default. |
| Parameter | Type | Description |
|---|---|---|
| url | URL | Required. The authentication URL of the current transaction. This URL is generated by the Beyond Identity API's /authorize endpoint in response to a standard OpenID Connect request from your app (see example below). (/bi-authenticate?request=someToken) |
| callback | @escaping (Result<AuthenticationContext, BISDKError>) -> Void | Required. Called with the result. |
The async throws variant takes the same parameters without callback, returns the result directly, and throws a BISDKError on failure.
| Parameter | Type | Description |
|---|---|---|
| url | string | Required. The authentication URL of the current transaction. This URL is generated by the Beyond Identity API's /authorize endpoint in response to a standard OpenID Connect request from your app (see example below). (/bi-authenticate?request=someToken) |
| Parameter | Type | Description |
|---|---|---|
| url | String | Required. The authentication URL of the current transaction. This URL is generated by the Beyond Identity API's /authorize endpoint in response to a standard OpenID Connect request from your app (see example below). (/bi-authenticate?request=someToken) |
Returns​
On success, the getAuthenticationContext function returns an AuthenticationContext. On Kotlin, the result is delivered to the callback, or emitted by the returned Flow. On Swift, it is delivered to the callback, or returned by the async throws variant. On JavaScript, React Native and Flutter, the function returns a Promise or Future.
- JavaScript
- Kotlin
- Swift
- React Native
- Flutter
On success, returns a Promise that resolves to an AuthenticationContext, which itself is a JSON object that contains the following keys:
| Key | Description |
|---|---|
| application | An object containing the application's id and displayName. |
| authMethods | An array containing the type of authentication methods the application supports. |
| authUrl | A string containing a URL you must pass into authenticate or authenticateOtp. |
| origin | An object containing the sourceIp, userAgent, geolocation, and referer of the request. |
Currently, the following authentication methods are supported, and more are coming soon:
| Auth method | Description |
|---|---|
| webauthn_passkey | Generates a hardware key within your device's trusted execution environment (TEE). |
| software_passkey | A generates a passkey securely created within the browser's context. |
| email_one_time_password | Enables a workflow that verifies identity via an email one-time password. |
data class AuthenticationContext(val authUrl: String, val application: Application, val origin: Origin) {
data class Origin(val sourceIp: String?, val userAgent: String?, val geolocation: String?, val referer: String?)
data class Application(val id: String, val displayName: String?)
}
| Key | Description |
|---|---|
| authUrl | A URL you must pass into authenticate or authenticateOtp instead of the original URL. |
| application | The application's id and optional displayName. |
| origin | The optional sourceIp, userAgent, geolocation and referer of the request. |
public struct AuthenticationContext: Equatable, Hashable {
public let authUrl: URL
public let application: Application // id: Application.Id, displayName: String?
public let origin: Origin // sourceIp, userAgent, geolocation, referer: String?
}
| Key | Description |
|---|---|
| authUrl | A URL you must pass into authenticate or authenticateOtp instead of the original URL. |
| application | The application's id and optional displayName. |
| origin | The optional sourceIp, userAgent, geolocation and referer of the request. |
interface AuthenticationContext {
authUrl: string;
application: {
id: string;
displayName?: string;
};
origin: {
sourceIp?: string;
userAgent?: string;
geolocation?: string;
referer?: string;
};
}
| Key | Description |
|---|---|
| authUrl | A URL you must pass into authenticate or authenticateOtp instead of the original URL. |
| application | The application's id and optional displayName. |
| origin | The optional sourceIp, userAgent, geolocation and referer of the request. |
class AuthenticationContext {
String authUrl;
AuthenticationContextApplication application; // String id, String? displayName
AuthenticationContextOrigin origin; // String? sourceIp, userAgent, geolocation, referer
}
| Key | Description |
|---|---|
| authUrl | A URL you must pass into authenticate or authenticateOtp instead of the original URL. |
| application | The application's id and optional displayName. |
| origin | The optional sourceIp, userAgent, geolocation and referer of the request. |
Examples​
Example: Call getAuthenticationContext​
- JavaScript
- Kotlin
- Swift
- React Native
- Flutter
let authenticationContext = await embedded.getAuthenticationContext(url);
EmbeddedSdk.getAuthenticationContext(url) { result ->
result.onSuccess { context ->
// Pass context.authUrl, not the original url, to authenticate or authenticateOtp
print("Signing in to ${context.application.displayName}")
}
result.onFailure { e ->
print("Error getting authentication context: $e")
}
}
Embedded.shared.getAuthenticationContext(url: url) { result in
switch result {
case .success(let context):
// Pass context.authUrl, not the original url, to authenticate or authenticateOtp
print("Signing in to \(context.application.displayName ?? "")")
case .failure(let error):
print("Error getting authentication context: \(error.localizedDescription)")
}
}
const context = await Embedded.getAuthenticationContext(url);
// Pass context.authUrl, not the original url, to authenticate or authenticateOtp
AuthenticationContext context = await EmbeddedSdk.getAuthenticationContext(url);
// Pass context.authUrl, not the original url, to authenticate or authenticateOtp
Example: Retrieve Beyond Identity authentication url via OIDC call​
The app sends an OIDC call to the Beyond Identity API's /authorize endpoint:
GET https://auth-us.beyondidentity.com/v1/tenants/{TENANT_ID}/realms/{REALM_ID}/applications/{APPLICATION_ID}/authorize?client_id={CLIENT_ID}&scope=openid&response_type=code&redirect_uri={REDIRECT_URI}&state=8LIY29kN8Oz7zrAhb8xb0yvem-gvnRy1HTn03MAuL_E
where the following elements match the corresponding properties of the app as configured in your Beyond Identity tenant:
| Property | Description |
|---|---|
| TENANT_ID | The Tenant ID of the tenant in which the app is configured. |
| REALM_ID | The Realm ID of the realm in which the app is configured. |
| APPLICATION_ID | The Application ID from the header of the app's configuration page. |
| CLIENT_ID | The Client ID from the External Protocol tab of the app's configuration page. |
| REDIRECT_URI | Matches one of the Redirect URIs configured on the External Protocol tab of the app's configuration page, URL encoded. |
When the Invocation Type configured on the Authenticator Config tab of the app's configuration page is set to Manual, returns a JSON object:
{ "authenticate_url": "http://localhost:8083/bi-authenticate?request={BI_JWT}" }
where BI_JWT is a base64url encoded JWT token containing the challenge and other data to kick off the passkey authentication.
When the Invocation Type on the app is set to Automatic, it returns an HTTP 302 to the authentication URL:
http/1.1 302 Found
...
location: http://localhost:8083/bi-authenticate?request={BI_JWT}
where BI_JWT is a base64url encoded JWT token containing the challenge and other data to kick off the passkey authentication.