Class UserAuthorizer

java.lang.Object
com.google.auth.oauth2.UserAuthorizer

public class UserAuthorizer extends Object
Handles an interactive 3-Legged-OAuth2 (3LO) user consent authorization.
  • Field Details

  • Constructor Details

  • Method Details

    • getClientId

      public ClientId getClientId()
      Returns the Client ID user to identify the OAuth2 consent prompt.
      Returns:
      The Client ID.
    • getScopes

      public Collection<String> getScopes()
      Returns the scopes defining the user consent.
      Returns:
      The collection of scopes defining the user consent.
    • getCallbackUri

      public URI getCallbackUri()
      Returns the URI for implementation of the OAuth2 web callback.
      Returns:
      The URI for the OAuth2 web callback.
    • getCallbackUri

      public URI getCallbackUri(URI baseUri)
      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

      public TokenStore 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

      public UserAuthorizer.ClientAuthenticationType getClientAuthenticationType()
      Returns the client authentication type as defined in RFC 7591.
      Returns:
      The UserAuthorizer.ClientAuthenticationType
    • getAuthorizationUrl

      public URL getAuthorizationUrl(String userId, String state, URI baseUri)
      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

      public UserCredentials getCredentials(String userId) throws IOException
      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

      public UserCredentials getCredentialsFromCode(String code, URI baseUri) throws IOException
      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 IOException
      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.
      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 IOException
      Handles OAuth2 authorization code exchange and returns a UserAuthorizer.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

      public void revokeAuthorization(String userId) throws IOException
      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

      public void storeCredentials(String userId, UserCredentials credentials) throws IOException
      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

      protected void monitorCredentials(String userId, UserCredentials credentials)
      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

      public static UserAuthorizer.Builder newBuilder()
    • toBuilder

      public UserAuthorizer.Builder toBuilder()