Class BearerTokenStrategy

  • All Implemented Interfaces:
    AuthStrategy

    public class BearerTokenStrategy
    extends Object
    implements AuthStrategy
    Authentication strategy that uses Bearer token authentication.

    This strategy adds an Authorization: Bearer <token> header to all requests. It's suitable for APIs that use API keys, JWT tokens, or other bearer-style authentication.

    Basic Usage

    
     // Create via factory (recommended)
     AuthStrategy auth = AuthStrategyFactory.createBearerToken("my-api-token");
    
     // Configure client
     RocketRestConfig config = RocketRestConfig.builder("https://api.example.com")
         .authStrategy(auth)
         .build();
     

    With Custom Refresh Logic

    For tokens that expire, you can provide custom refresh logic:

    
     AtomicReference<String> tokenRef = new AtomicReference<>("initial-token");
    
     AuthStrategy auth = AuthStrategyFactory.createBearerToken(tokenRef.get(), () -> {
         try {
             String newToken = myAuthService.refreshToken();
             tokenRef.set(newToken);
             return true;
         } catch (Exception e) {
             return false;
         }
     });
     

    For OAuth Tokens

    If your bearer token comes from an OAuth flow, consider using the dedicated OAuth strategies which handle token refresh automatically:

    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    AuthStrategy, AuthStrategyFactory.createBearerToken(String)
    • Constructor Detail

      • BearerTokenStrategy

        public BearerTokenStrategy​(String token)
        Creates a new BearerTokenStrategy with no custom refresh logic. In this case, refreshCredentials() will always return false.
        Parameters:
        token - the bearer token
      • BearerTokenStrategy

        public BearerTokenStrategy​(String token,
                                   BooleanSupplier refreshTokenLogic)
        Creates a new BearerTokenStrategy with custom token refresh logic.
        Parameters:
        token - the bearer token
        refreshTokenLogic - a BooleanSupplier that will be invoked by refreshCredentials(). It should return true if the token was successfully refreshed, false otherwise.
    • Method Detail

      • applyAuthHeaders

        public RocketHeaders applyAuthHeaders​(RocketHeaders headers)
        Description copied from interface: AuthStrategy
        Applies authentication headers to an existing HttpHeader object.
        Specified by:
        applyAuthHeaders in interface AuthStrategy
        Parameters:
        headers - the current HttpHeader to update
        Returns:
        the updated HttpHeader
      • needsTokenRefresh

        public boolean needsTokenRefresh()
        Description copied from interface: AuthStrategy
        Indicates whether this strategy needs a token refresh.
        Specified by:
        needsTokenRefresh in interface AuthStrategy
        Returns:
        true if token refresh is required
      • refreshCredentials

        public boolean refreshCredentials()
        Handles refreshing the authentication credentials for strategies that support it.

        If a custom refreshTokenLogic was provided during construction, this method will invoke it and return its result.

        If no custom logic was provided, this method always returns false, as this strategy itself does not handle credential refresh.

        Specified by:
        refreshCredentials in interface AuthStrategy
        Returns:
        true if the credentials were successfully refreshed