Interface AuthStrategy

  • All Known Implementing Classes:
    AbstractOAuth2Strategy, BasicAuthStrategy, BearerTokenStrategy, NoAuthStrategy, OAuth2AssertionStrategy, OAuth2ClientCredentialsStrategy, OAuth2PasswordStrategy

    public interface AuthStrategy
    Interface for authentication strategies used by RocketRest.

    Authentication strategies encapsulate the logic for different authentication methods, from simple bearer tokens to complex OAuth 2.0 flows. Implementations are pluggable and can be configured via RocketRestConfig.

    Available Implementations

    Using Strategies

    
     // Via factory (recommended)
     AuthStrategy bearer = AuthStrategyFactory.createBearerToken("my-token");
     AuthStrategy basic = AuthStrategyFactory.createBasicAuth("user", "pass");
     AuthStrategy oauth = AuthStrategyFactory.createOAuth2ClientCredentials(
         "client-id", "client-secret", "https://auth.example.com/token"
     );
    
     // Configure in RocketRestConfig
     RocketRestConfig config = RocketRestConfig.builder("https://api.example.com")
         .authStrategy(bearer)
         .build();
     

    Custom Strategy Implementation

    
     public class CustomAuthStrategy implements AuthStrategy {
         @Override
         public AuthType getType() {
             return AuthType.BEARER_TOKEN;
         }
    
         @Override
         public RocketHeaders applyAuthHeaders(RocketHeaders headers) {
             headers.set("X-Custom-Auth", computeAuthValue());
             return headers;
         }
    
         @Override
         public boolean needsTokenRefresh() {
             return isTokenExpired();
         }
    
         @Override
         public boolean refreshCredentials() {
             return fetchNewToken();
         }
     }
     
    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    AuthStrategyFactory, RocketRestConfig
    • Method Detail

      • getType

        AuthStrategy.AuthType getType()
        Returns the auth type of this strategy.
        Returns:
        the authentication type
      • applyAuthHeaders

        RocketHeaders applyAuthHeaders​(RocketHeaders headers)
        Applies authentication headers to an existing HttpHeader object.
        Parameters:
        headers - the current HttpHeader to update
        Returns:
        the updated HttpHeader
      • needsTokenRefresh

        boolean needsTokenRefresh()
        Indicates whether this strategy needs a token refresh.
        Returns:
        true if token refresh is required
      • refreshCredentials

        boolean refreshCredentials()
        Handles refreshing the authentication credentials for strategies that support it.
        Returns:
        true if the credentials were successfully refreshed
        Throws:
        TokenRefreshException - if the refresh operation fails