> 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-api/smart-auth-api/authentication.md).

# Authentication

To use Smart Auth Authentications API, you will need to generate Smart Auth access token. This page shows the steps on how to create the key, generate the access tokens and the recommended ways to do it.

## **Getting your Smart Auth key**

1. Go to the [Smart Auth tokens page](https://identity.combateafraude.com/tokens);
2. If you do not have a token, generate one.
3. Retrieve the `clientId` and `clientSecret` from one of the generated tokens.

{% hint style="info" %}
You can repeat this procedure to generate accesses combining different functions and SDKs.
{% endhint %}

## **Generating your token**

### **Recommended method**

The following steps describe how you can generate a token that is valid only for a specific user. This is the recommended way to generate and distribute tokens because it limits a possible attack to a single user account.

1. At some point in your application flow, create a JWT with the structure of the example below;
   * Remember to replace the `{clientId}`, `{personId}` and `{expiresAt}` fields.
   * All of these fields are strongly recommended, but you can see which ones are required at the bottom of this page.
2. Sign the token with your `clientSecret`;
3. Send this token to your application.

**Example:**

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

```json
{
  "alg": "HS256",
  "typ": "JWT"
}
```

{% endtab %}

{% tab title="Payload" %}

```json
{
    "iss": "{clientId}", // string
    "exp": {expiresAt}, // number
    "personId": "{personId}" // string
}
```

{% endtab %}
{% endtabs %}

### **Not recommended method (only for tests)**

1. Go to [jwt.io](https://jwt.io/);
2. Keep the **Header** field, do not change;
3. Edit the payload, only the `iss` field is required;
4. Replace \*\*\*\* `your-256-bit-secret` with your `clientSecret`;
5. Click **Share JWT** to copy the generated token to the clipboard;
6. Use this token to authenticate the SDK.

### **JWT payload parameters**

| Parameter      | Required | Description                                                                            |
| -------------- | -------- | -------------------------------------------------------------------------------------- |
| **`iss`**      | Yes      | Your `clientId`                                                                        |
| **`exp`**      | No       | Expiration time (seconds since the [Unix Era](https://pt.wikipedia.org/wiki/Era_Unix)) |
| **`personId`** | No       | The CPF (Individual Taxpayer Registration Number) for which the token will be valid    |

## 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.                                 |
| **Use HTTPS for all communications**       | Prevent man-in-the-middle attacks and ensure encryption in transit.                                                |
| **Use short expiration times for JWTs**    | Minimize the window for token misuse in case of interception. Tokens should typically expire within a few minutes. |
| **Monitor token usage and behavior**       | Implement logging and monitoring to detect abnormal or suspicious activity.                                        |

### ❌ 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.                     |
| **Using long-lived tokens**                                 | Increases the window of vulnerability in case of leakage.                     |
| **Hardcoding secrets in version-controlled code**           | Secrets in code repositories can be leaked or accessed by unauthorized users. |

{% 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-api/smart-auth-api/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.
