Package com.google.auth.oauth2
Class UserAuthorizer
java.lang.Object
com.google.auth.oauth2.UserAuthorizer
Handles an interactive 3-Legged-OAuth2 (3LO) user consent authorization.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static enum
Represents the client authentication types as specified in RFC 7591.static class
Represents the response from an OAuth token exchange, including configuration details used to initiate the flow.private class
Implementation of listener used by monitorCredentials to rewrite the credentials when the tokens are refreshed. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final URI
private final UserAuthorizer.ClientAuthenticationType
private final ClientId
(package private) static final URI
private final String
private final PKCEProvider
private final Collection
<String> private final String
private final URI
private final TokenStore
private final HttpTransportFactory
private final URI
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
UserAuthorizer
(UserAuthorizer.Builder builder) Internal constructor. -
Method Summary
Modifier and TypeMethodDescriptiongetAndStoreCredentialsFromCode
(String userId, String code, URI baseUri) Exchanges an authorization code for tokens and stores them.getAuthorizationUrl
(String userId, String state, URI baseUri) Return a URL that performs the authorization consent prompt web UI.getAuthorizationUrl
(String userId, String state, URI baseUri, Map<String, String> additionalParameters) Return a URL that performs the authorization consent prompt web UI.Returns the URI for implementation of the OAuth2 web callback.getCallbackUri
(URI baseUri) Returns the URI for implementation of the OAuth2 web callback, optionally relative to the specified URI.Returns the client authentication type as defined in RFC 7591.Returns the Client ID user to identify the OAuth2 consent prompt.getCredentials
(String userId) Attempts to retrieve credentials for the approved end user consent.getCredentialsFromCode
(String code, URI baseUri) Returns a UserCredentials instance by exchanging an OAuth2 authorization code for tokens.Returns a UserCredentials instance by exchanging an OAuth2 authorization code for tokens.Returns the scopes defining the user consent.getTokenResponseFromAuthCodeExchange
(String code, URI callbackUri, Map<String, String> additionalParameters) Handles OAuth2 authorization code exchange and returns aUserAuthorizer.TokenResponseWithConfig
object containing the tokens and configuration details.Returns the implementation of a component for long term storage of tokens.protected void
monitorCredentials
(String userId, UserCredentials credentials) Adds a listen to rewrite the credentials when the tokens are refreshed.static UserAuthorizer.Builder
void
revokeAuthorization
(String userId) Revokes the authorization for tokens stored for the user.void
storeCredentials
(String userId, UserCredentials credentials) Puts the end user credentials in long term storage.
-
Field Details
-
DEFAULT_CALLBACK_URI
-
TOKEN_STORE_ERROR
- See Also:
-
FETCH_TOKEN_ERROR
- See Also:
-
clientId
-
scopes
-
tokenStore
-
callbackUri
-
transportFactory
-
tokenServerUri
-
userAuthUri
-
pkce
-
clientAuthenticationType
-
-
Constructor Details
-
UserAuthorizer
Internal constructor. SeeUserAuthorizer.Builder
.
-
-
Method Details
-
getClientId
Returns the Client ID user to identify the OAuth2 consent prompt.- Returns:
- The Client ID.
-
getScopes
Returns the scopes defining the user consent.- Returns:
- The collection of scopes defining the user consent.
-
getCallbackUri
Returns the URI for implementation of the OAuth2 web callback.- Returns:
- The URI for the OAuth2 web callback.
-
getCallbackUri
Returns the URI for implementation of the OAuth2 web callback, optionally relative to the specified URI.The callback URI is often relative to enable an application to be tested from more than one place so this can be used to resolve it relative to another URI.
- Parameters:
baseUri
- The URI to resolve the callback URI relative to.- Returns:
- The resolved URI.
-
getTokenStore
Returns the implementation of a component for long term storage of tokens.- Returns:
- The token storage implementation for long term storage of tokens.
-
getClientAuthenticationType
Returns the client authentication type as defined in RFC 7591.- Returns:
- The
UserAuthorizer.ClientAuthenticationType
-
getAuthorizationUrl
Return a URL that performs the authorization consent prompt web UI.- Parameters:
userId
- Application's identifier for the end user.state
- State that is passed on to the OAuth2 callback URI after the consent.baseUri
- The URI to resolve the OAuth2 callback URI relative to.- Returns:
- The URL that can be navigated or redirected to.
-
getAuthorizationUrl
public URL getAuthorizationUrl(String userId, String state, URI baseUri, Map<String, String> additionalParameters) Return a URL that performs the authorization consent prompt web UI.- Parameters:
userId
- Application's identifier for the end user.state
- State that is passed on to the OAuth2 callback URI after the consent.baseUri
- The URI to resolve the OAuth2 callback URI relative to.additionalParameters
- Additional query parameters to be added to the authorization URL.- Returns:
- The URL that can be navigated or redirected to.
-
getCredentials
Attempts to retrieve credentials for the approved end user consent.- Parameters:
userId
- Application's identifier for the end user.- Returns:
- The loaded credentials or null if there are no valid approved credentials.
- Throws:
IOException
- If there is error retrieving or loading the credentials.
-
getCredentialsFromCode
Returns a UserCredentials instance by exchanging an OAuth2 authorization code for tokens.- Parameters:
code
- Code returned from OAuth2 consent prompt.baseUri
- The URI to resolve the OAuth2 callback URI relative to.- Returns:
- the UserCredentials instance created from the authorization code.
- Throws:
IOException
- An error from the server API call to get the tokens.
-
getCredentialsFromCode
public UserCredentials getCredentialsFromCode(String code, URI baseUri, Map<String, String> additionalParameters) throws IOExceptionReturns a UserCredentials instance by exchanging an OAuth2 authorization code for tokens.- Parameters:
code
- Code returned from OAuth2 consent prompt.baseUri
- The URI to resolve the OAuth2 callback URI relative to.additionalParameters
- Additional parameters to be added to the post body of token endpoint request.- Returns:
- the UserCredentials instance created from the authorization code.
- Throws:
IOException
- An error from the server API call to get the tokens.
-
getTokenResponseFromAuthCodeExchange
public UserAuthorizer.TokenResponseWithConfig getTokenResponseFromAuthCodeExchange(String code, URI callbackUri, Map<String, String> additionalParameters) throws IOExceptionHandles OAuth2 authorization code exchange and returns aUserAuthorizer.TokenResponseWithConfig
object containing the tokens and configuration details.- Parameters:
code
- The authorization code received from the OAuth2 authorization server.callbackUri
- The URI to which the authorization server redirected the user after granting authorization.additionalParameters
- Additional parameters to include in the token exchange request.- Returns:
- A
UserAuthorizer.TokenResponseWithConfig
object containing the access token, refresh token (if granted), and configuration details used in the OAuth flow. - Throws:
IOException
- If an error occurs during the token exchange process.
-
getAndStoreCredentialsFromCode
public UserCredentials getAndStoreCredentialsFromCode(String userId, String code, URI baseUri) throws IOException Exchanges an authorization code for tokens and stores them.- Parameters:
userId
- Application's identifier for the end user.code
- Code returned from OAuth2 consent prompt.baseUri
- The URI to resolve the OAuth2 callback URI relative to.- Returns:
- UserCredentials instance created from the authorization code.
- Throws:
IOException
- An error from the server API call to get the tokens or store the tokens.
-
revokeAuthorization
Revokes the authorization for tokens stored for the user.- Parameters:
userId
- Application's identifier for the end user.- Throws:
IOException
- An error calling the revoke API or deleting the state.
-
storeCredentials
Puts the end user credentials in long term storage.- Parameters:
userId
- Application's identifier for the end user.credentials
- UserCredentials instance for the authorized consent.- Throws:
IOException
- An error storing the credentials.
-
monitorCredentials
Adds a listen to rewrite the credentials when the tokens are refreshed.- Parameters:
userId
- Application's identifier for the end user.credentials
- UserCredentials instance to listen to.
-
getCredentialsFromCodeInternal
private UserAuthorizer.TokenResponseWithConfig getCredentialsFromCodeInternal(String code, URI baseUri, Map<String, String> additionalParameters) throws IOException- Throws:
IOException
-
newBuilder
-
toBuilder
-