To authenticate with CCAI Platform, you must implement a token endpoint to generate a
JSON web token as the response.

## Get company secret

> [!WARNING]
> **Warning:** It is imperative to store your `COMPANY_SECRET` securely on your server so the application can validate your identity. Without this precaution in place, it is possible for malicious entities to act on your behalf.

1. Sign into the Contact Center AI Platform portal using an account with administrator
   permissions.

2. Go to **Settings \> Developer Settings**.

3. Copy the Company Secret as `COMPANY_SECRET`.

## Token endpoint

The token endpoint must return a JWT containing the current user's
information. The response is similar to the following:

    {"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.n...."}

The token is created with:

1. `alg` of `HS256`.

2. `payload` must contain user identifier, name and email. Even if the user is
   an anonymous user and has not yet authenticated, the identifier is required
   in order for the SDK to work as expected.

3. Sign the signature with `COMPANY_SECRET`

    header = {"alg": "HS256"}
    payload = {
      "iss": "{YOUR_COMPANY_NAME}",
      "iat": $unix_timestamp_now,
      "exp": $unix_timestamp_now + 3600,
      "identifier": "{CURRENT_USER_ID}",
      "name": "{CURRENT_USER_NAME}",
      "email": "{CURRENT_USER_EMAIL}",
    }

    token = jwt_encode(header, payload, COMPANY_SECRET)

## Examples

Here are some examples, we are using `/auth/token` as the endpoint.

### Express (Node.js)

    const express = require('express')
    const jwt = require('jsonwebtoken')

    const COMPANY_NAME = 'AMCE'
    const COMPANY_SECRET = '__read_from_config__'

    const app = express()
    app.use(express.json())
    app.use(your_session_middleware)

    app.get('/auth/token', function (req, res) {
      const now = parseInt(Date.now() / 1000, 10)
      const payload = {
        'iss': COMPANY_NAME,
        'iat': now,
        'exp': now + 3600,
      }
      if (req.user) {
        payload.identifier = req.user.id,
        payload.name = req.user.name
        payload.email = req.user.email
      }
      const token = jwt.sign(payload, COMPANY_SECRET, { algorithm: 'HS256' })
      res.json({ token })
    })

### Flask (Python)

    import time
    from flask import Flask, request
    from joserfc import jwt, jwk

    app = Flask(__name__)

    COMPANY_NAME = 'AMCE'
    COMPANY_SECRET = '__read_from_config__'
    secret_key = jwk.OctKey.import_key(COMPANY_SECRET)

    @app.route('/auth/token')
    def auth_token():
        now = int(time.time())
        payload = {
          'iss': COMPANY_NAME,
          'iat': now,
          'exp': now + 3600,
        }
        if (request.user) {
          payload.identifier = request.user.id,
          payload.name = request.user.name
          payload.email = request.user.email
        }
        token = jwt.encode({'alg': 'HS256'}, payload, secret_key)
        return {'token': token}