Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ switcher.poolsize=2
| `switcher.environment` | ❌ | default | Environment name (dev, staging, default) |
| `switcher.local` | ❌ | false | Enable local-only mode |
| `switcher.check` | ❌ | false | Validate switcher keys on startup |
| `switcher.autorefreshtoken` | ❌ | false | Automatically refresh API token before expiration |
| `switcher.auth.autorefresh` | ❌ | false | Automatically refresh API token before expiration |
| `switcher.relay.restrict` | ❌ | true | Defines if client will trigger local snapshot relay verification |
| `switcher.snapshot.location` | ❌ | - | Directory for snapshot files |
| `switcher.snapshot.auto` | ❌ | false | Auto-load snapshots on startup |
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/switcherapi/client/ContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ public ContextBuilder poolConnectionSize(Integer poolSize) {
return this;
}

public ContextBuilder autoRefreshToken(boolean autoRefreshToken) {
switcherProperties.setValue(ContextKey.AUTO_REFRESH_TOKEN, autoRefreshToken);
/**
* @param authAutoRefresh true/false When true, it will enable automatic refresh of authentication token
* @return ContextBuilder
*/
public ContextBuilder authAutoRefresh(boolean authAutoRefresh) {
switcherProperties.setValue(ContextKey.AUTH_AUTO_REFRESH, authAutoRefresh);
return this;
}
}
24 changes: 20 additions & 4 deletions src/main/java/com/switcherapi/client/SwitcherConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ abstract class SwitcherConfig {

protected boolean local;
protected boolean check;
protected boolean autoRefreshToken;
protected String silent;
protected Integer timeout;
protected Integer poolSize;
protected AuthConfig auth;
protected RelayConfig relay;
protected SnapshotConfig snapshot;
protected TruststoreConfig truststore;

SwitcherConfig() {
this.auth = new AuthConfig();
this.relay = new RelayConfig();
this.snapshot = new SnapshotConfig();
this.truststore = new TruststoreConfig();
Expand All @@ -39,11 +40,14 @@ protected void updateSwitcherConfig(SwitcherProperties properties) {
setEnvironment(properties.getValue(ContextKey.ENVIRONMENT));
setLocal(properties.getBoolean(ContextKey.LOCAL_MODE));
setCheck(properties.getBoolean(ContextKey.CHECK_SWITCHERS));
setAutoRefreshToken(properties.getBoolean(ContextKey.AUTO_REFRESH_TOKEN));
setSilent(properties.getValue(ContextKey.SILENT_MODE));
setTimeout(properties.getInt(ContextKey.TIMEOUT_MS));
setPoolSize(properties.getInt(ContextKey.POOL_CONNECTION_SIZE));

AuthConfig authConfig = new AuthConfig();
authConfig.setAutoRefresh(properties.getBoolean(ContextKey.AUTH_AUTO_REFRESH));
setAuth(authConfig);

RelayConfig relayConfig = new RelayConfig();
relayConfig.setRestrict(properties.getBoolean(ContextKey.RESTRICT_RELAY));
setRelay(relayConfig);
Expand Down Expand Up @@ -107,8 +111,8 @@ public void setCheck(boolean check) {
this.check = check;
}

public void setAutoRefreshToken(boolean autoRefreshToken) {
this.autoRefreshToken = autoRefreshToken;
public void setAuth(AuthConfig auth) {
this.auth = auth;
}

public void setSilent(String silent) {
Expand All @@ -134,6 +138,18 @@ public void setTruststore(TruststoreConfig truststore) {
this.truststore = truststore;
}

public static class AuthConfig {
private boolean autoRefresh;

public boolean isAutoRefresh() {
return autoRefresh;
}

public void setAutoRefresh(boolean autoRefresh) {
this.autoRefresh = autoRefresh;
}
}

public static class RelayConfig {
private boolean restrict;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected void configureClient() {
.restrictRelay(relay.isRestrict())
.silentMode(silent)
.timeoutMs(timeout)
.autoRefreshToken(autoRefreshToken)
.authAutoRefresh(auth.isAutoRefresh())
.poolConnectionSize(poolSize)
.snapshotLocation(snapshot.getLocation())
.snapshotAutoLoad(snapshot.isAuto())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private void initDefaults() {
setValue(ContextKey.LOCAL_MODE, false);
setValue(ContextKey.CHECK_SWITCHERS, false);
setValue(ContextKey.RESTRICT_RELAY, true);
setValue(ContextKey.AUTO_REFRESH_TOKEN, false);
setValue(ContextKey.AUTH_AUTO_REFRESH, false);
}

@Override
Expand All @@ -50,7 +50,7 @@ public void loadFromProperties(Properties prop) {
setValue(ContextKey.LOCAL_MODE, getBoolDefault(resolveProperties(ContextKey.LOCAL_MODE.getParam(), prop), false));
setValue(ContextKey.CHECK_SWITCHERS, getBoolDefault(resolveProperties(ContextKey.CHECK_SWITCHERS.getParam(), prop), false));
setValue(ContextKey.RESTRICT_RELAY, getBoolDefault(resolveProperties(ContextKey.RESTRICT_RELAY.getParam(), prop), true));
setValue(ContextKey.AUTO_REFRESH_TOKEN, getBoolDefault(resolveProperties(ContextKey.AUTO_REFRESH_TOKEN.getParam(), prop), false));
setValue(ContextKey.AUTH_AUTO_REFRESH, getBoolDefault(resolveProperties(ContextKey.AUTH_AUTO_REFRESH.getParam(), prop), false));
setValue(ContextKey.REGEX_TIMEOUT, getIntDefault(resolveProperties(ContextKey.REGEX_TIMEOUT.getParam(), prop), DEFAULT_REGEX_TIMEOUT));
setValue(ContextKey.TRUSTSTORE_PATH, resolveProperties(ContextKey.TRUSTSTORE_PATH.getParam(), prop));
setValue(ContextKey.TRUSTSTORE_PASSWORD, resolveProperties(ContextKey.TRUSTSTORE_PASSWORD.getParam(), prop));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/switcherapi/client/model/ContextKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public enum ContextKey {
/**
* (boolean) Enables automatic refresh of authentication token (default is false)
*/
AUTO_REFRESH_TOKEN("switcher.autorefreshtoken");
AUTH_AUTO_REFRESH("switcher.auth.autorefresh");

private final String param;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void scheduleNextAuth() {
}

private boolean isAutoRefreshable() {
return switcherProperties.getBoolean(ContextKey.AUTO_REFRESH_TOKEN) &&
return switcherProperties.getBoolean(ContextKey.AUTH_AUTO_REFRESH) &&
(Objects.isNull(refreshFuture) || refreshFuture.isDone());
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/switcherapi/client/SwitcherConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ void shouldInitializeClientFromSwitcherConfig_Minimal() {

private <T extends SwitcherConfig> T buildSwitcherClientConfig(T classConfig, String component,
String domain) {
SwitcherConfig.AuthConfig auth = new SwitcherConfig.AuthConfig();
auth.setAutoRefresh(false);

SwitcherConfig.RelayConfig relay = new SwitcherConfig.RelayConfig();
relay.setRestrict(false);

Expand All @@ -61,6 +64,7 @@ private <T extends SwitcherConfig> T buildSwitcherClientConfig(T classConfig, St
classConfig.setSilent("5m");
classConfig.setTimeout(3000);
classConfig.setPoolSize(2);
classConfig.setAuth(auth);
classConfig.setRelay(relay);
classConfig.setSnapshot(snapshot);
classConfig.setTruststore(truststore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SwitcherRemoteAutoRefreshTokenTest extends MockWebServerHelper {
class SwitcherRemoteAuthAutoRefreshTest extends MockWebServerHelper {

@BeforeEach
void setup() throws IOException {
Expand All @@ -40,7 +40,7 @@ void shouldAutoRefreshAuthToken() {
givenResponse(generateCriteriaResponse("true", false));

//when
givenAutoRefreshToken(true);
givenAuthAutoRefresh(true);

//test
SwitcherRequest switcher = SwitchersBase.getSwitcher(SwitchersBase.USECASE11);
Expand All @@ -58,7 +58,7 @@ void shouldNotAutoRefreshAuthTokenWhenDisabled() {
givenResponse(generateCriteriaResponse("true", false));

//when
givenAutoRefreshToken(false);
givenAuthAutoRefresh(false);

//test
SwitcherRequest switcher = SwitchersBase.getSwitcher(SwitchersBase.USECASE11);
Expand All @@ -75,7 +75,7 @@ void shouldNotAutoRefreshAuthTokenWhenAuthFails() {
givenResponse(generateStatusResponse("500"));

//when
givenAutoRefreshToken(true);
givenAuthAutoRefresh(true);

//test
SwitcherRequest switcher = SwitchersBase.getSwitcher(SwitchersBase.USECASE11);
Expand All @@ -84,14 +84,14 @@ void shouldNotAutoRefreshAuthTokenWhenAuthFails() {
assertThrows(SwitcherRemoteException.class, switcher::isItOn);
}

private void givenAutoRefreshToken(boolean enabled) {
private void givenAuthAutoRefresh(boolean enabled) {
SwitchersBase.configure(ContextBuilder.builder(true)
.context(SwitchersBase.class.getName())
.url(String.format("http://localhost:%s", mockBackEnd.getPort()))
.domain("domain")
.apiKey("apiKey")
.component("component")
.autoRefreshToken(enabled));
.authAutoRefresh(enabled));

SwitchersBase.initializeClient();
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/switcherapi/playground/Features.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected void configureClient() {
.apiKey(System.getenv("switcher.api.key"))
.component(System.getenv("switcher.component"))
.domain(System.getenv("switcher.domain"))
.autoRefreshToken(true));
.authAutoRefresh(true));

initializeClient();
}
Expand Down
Loading