Package io.ably.lib.rest
Class Auth
java.lang.Object
io.ably.lib.rest.Auth
Token-generation and authentication operations for the Ably API.
See the Ably Authentication documentation for details of the
authentication methods available.
Creates Ably
Auth.TokenRequest
objects and obtains Ably Tokens from Ably to subsequently issue to less trusted clients.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Authentication methodsstatic class
Passes authentication-specific properties in authentication requests to Ably.static interface
An interface providing update result for onAuthUpdatedstatic interface
An interface providing completion callbackk for renewAuthstatic interface
An interface implemented by a callback that provides either tokens, or signed token requests, in response to a request with given token params.static class
Contains an Ably Token and its associated metadata.static class
Defines the properties of an Ably Token.static class
Contains the properties of a request for a token to Ably. -
Field Summary
Modifier and TypeFieldDescriptionA client ID, used for identifying this client when publishing messages or for presence purposes.static final String
-
Method Summary
Modifier and TypeMethodDescriptionvoid
assertAuthorizationHeader
(boolean forceRenew) Get the Authorization header, forcing the creation of a new token if requestedauthorise
(Auth.TokenParams params, Auth.AuthOptions options) Deprecated.authorize
(Auth.TokenParams params, Auth.AuthOptions options) Instructs the library to get a new token immediately.checkClientId
(BaseMessage msg, boolean allowNullClientId, boolean connected) Verify that a message, possibly containing a clientId, is compatible with Auth.clientId if it is setvoid
For testing purposes we need method to clear cached timeDeltacreateTokenRequest
(Auth.TokenParams params, Auth.AuthOptions options) Creates and signs an AblyAuth.TokenRequest
based on the specified (or if none specified, the client library stored)Auth.TokenParams
andAuth.AuthOptions
.Get the authentication method for this library instance.Get (a copy of) auth options currently set in this Auth.Param[]
Get query params representing the current authentication method and credentials.Get the credentials for HTTP basic auth, if available.void
onAuthError
(ErrorInfo err) renew()
Deprecated.Because the method returns early before renew() completes and does not provide a completion handler for callers.void
renewAuth
(Auth.RenewAuthResult result) Renew auth credentials.requestToken
(Auth.TokenParams params, Auth.AuthOptions tokenOptions) Calls the requestToken REST API endpoint to obtain an Ably Token according to the specifiedAuth.TokenParams
andAuth.AuthOptions
.long
Using time delta obtained before guess current server timevoid
setClientId
(String clientId) Set the clientId, after first initialisation in the construction of the library therefore an existing null value is significant - it means that ClientOptions.clientId was nullstatic long
-
Field Details
-
clientId
A client ID, used for identifying this client when publishing messages or for presence purposes. The clientId can be any non-empty string, except it cannot contain a *. This option is primarily intended to be used in situations where the library is instantiated with a key. Note that a clientId may also be implicit in a token used to instantiate the library. An error is raised if a clientId specified here conflicts with the clientId implicit in the token. Find out more about identified clients.Spec: RSA7, RSC17, RSA12
-
WILDCARD_CLIENTID
- See Also:
-
-
Method Details
-
authorize
public Auth.TokenDetails authorize(Auth.TokenParams params, Auth.AuthOptions options) throws AblyException Instructs the library to get a new token immediately. When using the realtime client, it upgrades the current realtime connection to use the new token, or if not connected, initiates a connection to Ably, once the new token has been obtained. Also stores anyAuth.TokenParams
andAuth.AuthOptions
passed in as the new defaults, to be used for all subsequent implicit or explicit token requests. AnyAuth.TokenParams
andAuth.AuthOptions
objects passed in entirely replace, as opposed to being merged with, the current client library saved values.Spec: RSA10
- Parameters:
-
params
- AAuth.TokenParams
object. -
options
- AnAuth.AuthOptions
object. - Returns:
- A
Auth.TokenDetails
object. - Throws:
AblyException
-
authorise
@Deprecated public Auth.TokenDetails authorise(Auth.TokenParams params, Auth.AuthOptions options) throws AblyException Deprecated.Alias of authorize() (0.9 RSA10l)- Throws:
AblyException
-
requestToken
public Auth.TokenDetails requestToken(Auth.TokenParams params, Auth.AuthOptions tokenOptions) throws AblyException Calls the requestToken REST API endpoint to obtain an Ably Token according to the specifiedAuth.TokenParams
andAuth.AuthOptions
. BothAuth.TokenParams
andAuth.AuthOptions
are optional. When omitted or null, the default token parameters and authentication options for the client library are used, as specified in theClientOptions
when the client library was instantiated, or later updated with an explicit authorize request. Values passed in are used instead of, rather than being merged with, the default values. To understand why an AblyAuth.TokenRequest
may be issued to clients in favor of a token, see Token Authentication explained.Spec: RSA8e
- Parameters:
-
params
- : AAuth.TokenParams
object. -
tokenOptions
- : AnAuth.AuthOptions
object. - Returns:
- A
Auth.TokenDetails
object. - Throws:
AblyException
-
createTokenRequest
public Auth.TokenRequest createTokenRequest(Auth.TokenParams params, Auth.AuthOptions options) throws AblyException Creates and signs an AblyAuth.TokenRequest
based on the specified (or if none specified, the client library stored)Auth.TokenParams
andAuth.AuthOptions
. Note this can only be used when the API key value is available locally. Otherwise, the AblyAuth.TokenRequest
must be obtained from the key owner. Use this to generate an AblyAuth.TokenRequest
in order to implement an Ably Token request callback for use by other clients. BothAuth.TokenParams
andAuth.AuthOptions
are optional. When omitted or null, the default token parameters and authentication options for the client library are used, as specified in theClientOptions
when the client library was instantiated, or later updated with an explicit authorize request. Values passed in are used instead of, rather than being merged with, the default values. To understand why an AblyAuth.TokenRequest
may be issued to clients in favor of a token, see Token Authentication explained.Spec: RSA9
- Parameters:
-
params
- : AAuth.TokenParams
object. -
options
- : AnAuth.AuthOptions
object. - Returns:
- A
Auth.TokenRequest
object. - Throws:
AblyException
-
getAuthMethod
Get the authentication method for this library instance.- Returns:
-
getBasicCredentials
Get the credentials for HTTP basic auth, if available.- Returns:
-
getAuthParams
Get query params representing the current authentication method and credentials.- Returns:
- Throws:
AblyException
-
getAuthOptions
Get (a copy of) auth options currently set in this Auth. -
renew
Deprecated.Because the method returns early before renew() completes and does not provide a completion handler for callers. Please userenewAuth(io.ably.lib.rest.Auth.RenewAuthResult)
insteadRenew auth credentials. Will obtain a new token, even if we already have an apparently valid one. Authorization will use the parameters supplied on construction.- Throws:
AblyException
-
renewAuth
Renew auth credentials. Will obtain a new token, even if we already have an apparently valid one. Authorization will use the parameters supplied on construction.- Parameters:
-
result
- Asynchronous result the completion Please note that completion callbackAuth.RenewAuthResult.onCompletion(boolean, TokenDetails, ErrorInfo)
is called on a background thread. - Throws:
AblyException
-
onAuthError
-
timestamp
public static long timestamp() -
getTokenDetails
-
getEncodedToken
-
assertValidToken
- Throws:
AblyException
-
assertAuthorizationHeader
Get the Authorization header, forcing the creation of a new token if requested- Parameters:
-
forceRenew
- - Throws:
AblyException
-
getAuthorizationHeader
-
setClientId
Set the clientId, after first initialisation in the construction of the library therefore an existing null value is significant - it means that ClientOptions.clientId was null- Parameters:
-
clientId
- - Throws:
AblyException
-
checkClientId
public String checkClientId(BaseMessage msg, boolean allowNullClientId, boolean connected) throws AblyException Verify that a message, possibly containing a clientId, is compatible with Auth.clientId if it is set- Parameters:
-
msg
- -
allowNullClientId
- true if it is ok for there to be no resolved clientId -
connected
- true if connected; if false it is ok for the library to be unidentified - Returns:
- the resolved clientId
- Throws:
AblyException
-
serverTimestamp
public long serverTimestamp()Using time delta obtained before guess current server time -
clearCachedServerTime
public void clearCachedServerTime()For testing purposes we need method to clear cached timeDelta
-