Auth

The Auth interface is used to issue short-lived credentials to less-trusted clients without sharing your private API key. The recommended approach for most applications is to use Ably JWTs, which enable your server to mint tokens using standard JWT libraries.

Access it via the auth property of a Rest client instance.

The Auth interface also supports the creation of Ably TokenRequest objects with createTokenRequest(), and the ability to obtain Ably Tokens from Ably with requestToken(). TokenRequest objects and Ably Tokens are only recommended over JWTs when your capability list is very large, or you need to keep your capabilities confidential.

JavaScript

1

const auth = rest.auth;

Properties

The Auth interface has the following properties:

clientIdString
The client ID this client is identified as. Trusted client identifiers allow a client to be identified to other clients.

Authorize the client

auth.authorize(tokenParams?: TokenParams, authOptions?: AuthOptions): Promise<TokenDetails>

Instructs the SDK to get a new token immediately. Once fetched, the token is used for all subsequent REST requests made by this client. Also stores any tokenParams and authOptions passed in as the new defaults, to be used for all subsequent implicit or explicit token requests.

Any tokenParams and authOptions objects passed in will entirely replace (as opposed to being merged with) the current client.auth tokenParams and authOptions.

A subsidiary use case for authorize() is to preemptively trigger renewal of a token or to acquire a new token with a revised set of capabilities.

JavaScript

1

2

const tokenDetails = await rest.auth.authorize({ clientId: 'user-123' });
console.log(tokenDetails.token);

Parameters

The authorize() method takes the following parameters:

tokenParamsoptionalTokenParams
Parameters used when generating the requested token. When omitted, the default token parameters configured on the client are used.
authOptionsoptionalAuthOptions
Authentication options. When omitted, the default authentication options configured on the client are used.

Returns

Promise<TokenDetails>

Returns a promise. The promise is fulfilled with a TokenDetails object containing the new token, or rejected with an ErrorInfo object if a token could not be obtained.

Create a TokenRequest

auth.createTokenRequest(tokenParams?: TokenParams, authOptions?: AuthOptions): Promise<TokenRequest>

Creates and signs an Ably TokenRequest based on the specified (or, if none specified, the SDK's stored) tokenParams and authOptions. Note this can only be used when the API key value is available locally. Otherwise, the Ably TokenRequest must be obtained from the key owner. Use this to generate an Ably TokenRequest in order to implement an Ably Token request callback for use by other clients.

Both authOptions and tokenParams are optional. When omitted or null, the defaults specified in the ClientOptions when the SDK was instantiated, or set later via authorize(), are used. Values passed in are used instead of (rather than being merged with) the default values.

Issuing an Ably TokenRequest to clients in favor of a token avoids sharing your private API key, as explained in token authentication.

JavaScript

1

2

const tokenRequest = await rest.auth.createTokenRequest({ clientId: 'user-123' });
// Send tokenRequest JSON to the requesting client

Parameters

The createTokenRequest() method takes the following parameters:

tokenParamsoptionalTokenParams
Parameters used when generating the TokenRequest.
authOptionsoptionalAuthOptions
Authentication options.

Returns

Promise<TokenRequest>

Returns a promise. The promise is fulfilled with a TokenRequest object, or rejected with an ErrorInfo object.

keyNameString
The name of the key against which this request is made. The key name is public, whereas the key secret is private.
ttlNumber
Requested time to live for the token in milliseconds. When omitted, the default of 60 minutes is used. If the request is successful, the TTL of the returned token is less than or equal to this value, depending on application settings and the attributes of the issuing key.
timestampNumber
The timestamp of this request as milliseconds since the Unix epoch.
capabilityString
Capability of the requested token. If the request is successful, the capability of the returned token will be the intersection of this capability with the capability of the issuing key. The capability is a JSON-encoded canonicalized representation of the resource paths and associated operations.
clientIdString
The client ID to associate with the requested token. When provided, the token may only be used to perform operations on behalf of that client ID.
nonceString
A cryptographically secure random string of at least 16 characters, used to ensure the TokenRequest cannot be reused.
macString
The Message Authentication Code for this request.

Request an Ably Token

auth.requestToken(tokenParams?: TokenParams, authOptions?: AuthOptions): Promise<TokenDetails>

Calls the requestToken REST API endpoint to obtain an Ably Token according to the specified tokenParams and authOptions.

Both authOptions and tokenParams are optional. When omitted or null, the defaults specified in the ClientOptions when the SDK was instantiated, or set later via authorize(), are used. Values passed in are used instead of (rather than being merged with) the default values.

Issuing an Ably TokenRequest to clients in favor of a token avoids sharing your private API key, as explained in token authentication.

Note that since you normally use the ClientOptions callbacks to authenticate dynamically, you'll rarely need to call requestToken() explicitly, but it's available when you need finer control.

JavaScript

1

const tokenDetails = await rest.auth.requestToken();

Parameters

The requestToken() method takes the following parameters:

tokenParamsoptionalTokenParams
Parameters for the requested token.
authOptionsoptionalAuthOptions
Authentication options.

Returns

Promise<TokenDetails>

Returns a promise. The promise is fulfilled with a TokenDetails object, or rejected with an ErrorInfo object.

Revoke tokens

auth.revokeTokens(specifiers: TokenRevocationTargetSpecifier[], options?: TokenRevocationOptions): Promise<BatchResult>

Revokes the tokens specified by the provided array of TokenRevocationTargetSpecifiers. Only tokens issued by an API key that had token revocation enabled before the token was issued can be revoked.

JavaScript

1

2

3

4

5

const result = await rest.auth.revokeTokens(
  [{ type: 'clientId', value: 'user-123' }],
  { allowReauthMargin: true }
);
console.log(`${result.successCount} succeeded, ${result.failureCount} failed`);

Parameters

The revokeTokens() method takes the following parameters:

specifiersrequiredArray of TokenRevocationTargetSpecifier
An array of objects describing which tokens should be revoked.
optionsoptionalTokenRevocationOptions
Options for the revoke request.

Returns

Promise<BatchResult>

Returns a promise. The promise is fulfilled with a BatchResult whose results array contains a success or failure entry for each specifier. The promise is rejected with an ErrorInfo object if the request itself fails.

successCountNumber
The number of successful operations in the request.
failureCountNumber
The number of unsuccessful operations in the request.
resultsArray of TokenRevocationSuccessResult or TokenRevocationFailureResult
The per-specifier results, one entry per specifier.