public class AuthState
extends java.lang.Object
Collects authorization state from authorization requests and responses. This facilitates the creation of subsequent requests based on this state, and allows for this state to be persisted easily.
| Modifier and Type | Class and Description |
|---|---|
static interface |
AuthState.AuthStateAction
Interface for actions executed in the context of fresh (non-expired) tokens.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
EXPIRY_TIME_TOLERANCE_MS
Tokens which have less time than this value left before expiry will be considered to be
expired for the purposes of calls to
performActionWithFreshTokens. |
| Constructor and Description |
|---|
AuthState()
Creates an empty, unauthenticated
AuthState. |
AuthState(AuthorizationResponse authResponse,
AuthorizationException authError)
Creates an
AuthState based on an authorization exchange. |
AuthState(AuthorizationResponse authResponse,
TokenResponse tokenResponse,
AuthorizationException authException)
Creates an
AuthState based on an authorization exchange and subsequent token
exchange. |
AuthState(AuthorizationServiceConfiguration config)
Creates an unauthenticated
AuthState, with the service configuration retained
for convenience. |
AuthState(RegistrationResponse regResponse)
Creates an
AuthState based on a dynamic registration client registration request. |
| Modifier and Type | Method and Description |
|---|---|
TokenRequest |
createTokenRefreshRequest()
Creates a token request for new tokens using the current refresh token.
|
TokenRequest |
createTokenRefreshRequest(java.util.Map<java.lang.String,java.lang.String> additionalParameters)
Creates a token request for new tokens using the current refresh token, adding the
specified additional parameters.
|
java.lang.String |
getAccessToken()
The current access token, if available.
|
java.lang.Long |
getAccessTokenExpirationTime()
The expiration time of the current access token (if available), as milliseconds from the
UNIX epoch (consistent with
System.currentTimeMillis()). |
AuthorizationException |
getAuthorizationException()
If the last response was an OAuth related failure, this returns the exception describing
the failure.
|
AuthorizationServiceConfiguration |
getAuthorizationServiceConfiguration()
The configuration of the authorization service associated with this authorization state.
|
ClientAuthentication |
getClientAuthentication()
Creates the required client authentication for the token endpoint based on information
in the most recent registration response (if it is set).
|
java.lang.String |
getClientSecret()
The current client secret, if available.
|
java.lang.Long |
getClientSecretExpirationTime()
The expiration time of the current client credentials (if available), as milliseconds from
the UNIX epoch (consistent with
System.currentTimeMillis()). |
java.lang.String |
getIdToken()
The current ID token, if available.
|
AuthorizationResponse |
getLastAuthorizationResponse()
The most recent authorization response used to update the authorization state.
|
RegistrationResponse |
getLastRegistrationResponse()
The most recent client registration response used to update this authorization state.
|
TokenResponse |
getLastTokenResponse()
The most recent token response used to update this authorization state.
|
boolean |
getNeedsTokenRefresh()
Determines whether the access token is considered to have expired.
|
java.lang.String |
getRefreshToken()
The most recent refresh token received from the server, if available.
|
java.lang.String |
getScope()
The scope of the current authorization grant.
|
java.util.Set<java.lang.String> |
getScopeSet()
A set representation of
getScope(), for convenience. |
boolean |
hasClientSecretExpired()
Determines whether the client credentials is considered to have expired.
|
boolean |
isAuthorized()
Determines whether the current state represents a successful authorization,
from which at least either an access token or an ID token have been retrieved.
|
static AuthState |
jsonDeserialize(org.json.JSONObject json)
Reads an authorization state instance from a JSON string representation produced by
jsonSerialize(). |
static AuthState |
jsonDeserialize(java.lang.String jsonStr)
Reads an authorization state instance from a JSON string representation produced by
jsonSerializeString(). |
org.json.JSONObject |
jsonSerialize()
Produces a JSON representation of the authorization state for persistent storage or local
transmission (e.g.
|
java.lang.String |
jsonSerializeString()
Produces a JSON string representation of the authorization state for persistent storage or
local transmission (e.g.
|
void |
performActionWithFreshTokens(AuthorizationService service,
AuthState.AuthStateAction action)
Ensures that a non-expired access token is available before invoking the provided action.
|
void |
performActionWithFreshTokens(AuthorizationService service,
ClientAuthentication clientAuth,
AuthState.AuthStateAction action)
Ensures that a non-expired access token is available before invoking the provided action.
|
void |
performActionWithFreshTokens(AuthorizationService service,
ClientAuthentication clientAuth,
java.util.Map<java.lang.String,java.lang.String> refreshTokenAdditionalParams,
AuthState.AuthStateAction action)
Ensures that a non-expired access token is available before invoking the provided action.
|
void |
performActionWithFreshTokens(AuthorizationService service,
java.util.Map<java.lang.String,java.lang.String> refreshTokenAdditionalParams,
AuthState.AuthStateAction action)
Ensures that a non-expired access token is available before invoking the provided action.
|
void |
setNeedsTokenRefresh(boolean needsTokenRefresh)
Sets whether to force an access token refresh, regardless of the current access token’s
expiration time.
|
void |
update(AuthorizationResponse authResponse,
AuthorizationException authException)
Updates the authorization state based on a new authorization response.
|
void |
update(RegistrationResponse regResponse)
Updates the authorization state based on a new client registration response.
|
void |
update(TokenResponse tokenResponse,
AuthorizationException authException)
Updates the authorization state based on a new token response.
|
public static final int EXPIRY_TIME_TOLERANCE_MS
Tokens which have less time than this value left before expiry will be considered to be
expired for the purposes of calls to
performActionWithFreshTokens.
public AuthState()
Creates an empty, unauthenticated AuthState.
public AuthState(@NonNull
AuthorizationServiceConfiguration config)
Creates an unauthenticated AuthState, with the service configuration retained
for convenience.
public AuthState(@Nullable
AuthorizationResponse authResponse,
@Nullable
AuthorizationException authError)
Creates an AuthState based on an authorization exchange.
public AuthState(@NonNull
RegistrationResponse regResponse)
Creates an AuthState based on a dynamic registration client registration request.
public AuthState(@NonNull
AuthorizationResponse authResponse,
@Nullable
TokenResponse tokenResponse,
@Nullable
AuthorizationException authException)
Creates an AuthState based on an authorization exchange and subsequent token
exchange.
@Nullable public java.lang.String getRefreshToken()
The most recent refresh token received from the server, if available. Rather than using
this property directly as part of any request depending on authorization state, it is
recommended to call performActionWithFreshTokens to ensure that fresh tokens are available.
@Nullable public java.lang.String getScope()
The scope of the current authorization grant. This represents the latest scope returned by the server and may be a subset of the scope that was initially granted.
@Nullable public java.util.Set<java.lang.String> getScopeSet()
A set representation of getScope(), for convenience.
@Nullable public AuthorizationResponse getLastAuthorizationResponse()
The most recent authorization response used to update the authorization state. For the
implicit flow, this will contain the latest access token. It is rarely necessary to
directly use the response; instead convenience methods are provided to retrieve the
access token,
access token expiration,
ID token
and scope regardless of the flow used to retrieve them.
@Nullable public TokenResponse getLastTokenResponse()
The most recent token response used to update this authorization state. For the
authorization code flow, this will contain the latest access token. It is rarely necessary
to directly use the response; instead convenience methods are provided to retrieve the
access token,
access token expiration,
ID token
and scope regardless of the flow used to retrieve them.
@Nullable public RegistrationResponse getLastRegistrationResponse()
The most recent client registration response used to update this authorization state.
It is rarely necessary to directly use the response; instead convenience methods are provided
to retrieve the client secret and
client secret expiration.
@Nullable public AuthorizationServiceConfiguration getAuthorizationServiceConfiguration()
The configuration of the authorization service associated with this authorization state.
@Nullable public java.lang.String getAccessToken()
The current access token, if available. Rather than using
this property directly as part of any request depending on authorization state, it s
recommended to call performActionWithFreshTokens to ensure that fresh tokens are available.
@Nullable public java.lang.Long getAccessTokenExpirationTime()
The expiration time of the current access token (if available), as milliseconds from the
UNIX epoch (consistent with System.currentTimeMillis()).
@Nullable public java.lang.String getIdToken()
The current ID token, if available.
public java.lang.String getClientSecret()
The current client secret, if available.
@Nullable public java.lang.Long getClientSecretExpirationTime()
The expiration time of the current client credentials (if available), as milliseconds from
the UNIX epoch (consistent with System.currentTimeMillis()). If the value is 0, the
client credentials will not expire.
public boolean isAuthorized()
Determines whether the current state represents a successful authorization, from which at least either an access token or an ID token have been retrieved.
@Nullable public AuthorizationException getAuthorizationException()
If the last response was an OAuth related failure, this returns the exception describing the failure.
public boolean getNeedsTokenRefresh()
Determines whether the access token is considered to have expired. If no refresh token
has been acquired, then this method will always return false. A token refresh
can be forced, regardless of the validity of any currently acquired access token, by
calling setNeedsTokenRefresh(true).
public void setNeedsTokenRefresh(boolean needsTokenRefresh)
Sets whether to force an access token refresh, regardless of the current access token’s expiration time.
public boolean hasClientSecretExpired()
Determines whether the client credentials is considered to have expired. If no client
credentials have been acquired, then this method will always return false
public void update(@Nullable
AuthorizationResponse authResponse,
@Nullable
AuthorizationException authException)
Updates the authorization state based on a new authorization response.
public void update(@Nullable
TokenResponse tokenResponse,
@Nullable
AuthorizationException authException)
Updates the authorization state based on a new token response.
public void update(@Nullable
RegistrationResponse regResponse)
Updates the authorization state based on a new client registration response.
public void performActionWithFreshTokens(@NonNull
AuthorizationService service,
@NonNull
AuthState.AuthStateAction action)
Ensures that a non-expired access token is available before invoking the provided action.
public void performActionWithFreshTokens(@NonNull
AuthorizationService service,
@NonNull
ClientAuthentication clientAuth,
@NonNull
AuthState.AuthStateAction action)
Ensures that a non-expired access token is available before invoking the provided action.
public void performActionWithFreshTokens(@NonNull
AuthorizationService service,
@NonNull
java.util.Map<java.lang.String,java.lang.String> refreshTokenAdditionalParams,
@NonNull
AuthState.AuthStateAction action)
Ensures that a non-expired access token is available before invoking the provided action. If a token refresh is required, the provided additional parameters will be included in this refresh request.
public void performActionWithFreshTokens(@NonNull
AuthorizationService service,
@NonNull
ClientAuthentication clientAuth,
@NonNull
java.util.Map<java.lang.String,java.lang.String> refreshTokenAdditionalParams,
@NonNull
AuthState.AuthStateAction action)
Ensures that a non-expired access token is available before invoking the provided action. If a token refresh is required, the provided additional parameters will be included in this refresh request.
@NonNull public TokenRequest createTokenRefreshRequest()
Creates a token request for new tokens using the current refresh token.
@NonNull public TokenRequest createTokenRefreshRequest(@NonNull java.util.Map<java.lang.String,java.lang.String> additionalParameters)
Creates a token request for new tokens using the current refresh token, adding the specified additional parameters.
public org.json.JSONObject jsonSerialize()
Produces a JSON representation of the authorization state for persistent storage or local transmission (e.g. between activities).
public java.lang.String jsonSerializeString()
Produces a JSON string representation of the authorization state for persistent storage or
local transmission (e.g. between activities). This method is just a convenience wrapper
for jsonSerialize(), converting the JSON object to its string form.
public static AuthState jsonDeserialize(@NonNull org.json.JSONObject json) throws org.json.JSONException
Reads an authorization state instance from a JSON string representation produced by
jsonSerialize().
org.json.JSONException - if the provided JSON does not match the expected structure.public static AuthState jsonDeserialize(@NonNull java.lang.String jsonStr) throws org.json.JSONException
Reads an authorization state instance from a JSON string representation produced by
jsonSerializeString(). This method is just a convenience wrapper for
jsonDeserialize(JSONObject), converting the JSON string to its JSON object form.
org.json.JSONException - if the provided JSON does not match the expected structure.public ClientAuthentication getClientAuthentication() throws ClientAuthentication.UnsupportedAuthenticationMethod
Creates the required client authentication for the token endpoint based on information in the most recent registration response (if it is set).
ClientAuthentication.UnsupportedAuthenticationMethod - if the expected client
authentication method is unsupported by this client library.