Skip to content
Docs
WebsiteOpen app

Identity verification

Sign your users' ids with HMAC-SHA256 so the chat widget can trust who they are.

When a signed-in user opens the widget, you can tell opendesk who they are. Without verification, anyone could claim to be any user. With it, opendesk accepts the identity only when it comes with a signature that only your server can produce.

Each chat widget has its own secret. Sign with the secret of the widget that runs on that site: a session is verified only by the widget whose secret signed it.

The signature is:

user_hash = hex( HMAC-SHA256( key = widget_secret, message = user_id ) )

A session that sends a valid user_hash for its user_id is marked verified, and its conversations attach to the contact with that external id.

  1. Get the widget’s secret. Open Settings, Brands and widgets, the brand, then the widget on its Chat widgets tab. Under Identify signed-in users, click Rotate secret. The new secret is shown once, so store it in your server’s secrets. A new widget can also show its first secret right when you create it.

    Rotating needs the admin role, two-factor authentication and a sign-in from the last 12 hours. The old secret stops working at once: every hash signed with it is refused until your server uses the new one. Chats already open keep working.

  2. Compute the hash on your server for the signed-in user, with one of the examples below.

  3. Pass user_id and user_hash to the widget, together with the optional email and name, as in the identify snippet on the widget’s page. The widget sends them when it starts a session (POST /v1/widget/session).

import { createHmac } from 'node:crypto';
export function userHash(userId) {
return createHmac('sha256', process.env.OPENDESK_WIDGET_SECRET)
.update(String(userId))
.digest('hex');
}

With a verified identity you can also send the account the user belongs to and a few attributes, so agents see who a chat comes from without asking:

opendesk('identify', {
user_id: '42',
user_hash: '<hmac from your server>',
company: { id: 'acct-9' },
fields: { widget_plan: 'pro', widget_seats: 12 },
});
  • company.id is your own id for the account. opendesk links the contact to the company whose external id it is. Set that id on the company first, with external_id in the companies API. The widget never creates a company or changes its name or domain, since user_hash proves only who the user is, and an id no company has is ignored.
  • fields set the contact’s custom fields by key. Only fields whose key starts with widget_ are set: create widget_plan as a contact field first, and anything else, or a value that does not fit its field, is ignored. The values come from your page, so keep to this prefix only the fields you want the widget to fill, such as a plan name or a seat count.
  • When something changes without a page load, call opendesk('update', { … }) with the same user_id and user_hash. It updates the contact without starting a new chat session.
  • Visitors without a valid user_hash are anonymous: company and fields are ignored.

For the secret test-secret and user id 42, every example above must return the same 64-character lowercase hex string. Compare your output across languages, or with:

Terminal window
printf '42' | openssl dgst -sha256 -hmac 'test-secret'

All the widgets of a workspace share one set of user ids: a verified user_id is the same contact whichever widget verified it. So:

  • Use the same user ids on two sites only when they are the same people, such as one account system behind both.
  • Keep every widget’s secret as safe as the others: each one vouches for the same ids.
  • A visitor who was verified on one widget is verified on another only when that widget’s own secret signed the same user_id. Otherwise they start there as a new, anonymous visitor.
  • Use a stable id that never changes for the user, such as your database primary key, not an email address.
  • Send the id as a string. The hash is computed over its exact bytes.
  • When a user signs out of your product, call opendesk("shutdown"). It ends the widget session on the server too, so the next visitor on the same browser does not see their conversations.
  • A verified session is never picked up again without a user_hash: a page that loads the widget without one starts an anonymous visitor. A verified session also ends 30 days after it started, and the next page that verifies the user starts a new one without asking them anything.

Edit this page on GitHub