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.
-
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.
-
Compute the hash on your server for the signed-in user, with one of the examples below.
-
Pass
user_idanduser_hashto the widget, together with the optionalemailandname, as in the identify snippet on the widget’s page. The widget sends them when it starts a session (POST /v1/widget/session).
Examples
Section titled “Examples”import { createHmac } from 'node:crypto';
export function userHash(userId) { return createHmac('sha256', process.env.OPENDESK_WIDGET_SECRET) .update(String(userId)) .digest('hex');}import hashlibimport hmacimport os
def user_hash(user_id: str) -> str: secret = os.environ["OPENDESK_WIDGET_SECRET"].encode() return hmac.new(secret, str(user_id).encode(), hashlib.sha256).hexdigest()package widget
import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "os")
func UserHash(userID string) string { mac := hmac.New(sha256.New, []byte(os.Getenv("OPENDESK_WIDGET_SECRET"))) mac.Write([]byte(userID)) return hex.EncodeToString(mac.Sum(nil))}<?phpfunction user_hash(string $userId): string { return hash_hmac('sha256', $userId, getenv('OPENDESK_WIDGET_SECRET'));}require "openssl"
def user_hash(user_id) OpenSSL::HMAC.hexdigest("SHA256", ENV.fetch("OPENDESK_WIDGET_SECRET"), user_id.to_s)endSend the user’s company and attributes
Section titled “Send the user’s company and attributes”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.idis 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, withexternal_idin the companies API. The widget never creates a company or changes its name or domain, sinceuser_hashproves only who the user is, and an id no company has is ignored.fieldsset the contact’s custom fields by key. Only fields whose key starts withwidget_are set: createwidget_planas 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 sameuser_idanduser_hash. It updates the contact without starting a new chat session. - Visitors without a valid
user_hashare anonymous:companyandfieldsare ignored.
Check your hash
Section titled “Check your hash”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:
printf '42' | openssl dgst -sha256 -hmac 'test-secret'Several widgets
Section titled “Several widgets”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.