Component authentication
Surge aims to be easy to use but also to provide for evolving security needs. To that end, embedded UI components can be authenticated in two ways: a long-lived token shared by the various users in an account, or a short-lived per-user token that will need to be rotated frequently.
Which token type should I use?
Publishable account tokens are much easier to start with, but signed user tokens offer stronger security.
Publishable tokens
Our sample iframe snippets here in the docs and in HQ use a publishable token. This token belongs to the account and can be used to authenticate any user in that account. Component URLs using this authentication method must have both “token” and “user_id” query parameters. An inbox URL, for example, will look like this:
Signed tokens
When you’re ready to invest in added security for your embedded UI components, signed tokens belong to a specific user and are only valid for a limited duration.
There are two ways to generate these tokens: you can sign your own, or you can have Surge sign them for you. Having Surge sign your tokens is less involved, but it requires an API request, which will not be as performant as signing tokens on your own server. A guide to self-signed tokens is forthcoming, but the endpoint for Surge-signed tokens is POST /users/:user_id/tokens.
Component URLs using signed tokens need only the “token” query parameter. Since the token belongs to a user, a “user_id” query parameter would serve no purpose. The inbox URL from above, using instead a signed token, will look like this:
Rotating signed tokens
Because signed tokens are short-lived they may expire while users are still interacting with the embedded UI components authenticated with the expiring tokens. Connections will remain authenticated after their token has expired, but any reconnects will trigger re-authentication. We recommend rotating tokens just before they expire to ensure your components will continue to work.
Token expiring warning
When an embedded UI component is authenticated with a token that is about to expire, it will emit an event warning of the pending expiration. This event will have the the type “token_expiring”:
Updating a component’s token
Re-rendering the iframe with a new token in the URL could cause the component to lose necessary state, so embedded components also support an “update_token” message back into the iframe.
Example
Here’s an example snippet of some JavaScript in your app for rotating tokens before they expire. In
this example refreshSurgeToken
is a made-up function that makes a request to your server for a new
token.