Per l'autenticazione con CCAI Platform, devi implementare un endpoint token per generare un token web JSON come risposta.
Recupero del secret aziendale
Accedi al portale Contact Center AI Platform utilizzando un account con autorizzazioni di amministratore.
Vai a Impostazioni > Impostazioni sviluppatore.
Copia il secret aziendale come
COMPANY_SECRET.
Endpoint token
L'endpoint token deve restituire un JWT contenente le informazioni dell'utente corrente. La risposta è simile alla seguente:
{"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.n...."}
Il token viene creato con:
algdiHS256.payloaddeve contenere l'identificatore, il nome e l'indirizzo email dell'utente. Anche se l'utente è anonimo e non ha ancora eseguito l'autenticazione, l'identificatore è obbligatorio per il corretto funzionamento dell'SDK.Firma la firma con
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)
Esempi
Ecco alcuni esempi. Stiamo utilizzando /auth/token come 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}