Class AuthStrategyFactory


  • public class AuthStrategyFactory
    extends Object
    Factory for creating AuthStrategy instances.

    This factory provides static methods to create various authentication strategies without directly instantiating the strategy classes. It's the recommended way to create authentication strategies for use with RocketRestConfig.

    No Authentication

    
     AuthStrategy noAuth = AuthStrategyFactory.createNoAuth();
     

    Basic Authentication

    
     AuthStrategy basic = AuthStrategyFactory.createBasicAuth("username", "password");
     

    Bearer Token

    
     // Simple bearer token
     AuthStrategy bearer = AuthStrategyFactory.createBearerToken("my-api-token");
    
     // Bearer token with custom refresh logic
     AuthStrategy refreshable = AuthStrategyFactory.createBearerToken("initial-token", () -> {
         // Custom refresh logic
         String newToken = fetchNewTokenFromServer();
         return newToken != null;
     });
     

    OAuth 2.0 Client Credentials

    
     AuthStrategy oauth = AuthStrategyFactory.createOAuth2ClientCredentials(
         "client-id",
         "client-secret",
         "https://auth.example.com/oauth/token"
     );
    
     // With additional parameters (e.g., scope)
     Map<String, String> params = new HashMap<>();
     params.put("scope", "read write");
     AuthStrategy oauthWithScope = AuthStrategyFactory.createOAuth2ClientCredentials(
         "client-id", "client-secret", "https://auth.example.com/oauth/token", params
     );
     

    OAuth 2.0 Password Grant

    
     AuthStrategy password = AuthStrategyFactory.createOAuth2Password(
         "user@example.com",
         "userPassword",
         "https://auth.example.com/oauth/token"
     );
     
    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    AuthStrategy, RocketRestConfig
    • Constructor Detail

      • AuthStrategyFactory

        public AuthStrategyFactory()
    • Method Detail

      • createNoAuth

        public static AuthStrategy createNoAuth()
        Creates a strategy that does not perform authentication.
        Returns:
        a new no-auth strategy
      • createBasicAuth

        public static AuthStrategy createBasicAuth​(String username,
                                                   String password)
        Creates a strategy that uses HTTP Basic authentication.
        Parameters:
        username - the username
        password - the password
        Returns:
        a new basic auth strategy
      • createBearerToken

        public static AuthStrategy createBearerToken​(String token)
        Creates a strategy that uses Bearer token authentication.
        Parameters:
        token - the bearer token
        Returns:
        a new bearer token strategy
      • createBearerToken

        public static AuthStrategy createBearerToken​(String token,
                                                     BooleanSupplier refreshTokenLogic)
        Creates a strategy that uses Bearer token authentication with custom refresh logic.
        Parameters:
        token - the bearer token
        refreshTokenLogic - a BooleanSupplier that dictates the behavior of token refresh. It should return true if the token was successfully refreshed, false otherwise.
        Returns:
        a new bearer token strategy with custom refresh logic
      • createOAuth2ClientCredentials

        public static AuthStrategy createOAuth2ClientCredentials​(String clientId,
                                                                 String clientSecret,
                                                                 String tokenUrl)
        Creates a strategy that uses OAuth 2.0 client credentials flow.
        Parameters:
        clientId - the OAuth 2.0 client ID
        clientSecret - the OAuth 2.0 client secret
        Returns:
        a new OAuth 2.0 client credentials strategy
      • createOAuth2ClientCredentials

        public static AuthStrategy createOAuth2ClientCredentials​(String clientId,
                                                                 String clientSecret,
                                                                 String tokenUrl,
                                                                 Map<String,​String> additionalParams)
        Creates a strategy that uses OAuth 2.0 client credentials flow with additional parameters.
        Parameters:
        clientId - the OAuth 2.0 client ID
        clientSecret - the OAuth 2.0 client secret
        tokenUrl - the token endpoint URL
        additionalParams - additional parameters to include in the token request
        Returns:
        a new OAuth 2.0 client credentials strategy
      • createOAuth2Password

        public static AuthStrategy createOAuth2Password​(String username,
                                                        String password,
                                                        String tokenUrl)
        Creates a strategy that uses OAuth 2.0 password flow.
        Parameters:
        username - the user's username
        password - the user's password
        Returns:
        a new OAuth 2.0 password strategy
      • createOAuth2Password

        public static AuthStrategy createOAuth2Password​(String username,
                                                        String password,
                                                        String clientId,
                                                        String clientSecret,
                                                        String tokenUrl)
        Creates a strategy that uses OAuth 2.0 password flow with client credentials.
        Parameters:
        username - the user's username
        password - the user's password
        clientId - the OAuth 2.0 client ID
        clientSecret - the OAuth 2.0 client secret
        Returns:
        a new OAuth 2.0 password strategy
      • createOAuth2Password

        public static AuthStrategy createOAuth2Password​(String username,
                                                        String password,
                                                        String clientId,
                                                        String clientSecret,
                                                        String tokenUrl,
                                                        Map<String,​String> additionalParams)
        Creates a strategy that uses OAuth 2.0 password flow with client credentials and additional parameters.
        Parameters:
        username - the user's username
        password - the user's password
        clientId - the OAuth 2.0 client ID
        clientSecret - the OAuth 2.0 client secret
        tokenUrl - the token endpoint URL
        additionalParams - additional parameters to include in the token request
        Returns:
        a new OAuth 2.0 password strategy
      • createOAuth2Assertion

        public static AuthStrategy createOAuth2Assertion​(String clientId,
                                                         String userId,
                                                         String privateKey,
                                                         String companyId,
                                                         String grantType,
                                                         String assertionUrl,
                                                         String tokenUrl)
        Creates a strategy that uses OAuth 2.0 assertion flow. This can be used with various identity providers like SAP, Azure AD, Okta, etc.
        Parameters:
        clientId - the OAuth client ID
        userId - the user ID
        privateKey - the private key for assertion
        companyId - the company ID (optional, can be null)
        grantType - the OAuth grant type
        assertionUrl - the assertion endpoint URL
        tokenUrl - the token endpoint URL
        Returns:
        a new OAuth 2.0 assertion strategy
      • createOAuth2Assertion

        public static AuthStrategy createOAuth2Assertion​(String clientId,
                                                         String userId,
                                                         String privateKey,
                                                         String companyId,
                                                         String grantType,
                                                         String assertionUrl,
                                                         String tokenUrl,
                                                         Map<String,​String> additionalAssertionParams,
                                                         Map<String,​String> additionalTokenParams)
        Creates a strategy that uses OAuth 2.0 assertion flow with additional parameters. This can be used with various identity providers like SAP, Azure AD, Okta, etc.
        Parameters:
        clientId - the OAuth client ID
        userId - the user ID
        privateKey - the private key for assertion
        companyId - the company ID (optional, can be null)
        grantType - the OAuth grant type
        assertionUrl - the assertion endpoint URL
        tokenUrl - the token endpoint URL
        additionalAssertionParams - additional parameters for assertion request
        additionalTokenParams - additional parameters for token request
        Returns:
        a new OAuth 2.0 assertion strategy