Authenticate web users

|

View as Markdown

Live Chat authenticates web users with a personalization token. This token is a JSON Web Token (JWT)(opens in new tab) signed with the secret key of your widget. As an integrator, you generate the personalization token and provide it to the Live Chat widget.

NOTE

If the token is correctly signed, Live Chat fully trusts that its data is correct and that you provided it. Infobip does not take responsibility for incorrect data. For example, incorrect data might let users view the chat history of a different user.

To authenticate web users, complete the following steps:

  1. Get the secret key of your widget.
  2. Get the unique identifier of the user.
  3. Generate the personalization token.
  4. Submit the token to the widget.

Get the secret key [#get-the-secret-key]

Each Live Chat widget can have one or more secret keys. Use a secret key to sign personalization tokens.

To get a secret key:

  1. Go to the configuration page of your widget. You can generate keys only after you create the widget.
  2. Expand the Installation and security section.
  3. In Security → Secret keys, select Generate new key.
  4. Select Save widget. The key appears in the list with its key ID only after you save the widget. Secret keys in the Security part of the Installation and security section with a key ID and the Generate new key button
  5. Select Copy key next to the key. The key is copied in JSON format, with the id and key fields.

A secret key has the following structure:

json
1{
2 "id": "43f7a6d6-f064-4a6b-8890-fff70b1363ca",
3 "key": "0wQGQwukZLkJNShlHS0Nm3SCiIwrWNfm7NEPOhukJhM="
4}

The structure contains the following fields:

  • id: The unique identifier for your secret key. The JWT includes this value in its ski field.
  • key: The secret value encoded in Base64, used to sign the JWT. Treat this key as confidential and handle it with the same care as a password.

To rotate a secret key:

  1. Generate a new key and save the widget, as described in the previous steps.
  2. Update your server to sign the tokens with the new key.
  3. Delete the old key with the delete icon next to it. After you delete the key, tokens signed with it no longer authenticate users.


Get the user identifier [#get-the-user-identifier]

To distinguish each user, supply a unique identifier. Live Chat supports the following types of identifiers:

  • Email: The email address of the user.
  • MSISDN: The phone number of the user, in MSISDN format.
  • External ID: Any string ID from your system that is unique per user.
NOTE

If the profile of a user contains multiple identifiers, you can use any of them for authentication.



Generate the personalization token [#generate-the-personalization-token]

To generate a personalization token, implement a JWT generator on your server.

IMPORTANT

Generate and sign the token on your server. Never put the secret key in your website or app code.

NOTE

If you are not familiar with JSON Web Tokens (JWT), review their structure and creation process before you continue.

The generated token must meet the following requirements:

  • Follow the structure defined in JWT payload structure.
  • Be signed with the secret key of your widget.
  • Stay as the signed JWT string. Do not encode the token again, for example, in Base64.

After you generate and sign the token, supply it to the front end where Live Chat is embedded.

A token authenticates a user only once. Generate a separate token for each authentication attempt. Live Chat maintains the authenticated session until you explicitly de-authenticate the user.


JWT payload structure [#jwt-payload-structure]

The token contains the following header and payload fields:

FieldRequiredDescription
algYes(Header value) Signing algorithm. Supported value: HS256
typYes(Header value) Type of the token. Supported value: JWT. Live Chat does not check this value.
iatYesIssued at: Unix timestamp(opens in new tab) in seconds when the token is created.
issYesIssuer: the ID of your widget.
jtiYesJWT ID: must be unique for each token. Any string, max 50 characters.
skiYesSecret key ID: the ID (not the value) of the secret key that you use to sign the token.
stpYesSubject type: type of identifier in sub.
Allowed values:
• email: standardized email format.
• msisdn: phone number in MSISDN format.
• externalPersonId: any string user identifier unique to your system, max 100 characters.
subYesSubject: value of the unique identifier, matching the type in stp.
expNoExpiration: Unix timestamp(opens in new tab) in seconds when the token expires. If not set, defaults to iat + 15 seconds.
Avoid values less than iat + 15 seconds.
sidNoSession ID: your unique user session identifier, used for the Invalidate session API. Max 50 characters.
Must be unique for each user session in the widget. If another user sends the same sid, authentication fails with error 4013. If the same user sends it again, the session resumes.

The time values in the token follow these rules:

  • Enter iat and exp in seconds. Live Chat rejects values in milliseconds.
  • exp must not be earlier than iat.
  • Live Chat accepts a clock difference of up to 1 second between your server and Live Chat.
  • Keep the token lifetime short. A token authenticates a user only once.
IMPORTANT

Anyone with access to a JWT can read its contents. Do not include confidential information, such as a password, in the token payload.


Token examples [#token-examples]

The following examples show a decoded token and the same token in its encoded form:

LEARN MORE
Visit Token generator examples for JWT generator code in Node.js, Java, Kotlin, Swift, and PHP.


Submit the token [#submit-the-token]

After you generate the token, use the Auth command of the Live Chat SDK to submit it to your widget. Submit the signed JWT string as it is.

Authentication is asynchronous. Check the result in the callback of the Auth command. Authentication fails in the following cases, among others:

  • ALREADY_AUTHENTICATED: A user is already authenticated in the widget. De-authenticate the user first.
  • 2008 (JWT_ID_ALREADY_USED): The token uses a jti that was already used for the widget. Generate a new token.
  • 4013: Another user already uses the same sid in the widget.


Related pages

User types
Understand leads, customers, and how to de-authenticate users.

Token generator examples
Copy JWT generator code for Node.js, Java, Kotlin, Swift, and PHP.

JavaScript SDK in Live Chat
Review the Auth command and other SDK commands for the web widget.

Authenticate mobile users
Authenticate in-app chat users with the Mobile SDK.


Was this page helpful?