> For the complete documentation index, see [llms.txt](https://docs.caf.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.caf.io/caf-sdk/authentication.md).

# Authentication

This guide explains how to securely and efficiently authenticate Caf SDKs.

## Integration overview

1. Obtain access credentials (**Mobile Key**)
2. Generate the **Authentication Token** (JWT signed with your `client-secret`)
3. Exchange this token for a **Mobile Token** (Session Token)
4. Use the **Mobile Token** when initializing the SDK

{% hint style="warning" %}
For clients using Caf SDKs who subsequently create transactions to perform additional validations, you will have an additional step: link the **Mobile Token** to a transaction, sending it as a `referenceToken` in the creation request to allow complete tracking of the user journey. Check out [Transaction linking](https://docs.caf.io/caf-api/core-api/transaction-linking) for more details.
{% endhint %}

## 1. Obtain access credentials (Mobile Key)

Mobile Keys are used to sign and authenticate requests.

**How to obtain:**

1. Go to the [Trust Platform](https://trust.caf.io)
2. Navigate to **Settings** → **API configurations (Mobile Keys "tab")**
3. Copy an existing key or create a new one, specifying the products and key name.

## 2. Generate Authentication Token (JWT)

This JWT is generated on your server and signed with the Mobile Key's `client-secret`.

#### Payload fields:

| Field | Required | Description                        |
| ----- | -------- | ---------------------------------- |
| `iss` | Yes      | Your `client-id` (provided by CAF) |
| `exp` | No       | Token expiration (Unix timestamp)  |

#### Example payload:

```json
{
  "iss": "your-client-id",
  "exp": 1728000000
}
```

{% hint style="warning" %}
This JWT must be signed with the Mobile Key's `client-secret`.
{% endhint %}

## 3. Exchange Authentication Token for Mobile Token

After generating the JWT, you must exchange it for the **Mobile Token**, which will be used to initialize the SDK. For each SDK session in your application, generate a Mobile Token using the previously created Authentication Token.

{% openapi src="/files/QsW4Re1Y8Ot8BRxWUTSd" path="/session-tokens" method="get" %}
[bff-openapi.yaml](https://728704278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvnCLbngSdfkIVoF3ziNX%2Fuploads%2Fgit-blob-ea7014f435de65ce780257618d1bbc5febe6ce52%2Fbff-openapi.yaml?alt=media)
{% endopenapi %}

## 4. Use the Mobile Token in the SDK

The `mobile-token` (Session Token) is used during **SDK initialization** and ensures that each session is securely authenticated and traceable.

Below is how to integrate it on different platforms:

### 4.1 Android

{% tabs %}
{% tab title="CafSDK" %}

```swift
val sdkBuilder = CafSdkProvider.Builder(
    mobileToken = "mobile-token",
).build()
```

Check out [Android SDK Integration](/caf-sdk/android/getting-started-with-the-sdk-1.md) for more details.
{% endtab %}

{% tab title="CerttaSDK" %}

```kotlin
Certta.instance.updateMobileToken("new-jwt-token")
```

Check out [Android Certta SDK Installation Guide](/caf-sdk/android/installation-guide.md) for more details.
{% endtab %}
{% endtabs %}

### 4.2 iOS

{% tabs %}
{% tab title="CafSDK" %}

```swift
let builder = CafSDKProvider.Builder(
    self,
    mobileToken: "yourToken",
)
```

Check out [iOS SDK Integration](https://docs.caf.io/caf-sdk/ios/getting-started-with-the-sdk#builder-initialization) for more details.
{% endtab %}

{% tab title="CerttaSDK" %}

```kotlin
Certta.shared.updateMobileToken("new-jwt-token")
```

Check out [iOS SDK Integration](https://docs.caf.io/caf-sdk/ios/getting-started-with-the-sdk#builder-initialization) for more details.
{% endtab %}
{% endtabs %}

### 4.3 Web

{% tabs %}
{% tab title="Web" %}

```js
const builder = await CafSdkProvider.initializeSdk(
  "mobile-token",
  ...
);
```

Check out [Web SDK Integration](https://docs.caf.io/caf-sdk/web-javascript/getting-started/document-detector/documentdetector#xtquz7g7g6lm) for more details.
{% endtab %}
{% endtabs %}

## Transaction linking

To ensure the security and complete traceability of the user journey, each transaction created via the API must be linked to the original session generated by the SDK. This link is made by including the Mobile Token (session token) in your calls, creating a unified audit trail.

{% hint style="info" %}
See [Transaction linking](https://docs.caf.io/caf-api/core-api/transaction-linking) for more details.
{% endhint %}

## Best practices for token-based authentication

To ensure a secure and reliable integration when using token-based authentication with our service, follow these recommended practices and avoid common pitfalls.

### ✅ Recommended practices

| Practice                                                                           | Description                                                                                                                   |
| ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| **Generate and sign tokens on the server**                                         | Always issue JWTs from your trusted backend system to avoid exposing signing keys.                                            |
| **Exchange the JWT at the `/session-tokens` endpoint before initializing the SDK** | Use the `/session-tokens` endpoint to exchange the signed JWT and obtain a short-lived session token before starting the SDK. |
| **Use short expiration times for JWTs**                                            | Limit token lifetime to reduce the impact of potential exposure or misuse.                                                    |
| **Validate SDK responses on the backend using the `client-secret`**                | Perform server-side validation of SDK responses to ensure authenticity and integrity of the data.                             |

### ❌ Unsafe practices

| Practice                                                    | Risk                                                                           |
| ----------------------------------------------------------- | ------------------------------------------------------------------------------ |
| **Generating tokens on the frontend**                       | Exposes your signing keys and compromises the entire authentication system.    |
| **Storing signing keys or secrets in frontend/mobile apps** | Secrets in client-side code can be extracted and misused.                      |
| **Hardcoding secrets in version-controlled code**           | Secrets in code repositories can be leaked or accessed by unauthorized users.  |
| **Reusing long-lived tokens across multiple sessions**      | Increases the risk and blast radius in case a token is exposed or compromised. |

{% hint style="warning" %}
**Important Notice on Authentication and Integrations**

All integrations with Caf APIs must be implemented exclusively via backend/server-side. Client-side or frontend-based integrations may be blocked and significantly increase the risk of authentication key exposure.

Caf continuously monitors the usage and exposure of authentication keys. Any key identified as exposed, compromised, or used in non-compliant implementations will be immediately revoked, which may result in the interruption of the associated operation, without prior notice.

It is the integrator’s responsibility to ensure compliance with the recommended authentication patterns and security best practices described in this documentation.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.caf.io/caf-sdk/authentication.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
