Package com.guinetik.rr.auth
Class BearerTokenStrategy
- java.lang.Object
-
- com.guinetik.rr.auth.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)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.guinetik.rr.auth.AuthStrategy
AuthStrategy.AuthType
-
-
Constructor Summary
Constructors Constructor Description BearerTokenStrategy(String token)Creates a new BearerTokenStrategy with no custom refresh logic.BearerTokenStrategy(String token, BooleanSupplier refreshTokenLogic)Creates a new BearerTokenStrategy with custom token refresh logic.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RocketHeadersapplyAuthHeaders(RocketHeaders headers)Applies authentication headers to an existing HttpHeader object.AuthStrategy.AuthTypegetType()Returns the auth type of this strategy.booleanneedsTokenRefresh()Indicates whether this strategy needs a token refresh.booleanrefreshCredentials()Handles refreshing the authentication credentials for strategies that support it.
-
-
-
Constructor Detail
-
BearerTokenStrategy
public BearerTokenStrategy(String token)
Creates a new BearerTokenStrategy with no custom refresh logic. In this case,refreshCredentials()will always returnfalse.- 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 tokenrefreshTokenLogic- aBooleanSupplierthat will be invoked byrefreshCredentials(). It should returntrueif the token was successfully refreshed,falseotherwise.
-
-
Method Detail
-
getType
public AuthStrategy.AuthType getType()
Description copied from interface:AuthStrategyReturns the auth type of this strategy.- Specified by:
getTypein interfaceAuthStrategy- Returns:
- the authentication type
-
applyAuthHeaders
public RocketHeaders applyAuthHeaders(RocketHeaders headers)
Description copied from interface:AuthStrategyApplies authentication headers to an existing HttpHeader object.- Specified by:
applyAuthHeadersin interfaceAuthStrategy- Parameters:
headers- the current HttpHeader to update- Returns:
- the updated HttpHeader
-
needsTokenRefresh
public boolean needsTokenRefresh()
Description copied from interface:AuthStrategyIndicates whether this strategy needs a token refresh.- Specified by:
needsTokenRefreshin interfaceAuthStrategy- Returns:
- true if token refresh is required
-
refreshCredentials
public boolean refreshCredentials()
Handles refreshing the authentication credentials for strategies that support it.If a custom
refreshTokenLogicwas 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:
refreshCredentialsin interfaceAuthStrategy- Returns:
- true if the credentials were successfully refreshed
-
-