Class AuthState
- java.lang.Object
-
- net.openid.appauth.AuthState
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceAuthState.AuthStateActionInterface for actions executed in the context of fresh (non-expired) tokens.
-
Field Summary
Fields Modifier and Type Field Description static intEXPIRY_TIME_TOLERANCE_MSTokens which have less time than this value left before expiry will be considered to be expired for the purposes of calls toperformActionWithFreshTokens.
-
Constructor Summary
Constructors Constructor Description AuthState()Creates an empty, unauthenticatedAuthState.AuthState(AuthorizationResponse authResponse, AuthorizationException authError)Creates anAuthStatebased on an authorization exchange.AuthState(AuthorizationResponse authResponse, TokenResponse tokenResponse, AuthorizationException authException)Creates anAuthStatebased on an authorization exchange and subsequent token exchange.AuthState(AuthorizationServiceConfiguration config)Creates an unauthenticatedAuthState, with the service configuration retained for convenience.AuthState(RegistrationResponse regResponse)Creates anAuthStatebased on a dynamic registration client registration request.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TokenRequestcreateTokenRefreshRequest()Creates a token request for new tokens using the current refresh token.TokenRequestcreateTokenRefreshRequest(Map<String,String> additionalParameters)Creates a token request for new tokens using the current refresh token, adding the specified additional parameters.StringgetAccessToken()The current access token, if available.LonggetAccessTokenExpirationTime()The expiration time of the current access token (if available), as milliseconds from the UNIX epoch (consistent withSystem.currentTimeMillis()).AuthorizationExceptiongetAuthorizationException()If the last response was an OAuth related failure, this returns the exception describing the failure.AuthorizationServiceConfigurationgetAuthorizationServiceConfiguration()The configuration of the authorization service associated with this authorization state.ClientAuthenticationgetClientAuthentication()Creates the required client authentication for the token endpoint based on information in the most recent registration response (if it is set).StringgetClientSecret()The current client secret, if available.LonggetClientSecretExpirationTime()The expiration time of the current client credentials (if available), as milliseconds from the UNIX epoch (consistent withSystem.currentTimeMillis()).StringgetIdToken()The current ID token, if available.AuthorizationResponsegetLastAuthorizationResponse()The most recent authorization response used to update the authorization state.RegistrationResponsegetLastRegistrationResponse()The most recent client registration response used to update this authorization state.TokenResponsegetLastTokenResponse()The most recent token response used to update this authorization state.booleangetNeedsTokenRefresh()Determines whether the access token is considered to have expired.IdTokengetParsedIdToken()The current parsed ID token, if available.StringgetRefreshToken()The most recent refresh token received from the server, if available.StringgetScope()The scope of the current authorization grant.Set<String>getScopeSet()A set representation ofgetScope(), for convenience.booleanhasClientSecretExpired()Determines whether the client credentials is considered to have expired.booleanisAuthorized()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 AuthStatejsonDeserialize(String jsonStr)Reads an authorization state instance from a JSON string representation produced byjsonSerializeString().static AuthStatejsonDeserialize(JSONObject json)Reads an authorization state instance from a JSON string representation produced byjsonSerialize().JSONObjectjsonSerialize()Produces a JSON representation of the authorization state for persistent storage or local transmission (e.g.StringjsonSerializeString()Produces a JSON string representation of the authorization state for persistent storage or local transmission (e.g.voidperformActionWithFreshTokens(AuthorizationService service, Map<String,String> refreshTokenAdditionalParams, AuthState.AuthStateAction action)Ensures that a non-expired access token is available before invoking the provided action.voidperformActionWithFreshTokens(AuthorizationService service, AuthState.AuthStateAction action)Ensures that a non-expired access token is available before invoking the provided action.voidperformActionWithFreshTokens(AuthorizationService service, ClientAuthentication clientAuth, Map<String,String> refreshTokenAdditionalParams, AuthState.AuthStateAction action)Ensures that a non-expired access token is available before invoking the provided action.voidperformActionWithFreshTokens(AuthorizationService service, ClientAuthentication clientAuth, AuthState.AuthStateAction action)Ensures that a non-expired access token is available before invoking the provided action.voidsetNeedsTokenRefresh(boolean needsTokenRefresh)Sets whether to force an access token refresh, regardless of the current access token’s expiration time.voidupdate(AuthorizationResponse authResponse, AuthorizationException authException)Updates the authorization state based on a new authorization response.voidupdate(RegistrationResponse regResponse)Updates the authorization state based on a new client registration response.voidupdate(TokenResponse tokenResponse, AuthorizationException authException)Updates the authorization state based on a new token response.
-
-
-
Field Detail
-
EXPIRY_TIME_TOLERANCE_MS
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 toperformActionWithFreshTokens.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AuthState
public AuthState(@NonNull AuthorizationServiceConfiguration config)
Creates an unauthenticatedAuthState, with the service configuration retained for convenience.
-
AuthState
public AuthState(@Nullable AuthorizationResponse authResponse, @Nullable AuthorizationException authError)
Creates anAuthStatebased on an authorization exchange.
-
AuthState
public AuthState(@NonNull RegistrationResponse regResponse)
Creates anAuthStatebased on a dynamic registration client registration request.
-
AuthState
public AuthState(@NonNull AuthorizationResponse authResponse, @Nullable TokenResponse tokenResponse, @Nullable AuthorizationException authException)
Creates anAuthStatebased on an authorization exchange and subsequent token exchange.
-
-
Method Detail
-
getRefreshToken
@Nullable public 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
performActionWithFreshTokensto ensure that fresh tokens are available.
-
getScope
@Nullable public 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.
-
getScopeSet
@Nullable public Set<String> getScopeSet()
A set representation ofgetScope(), for convenience.
-
getLastAuthorizationResponse
@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 tokenandscoperegardless of the flow used to retrieve them.
-
getLastTokenResponse
@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 tokenandscoperegardless of the flow used to retrieve them.
-
getLastRegistrationResponse
@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 secretandclient secret expiration.
-
getAuthorizationServiceConfiguration
@Nullable public AuthorizationServiceConfiguration getAuthorizationServiceConfiguration()
The configuration of the authorization service associated with this authorization state.
-
getAccessToken
@Nullable public 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
performActionWithFreshTokensto ensure that fresh tokens are available.
-
getAccessTokenExpirationTime
@Nullable public Long getAccessTokenExpirationTime()
The expiration time of the current access token (if available), as milliseconds from the UNIX epoch (consistent withSystem.currentTimeMillis()).
-
getIdToken
@Nullable public String getIdToken()
The current ID token, if available.
-
getParsedIdToken
@Nullable public IdToken getParsedIdToken()
The current parsed ID token, if available.
-
getClientSecret
public String getClientSecret()
The current client secret, if available.
-
getClientSecretExpirationTime
@Nullable public Long getClientSecretExpirationTime()
The expiration time of the current client credentials (if available), as milliseconds from the UNIX epoch (consistent withSystem.currentTimeMillis()).If the value is 0, the client credentials will not expire.
-
isAuthorized
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.
-
getAuthorizationException
@Nullable public AuthorizationException getAuthorizationException()
If the last response was an OAuth related failure, this returns the exception describing the failure.
-
getNeedsTokenRefresh
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 callingsetNeedsTokenRefresh(true).
-
setNeedsTokenRefresh
public void setNeedsTokenRefresh(boolean needsTokenRefresh)
Sets whether to force an access token refresh, regardless of the current access token’s expiration time.
-
hasClientSecretExpired
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
-
update
public void update(@Nullable AuthorizationResponse authResponse, @Nullable AuthorizationException authException)
Updates the authorization state based on a new authorization response.
-
update
public void update(@Nullable TokenResponse tokenResponse, @Nullable AuthorizationException authException)
Updates the authorization state based on a new token response.
-
update
public void update(@Nullable RegistrationResponse regResponse)
Updates the authorization state based on a new client registration response.
-
performActionWithFreshTokens
public void performActionWithFreshTokens(@NonNull AuthorizationService service, @NonNull AuthState.AuthStateAction action)
Ensures that a non-expired access token is available before invoking the provided action.
-
performActionWithFreshTokens
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.
-
performActionWithFreshTokens
public void performActionWithFreshTokens(@NonNull AuthorizationService service, @NonNull Map<String,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.
-
performActionWithFreshTokens
public void performActionWithFreshTokens(@NonNull AuthorizationService service, @NonNull ClientAuthentication clientAuth, @NonNull Map<String,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.
-
createTokenRefreshRequest
@NonNull public TokenRequest createTokenRefreshRequest()
Creates a token request for new tokens using the current refresh token.
-
createTokenRefreshRequest
@NonNull public TokenRequest createTokenRefreshRequest(@NonNull Map<String,String> additionalParameters)
Creates a token request for new tokens using the current refresh token, adding the specified additional parameters.
-
jsonSerialize
public JSONObject jsonSerialize()
Produces a JSON representation of the authorization state for persistent storage or local transmission (e.g.between activities).
-
jsonSerializeString
public 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.
-
jsonDeserialize
public static AuthState jsonDeserialize(@NonNull JSONObject json) throws JSONException
Reads an authorization state instance from a JSON string representation produced byjsonSerialize().- Throws:
JSONException- if the provided JSON does not match the expected structure.
-
jsonDeserialize
public static AuthState jsonDeserialize(@NonNull String jsonStr) throws JSONException
Reads an authorization state instance from a JSON string representation produced byjsonSerializeString().This method is just a convenience wrapper for
jsonDeserialize(JSONObject), converting the JSON string to its JSON object form.- Throws:
JSONException- if the provided JSON does not match the expected structure.
-
getClientAuthentication
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).- Throws:
ClientAuthentication.UnsupportedAuthenticationMethod- if the expected client authentication method is unsupported by this client library.
-
-