> For the complete documentation index, see [llms.txt](https://doc.ozforensics.com/oz-knowledge/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.ozforensics.com/oz-knowledge/guides/developer-guide/api/oz-api/use-cases/authentication/how-to-issue-a-service-token.md).

# How to Issue a Service Token

A service token is a long-lived access token for automatic connections to Oz API, e.g., from Web SDK. It is issued for a service user and is valid for 5 years by default.

## How to work with service token

{% hint style="warning" %}
Since API 6.6.5, service tokens can be issued in the JWT format. The legacy format (a 128-character hexadecimal string) is deprecated: Oz API writes a deprecation warning to its logs for tokens in this format. To switch, see Migrate from the legacy format.
{% endhint %}

### Before you start

You need Oz API 6.6.5 or newer with the JWT format enabled: in the Oz API configuration, set `OZ_SERVICE_TOKENS_SUPPORT_ENABLE` and `OZ_SERVICE_TOKENS_JWT_FORMAT_ENABLED` to `true` (`OZ_SERVICE_TOKENS_JWT_FORMAT_ENABLED` is `false` by default). Restart Oz API to apply the change.

With the JWT format enabled, OPERATOR users also get service tokens in the JWT format. If your installation still uses legacy service tokens for OPERATOR users, contact Oz Support before enabling the JWT format.

{% hint style="info" %}
In SaaS, Oz manages the configuration and the blocklist. To enable the JWT format or revoke a token, contact Oz Support.
{% endhint %}

### How to issue a service token

{% stepper %}
{% step %}

#### Get an access token

Authorize as a user with the ADMIN or CLIENT ADMIN role: call `POST {{host}}/api/authorize/auth`. For details, see [Authentication](https://doc.ozforensics.com/oz-knowledge/guides/developer-guide/api/oz-api/use-cases/authentication.md).

**Example request**

```bash
curl -L 'https://{{host}}/api/authorize/auth' \
-H 'Content-Type: application/json' \
--data-raw '{
    "credentials": {
        "email": "admin_email@email.com",
        "password": "your_admin_password"
    }
}'
```

**Example response**

```json
{
    …
    "access_token": "<access_token>",
    …
}
```

{% endstep %}

{% step %}

#### Create a service user

Skip this step if you already have a user with the `CLIENT_SERVICE` or `OPERATOR` role.

Call `POST {{host}}/api/users/` with the access token from the previous step. You can also [create the user in Web UI](https://doc.ozforensics.com/oz-knowledge/guides/user-guide/oz-webui/webui-users.md#adding-a-user).

A `CLIENT_SERVICE` user must belong to a company: set its ID in `company_id`. If there’s no company yet, [create it in Web UI](https://doc.ozforensics.com/oz-knowledge/guides/user-guide/oz-webui/webui-users.md#adding-a-company) or call `POST {{host}}/api/companies/` with `{"name": "your_company_name"}` – the response contains `company_id`.

**Example request**

```bash
curl -L 'https://{{host}}/api/users/' \
-H 'X-Forensic-Access-Token: {{access_token}}' \
-H 'Content-Type: application/json' \
--data-raw '{
  "credentials": {
    "email": "<your_new_service_user@email.com>",
    "password": "<your_service_user_password>"
  },
  "profile": {
    "company_id": "{{company_id}}",
    "user_type": "CLIENT_SERVICE",
    "first_name": "john",
    "last_name": "doe",
    "middle_name": "",
    "can_start_analysis_biometry": true,
    "can_start_analysis_collection": true,
    "can_start_analysis_documents": true,
    "can_start_analysis_quality": true
  }
}'
```

**Example response**

```json
{
    "user_id": "user_id",
    "user_type": "CLIENT_SERVICE",
    …
    "is_active": true,
    …
    "is_service": true
}
```

{% endstep %}

{% step %}

#### Issue the token

Call `GET {{host}}/api/authorize/service_token/{user_id}` with the access token from the first step and `user_id` of the service user.

**Example request**

```bash
curl -L 'https://{{host}}/api/authorize/service_token/{{user_id}}' \
-H 'X-Forensic-Access-Token: {{access_token}}' \
-H 'Content-Type: application/json'
```

**Example response**

```json
{
    "token_id": "token_id",
    "user_id": "user_id",
    "access_token": "service_token",
    "expire_date": 1904659888.282587,
    "session_id": 0
}
```

The service token is returned in `access_token`. Each call returns a new token. Previously issued tokens remain valid – to revoke them, see [How to revoke a service token](#how-to-revoke-a-service-token).

{% hint style="info" %}
For Web SDK, specify this token’s value as `api_token` in the [Web Adapter configuration file](https://doc.ozforensics.com/oz-knowledge/guides/administrator-guide/configuration/web-adapter/configuration-file-settings.md).
{% endhint %}
{% endstep %}
{% endstepper %}

The token lifetime is set in `OZ_SERVICE_TOKENS_TTL` in seconds (default: `157680000`, 5 years). A new value applies only to tokens issued after the change.

JWT service tokens are signed with the Oz API `auth` key. If this key is removed, all issued service tokens stop working.

### How to migrate from the legacy format

If you use a legacy service token (a 128-character hexadecimal string):

1. Enable the JWT format as described in [Before you start](#before-you-start).
2. Issue a new service token.
3. Replace the legacy token with the new one wherever it is used, e.g., in `api_token` of the [Web Adapter configuration file](https://doc.ozforensics.com/oz-knowledge/guides/administrator-guide/configuration/web-adapter/configuration-file-settings.md).

Legacy tokens keep working after the JWT format is enabled. Their lifetime is extended with every request, so a legacy token in use doesn’t expire. Revoke it after the switch.

To revoke the legacy token, delete it via the API:

1. Call `GET {{host}}/api/users/{{user_id}}/tokens` and find the `token_id` of the service token.
2. Call `DELETE {{host}}/api/users/{{user_id}}/tokens/{{token_id}}`.

Use an ADMIN access token, or a CLIENT\_ADMIN access token for users of your company.

Blocking by `jti` doesn’t work for legacy tokens, and blocking the user also blocks the user’s new token.

### How to revoke a service token

{% hint style="warning" %}
Reissuing a JWT token, removing analysis permissions, deactivating or deleting the user, and deleting the company don’t revoke issued service tokens automatically. To revoke a token, add it to the blocklist.
{% endhint %}

| To revoke            | Add to the blocklist                  | Oz API version  |
| -------------------- | ------------------------------------- | --------------- |
| A single token       | The token’s `jti` claim to `jti_list` | 6.6.5 and newer |
| All tokens of a user | The user ID to `subject_list`         | 6.7.0 and newer |

1. Add the entry to the `access_service_tokens_config.toml` file. The path to the file is set in `OZ_ACCESS_SERVICE_TOKENS_CONFIG_PATH`. If Oz API is installed with Helm, set the blocklist in `Params.gunicorn.accessServiceTokensBlocklist` (`jti`, `subjects`) instead. The chart creates the file and sets the path.\
   When filling in the file:

   * Values are case-sensitive and must match exactly.
   * Take `jti` from the token payload – the second, base64url-encoded part of the JWT. Decode the token locally, not with public online decoders.
   * In `subject_list`, keep `iss = "AUTH"` and set `sub` to the user ID.
   * Keep the square brackets, as in the example.
   * You can omit either list.

   ```toml
   title = "Access Tokens Config"

   [blocklist]

   # block specific tokens
   jti_list = [
     "<jti>",
   ]

   # block all tokens of a user
   subject_list = [
     { iss = "AUTH", sub = "<user_id>" }
   ]
   ```

   If the file has invalid TOML syntax or an invalid record in `subject_list` (empty values, excessive fields, etc.), Oz API doesn’t start. In Oz API 6.9.0 and newer, Oz API also doesn't start if `[blocklist]` isn't a table or `jti_list` isn't a list of strings.

{% hint style="info" %}
If the path is set, but the file by the path is missing, API starts without blocklist and logs `Access tokens config not found: <path>` to stderr.
{% endhint %}

2. Restart Oz API. Changes to the file apply only after the restart.
3. Check that a request with the revoked token returns `401 Unauthorized` with `error_code` = `7`.

Oz API logs rejected requests at the `ERROR` level:

* blocked token: `Authentication failed for user_id=<user_id>, jti=<jti>`
* blocked user: `Authentication failed for iss=<iss>, sub=<sub>, jti=<jti>, reason=user_blocklist`

<details>

<summary>If the token isn’t blocked</summary>

* Check the path in `OZ_ACCESS_SERVICE_TOKENS_CONFIG_PATH` and that the file is mounted to the container.
* In Oz API 6.8.0 and newer, at startup, Oz API logs the number of loaded blocklist entries. Check this line to make sure the file is loaded.
* In Oz API 6.9.0 and newer, this line also shows the path to the file. If `OZ_ACCESS_SERVICE_TOKENS_CONFIG_PATH` isn't set, Oz API logs that the blocklist isn't configured.

</details>

{% hint style="info" %}
Blocking doesn’t deactivate the account. If account credentials are compromised, deactivate the account, create a new one, and add the old account’s user ID to `subject_list`. Don’t delete the old account: this keeps its earlier orders attributed to it.

To deactivate the account, clear the **User is active** checkbox in [Web UI](https://doc.ozforensics.com/oz-knowledge/guides/user-guide/oz-webui/webui-users.md#editing-a-user) or call `PATCH {{host}}/api/users/{{user_id}}` with `is_active` set to `false` in the request body:

```bash
curl -L -X PATCH 'https://{{host}}/api/users/{{user_id}}' \
-H 'X-Forensic-Access-Token: <ADMIN access token>' \
-H 'Content-Type: application/json' \
-d '{"is_active": false}'
```

{% endhint %}

<details>

<summary>Legacy service token (Oz API below 6.6.5)</summary>

In Oz API versions below 6.6.5, service tokens are issued only in the legacy format (a 128-character hexadecimal string).

You need a service user: in Oz API 6, a user with the `CLIENT_SERVICE` role; in Oz API 5, a user with `is_service` set to `true`.

1. Authorize using your ADMIN account: `{{host}}/api/authorize/auth`.

   ```bash
   curl -L 'https://{{host}}/api/authorize/auth' \
   -H 'Content-Type: application/json' \
   --data-raw '{
       "credentials": {
           "email": "admin_email@email.com",
           "password": "your_admin_password"
       }
   }'
   ```

   The response contains `access_token`.
2. With this access token, call:

   * Oz API 6: `{{host}}/api/authorize/service_token/{user_id}` with `user_id` of the service user.
   * Oz API 5: `{{host}}/api/authorize/service_token`.

   ```bash
   curl -L 'https://{{host}}/api/authorize/service_token/{{user_id}}' \
   -H 'X-Forensic-Access-Token: {{access_token}}' \
   -H 'Content-Type: application/json'
   ```

   ```json
   {
       "token_id": "token_id",
       "user_id": "user_id",
       "access_token": "service_token",
       "expire_date": 1904659888.282587,
       "session_id": 0
   }
   ```

   The service token is returned in `access_token`. For Web SDK, specify its value as `api_token` in the [Web Adapter configuration file](https://doc.ozforensics.com/oz-knowledge/guides/administrator-guide/configuration/web-adapter/configuration-file-settings.md).

The token’s lifetime is extended with every request, so a token in use doesn’t expire.

To replace the token, call `PATCH {{host}}/api/authorize/service_token/{user_id}` (Oz API 5: `PATCH {{host}}/api/authorize/service_token`). The previous token stops working.

In Oz API 6, to revoke the token, delete it via the API:

1. Call `GET {{host}}/api/users/{{user_id}}/tokens` and find the `token_id` of the service token.
2. Call `DELETE {{host}}/api/users/{{user_id}}/tokens/{{token_id}}`.

Use an ADMIN access token, or a CLIENT\_ADMIN access token for users of your company.

</details>


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://doc.ozforensics.com/oz-knowledge/guides/developer-guide/api/oz-api/use-cases/authentication/how-to-issue-a-service-token.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
