Authentication
Ably clients can authenticate with Ably in one of two ways. They can use Basic Authentication or Token Authentication. Basic Authentication makes use of the customer’s API key) to connect with Ably. Token Authentication requires a server to provide an Ably Token, an Ably TokenRequest
, an Ably JWT, or an External JWT with an embedded Ably-compatible token to the client. Token Authentication, in most cases, is the recommended strategy due to it providing robust access control and stringent security measures.
Understanding the Ably authentication schemes
This page of documentation is intended to describe the REST Authentication API and is not intended to explain in depth how Ably’s authentication works. If you are new to Ably and/or the authentication schemes, we strongly recommend that you get acquainted with the following first:
- Getting started with Ably’s authentication
- Selecting the right authentication scheme
- Basic Authentication explained
- Token Authentication explained
Tutorials
If you’re the kind of person who prefers to dive into code, we have client-server authentication tutorials.
Basic Authentication
Basic Authentication uses one of the API keys configured via the application dashboard as the authentication token. Basic Authentication is the simplest method to use but has some important limitations described in detail in the Basic Authentication documentation.
Here is a straightforward example of using Basic Authentication when instancing an Ably REST client library.
var rest = new Ably.Rest({ key: 'xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA' });
var rest = new Ably.Rest({ key: 'xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA' });
rest = Ably::Rest.new(key: 'xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA')
rest = AblyRest(key='xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA')
$rest = new Ably\AblyRest(array('key' => 'xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA'));
ClientOptions options = new ClientOptions();
options.key = "xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA";
AblyRest rest = new AblyRest(options);
AblyRest rest = new AblyRest("xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA");
let rest = ARTRest(key: "xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA")
ARTRest *rest = [[ARTRest alloc] initWithKey:@"xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA"];
Token Authentication
Look at the general authentication documentation for more in-depth information on Token Authentication.
Token Authentication uses an Ably-compatible token to authenticate with Ably. This can be an Ably Token obtained via the REST API requestToken
RequestToken
request_token
endpoint, an Ably JWT signed by your API key, or an External JWT object with an embedded Ably-compatible token. The best way to obtain these tokens is through the use of the Auth object.
Tokens are authentication credentials that are short-lived, and therefore they may more readily be distributed to clients where there is a risk of compromise. Tokens may also be issued with a particular scope – such as a limited set of access rights or capabilities or being limited to use by a specific clientId
ClientId
identity – and therefore token-based authentication provides the flexibility to implement access and identity control policies in the application. See the Token Authentication documentation for more details.
Below is a rather contrived yet straightforward example that instances a Realtime library using Token Authentication with a means to reissue tokens as required. Typically, in a browser environment, the authUrl
AuthUrl
provided would be a relative URL to a local endpoint that is used to issue tokens to trusted clients. Client requests can, for example, be trusted based on session cookies. For non-browser clients, an authentication callback is preferred thus relying on your application to communicate securely with your own servers to obtain a token.
var rest = new Ably.Rest({ authUrl: 'https://my.website/auth' });
var rest = new Ably.Rest({ authUrl: 'https://my.website/auth' });
rest = Ably::Rest.new(auth_url: 'https://my.website/auth')
rest = AblyRest(auth_url='https://my.website/auth')
$rest = new Ably\AblyRest(array('authUrl' => 'https://my.website/auth'));
ClientOptions options = new ClientOptions();
options.authUrl = "https://my.website/auth";
AblyRest rest = new AblyRest(options);
AblyRest rest = new AblyRest(new ClientOptions { AuthUrl = new Uri("https://my.website/auth") });
ARTClientOptions *options = [[ARTClientOptions alloc] init];
options.authUrl = [NSURL URLWithString:@"https://my.website/auth"];
ARTRest *rest = [[ARTRest alloc] initWithOptions:options];
let options = ARTClientOptions()
options.authUrl = NSURL(string: "https://my.website/auth")
let rest = ARTRest(options: options)
Ably supports both Ably Tokens and Ably JWTs for authentication.
Selecting an authentication mechanism
When deciding on which authentication method you will be using, it is recommended to bear in mind the principle of least privilege: a client should ideally only possess the credentials and rights that it needs to accomplish what it wants; this way, if the credentials are compromised, the rights that can be abused by an attacker are minimized.
The table below should be used as a rough guide as to what you should consider when choosing your authentication method. Many applications will most naturally use a mixed strategy: one or more trusted application servers will use basic authentication to access the service and issue tokens over HTTPS, whereas remote browsers and devices will use individually issued tokens:
Identified clients
When a client is authenticated and connected to Ably, they are considered to be an authenticated client. However, whilst an authenticated client has a verifiable means to authenticate with Ably, they do not necessarily have an identity. When a client is assigned a trusted identity (i.e. a client_id
ClientId
clientId
), then they are considered to be an identified client and for all operations they perform with the Ably service, their client_id
ClientId
clientId
field will be automatically populated and can be trusted by other clients.
We encourage customers to always issue tokens to clients so that they authenticate using the short-lived token and do not have access to a customer’s private API keys. Since the customer can then control the client_id
ClientId
clientId
that may be used by any of its clients, all other clients can rely on the validity of the client_id
ClientId
clientId
in published messages and of members present in presence channels.
The following example demonstrates how to issue an Ably TokenRequest
with an explicit client_id
ClientId
clientId
that, when used by a client, will then be considered an identified client.
var rest = new Ably.Rest({ key: 'xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA' });
rest.auth.createTokenRequest({ clientId: 'Bob' }, function(err, tokenRequest) {
/* ... issue the TokenRequest to a client ... */
})
var rest = new Ably.Rest({ key: 'xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA' });
rest.auth.createTokenRequest({ clientId: 'Bob' }, function(err, tokenRequest) {
/* ... issue the TokenRequest to a client ... */
})
rest = Ably::Rest.new(key: 'xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA')
token_request = rest.auth.create_token_request(client_id: 'Bob')
# ... issue the TokenRequest to a client ...
rest = AblyRest(key='xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA')
token_request = rest.auth.create_token_request(client_id='Bob')
# ... issue the TokenRequest to a client ...
$rest = new Ably\AblyRest(array('key' => 'xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA'));
$tokenRequest = $rest->auth->createTokenRequest(array('clientId' => 'Bob'));
# ... issue the TokenRequest to a client ...
ClientOptions options = new ClientOptions();
options.key = "xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA";
AblyRest rest = new AblyRest(options);
TokenParams tokenParams = new TokenParams();
tokenParams.clientId = "Bob";
TokenRequest tokenRequest;
tokenRequest = rest.auth.createTokenRequest(tokenParams, null);
/* ... issue the TokenRequest to a client ... */
AblyRest rest = new AblyRest(new ClientOptions {Key = "xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA"});
TokenParams tokenParams = new TokenParams {ClientId = "Bob"};
string tokenRequest = await rest.Auth.CreateTokenRequestAsync(tokenParams);
// ... issue the TokenRequest to a client ...
ARTRest *rest = [[ARTRest alloc] initWithKey:@"xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA"];
ARTTokenParams *tokenParams = [[ARTTokenParams alloc] initWithClientId:@"Bob"];
[rest.auth createTokenRequest:tokenParams options:nil
callback:^(ARTTokenRequest *tokenRequest, NSError *error) {
// ... issue the TokenRequest to a client ...
}];
let rest = ARTRest(key: "xVLyHw.VGTaOg:EzJGhlYvkv2dxEDCOlT2MjgAExwzDyl6FJC4-COZaSA")
let tokenParams = ARTTokenParams(clientId: "Bob")
rest.auth.createTokenRequest(tokenParams, options: nil) { tokenRequest, error in
// ... issue the TokenRequest to a client ...
}
API Reference
View the Authentication API Reference.