Package io.ably.lib.rest
Class Auth
- java.lang.Object
-
- io.ably.lib.rest.Auth
-
public class Auth extends java.lang.ObjectToken-generation and authentication operations for the Ably API. See the Ably Authentication documentation for details of the authentication methods available. Creates AblyAuth.TokenRequestobjects and obtains Ably Tokens from Ably to subsequently issue to less trusted clients.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAuth.AuthMethodAuthentication methodsstatic classAuth.AuthOptionsPasses authentication-specific properties in authentication requests to Ably.static interfaceAuth.AuthUpdateResultAn interface providing update result for onAuthUpdatedstatic interfaceAuth.RenewAuthResultAn interface providing completion callbackk for renewAuthstatic interfaceAuth.TokenCallbackAn interface implemented by a callback that provides either tokens, or signed token requests, in response to a request with given token params.static classAuth.TokenDetailsContains an Ably Token and its associated metadata.static classAuth.TokenParamsDefines the properties of an Ably Token.static classAuth.TokenRequestContains the properties of a request for a token to Ably.
-
Field Summary
Fields Modifier and Type Field Description java.lang.StringclientIdA client ID, used for identifying this client when publishing messages or for presence purposes.static java.lang.StringWILDCARD_CLIENTID
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidassertAuthorizationHeader(boolean forceRenew)Get the Authorization header, forcing the creation of a new token if requestedAuth.TokenDetailsassertValidToken()Auth.TokenDetailsauthorise(Auth.TokenParams params, Auth.AuthOptions options)Deprecated.Auth.TokenDetailsauthorize(Auth.TokenParams params, Auth.AuthOptions options)Instructs the library to get a new token immediately.java.lang.StringcheckClientId(BaseMessage msg, boolean allowNullClientId, boolean connected)Verify that a message, possibly containing a clientId, is compatible with Auth.clientId if it is setvoidclearCachedServerTime()For testing purposes we need method to clear cached timeDeltaAuth.TokenRequestcreateTokenRequest(Auth.TokenParams params, Auth.AuthOptions options)Creates and signs an AblyAuth.TokenRequestbased on the specified (or if none specified, the client library stored)Auth.TokenParamsandAuth.AuthOptions.Auth.AuthMethodgetAuthMethod()Get the authentication method for this library instance.Auth.AuthOptionsgetAuthOptions()Get (a copy of) auth options currently set in this Auth.java.lang.StringgetAuthorizationHeader()Param[]getAuthParams()Get query params representing the current authentication method and credentials.java.lang.StringgetBasicCredentials()Get the credentials for HTTP basic auth, if available.java.lang.StringgetEncodedToken()Auth.TokenDetailsgetTokenDetails()voidonAuthError(ErrorInfo err)Auth.TokenDetailsrenew()Deprecated.Because the method returns early before renew() completes and does not provide a completion handler for callers.voidrenewAuth(Auth.RenewAuthResult result)Renew auth credentials.Auth.TokenDetailsrequestToken(Auth.TokenParams params, Auth.AuthOptions tokenOptions)Calls the requestToken REST API endpoint to obtain an Ably Token according to the specifiedAuth.TokenParamsandAuth.AuthOptions.longserverTimestamp()Using time delta obtained before guess current server timevoidsetClientId(java.lang.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 longtimestamp()
-
-
-
Field Detail
-
clientId
public java.lang.String 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
public static final java.lang.String WILDCARD_CLIENTID
- See Also:
- Constant Field Values
-
-
Method Detail
-
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.TokenParamsandAuth.AuthOptionspassed in as the new defaults, to be used for all subsequent implicit or explicit token requests. AnyAuth.TokenParamsandAuth.AuthOptionsobjects passed in entirely replace, as opposed to being merged with, the current client library saved values.Spec: RSA10
- Parameters:
params- AAuth.TokenParamsobject.options- AnAuth.AuthOptionsobject.- Returns:
- A
Auth.TokenDetailsobject. - 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.TokenParamsandAuth.AuthOptions. BothAuth.TokenParamsandAuth.AuthOptionsare optional. When omitted or null, the default token parameters and authentication options for the client library are used, as specified in theClientOptionswhen 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.TokenRequestmay be issued to clients in favor of a token, see Token Authentication explained.Spec: RSA8e
- Parameters:
params- : AAuth.TokenParamsobject.tokenOptions- : AnAuth.AuthOptionsobject.- Returns:
- A
Auth.TokenDetailsobject. - Throws:
AblyException
-
createTokenRequest
public Auth.TokenRequest createTokenRequest(Auth.TokenParams params, Auth.AuthOptions options) throws AblyException
Creates and signs an AblyAuth.TokenRequestbased on the specified (or if none specified, the client library stored)Auth.TokenParamsandAuth.AuthOptions. Note this can only be used when the API key value is available locally. Otherwise, the AblyAuth.TokenRequestmust be obtained from the key owner. Use this to generate an AblyAuth.TokenRequestin order to implement an Ably Token request callback for use by other clients. BothAuth.TokenParamsandAuth.AuthOptionsare optional. When omitted or null, the default token parameters and authentication options for the client library are used, as specified in theClientOptionswhen 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.TokenRequestmay be issued to clients in favor of a token, see Token Authentication explained.Spec: RSA9
- Parameters:
params- : AAuth.TokenParamsobject.options- : AnAuth.AuthOptionsobject.- Returns:
- A
Auth.TokenRequestobject. - Throws:
AblyException
-
getAuthMethod
public Auth.AuthMethod getAuthMethod()
Get the authentication method for this library instance.- Returns:
-
getBasicCredentials
public java.lang.String getBasicCredentials()
Get the credentials for HTTP basic auth, if available.- Returns:
-
getAuthParams
public Param[] getAuthParams() throws AblyException
Get query params representing the current authentication method and credentials.- Returns:
- Throws:
AblyException
-
getAuthOptions
public Auth.AuthOptions getAuthOptions()
Get (a copy of) auth options currently set in this Auth.
-
renew
@Deprecated public Auth.TokenDetails renew() throws AblyException
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
public void renewAuth(Auth.RenewAuthResult result) throws AblyException
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
public void onAuthError(ErrorInfo err)
-
timestamp
public static long timestamp()
-
getTokenDetails
public Auth.TokenDetails getTokenDetails()
-
getEncodedToken
public java.lang.String getEncodedToken()
-
assertValidToken
public Auth.TokenDetails assertValidToken() throws AblyException
- Throws:
AblyException
-
assertAuthorizationHeader
public void assertAuthorizationHeader(boolean forceRenew) throws AblyExceptionGet the Authorization header, forcing the creation of a new token if requested- Parameters:
forceRenew-- Throws:
AblyException
-
getAuthorizationHeader
public java.lang.String getAuthorizationHeader()
-
setClientId
public void setClientId(java.lang.String clientId) throws AblyExceptionSet 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 java.lang.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 clientIdconnected- 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
-
-