Skip to content

Commit 5ca27ee

Browse files
stache3000purejava
authored andcommitted
upd: to take the proposed implementation to set the EU servers URLs
1 parent f457f8e commit 5ca27ee

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

src/main/java/org/purejava/integrations/keychain/BitwardenAccess.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,22 @@ public class BitwardenAccess implements KeychainAccessProvider {
2424
private final String stateFile;
2525
private boolean isSupported = false;
2626
private final String boID;
27-
private final String apiUrl = "https://api.bitwarden.com";
28-
private final String identityUrl = "https://identity.bitwarden.com";
27+
private final String apiUrl;
28+
private final String identityUrl;
2929
private final String APP_NAME = "Cryptomator";
30-
private final String envApiUrl;
31-
private final String envIdentityUrl;
3230

3331
public BitwardenAccess() {
3432
this.accessToken = System.getenv("BITWARDEN_ACCESS_TOKEN");
3533
this.boID = System.getenv("BITWARDEN_ORGANIZATION_ID");
3634
this.stateFile = System.getenv("BITWARDEN_STATE_FILE");
37-
this.envApiUrl = System.getenv("BITWARDEN_API_URL");
38-
this.envIdentityUrl = System.getenv("BITWARDEN_IDENTITY_URL");
3935

40-
if (isEnvVarValid(envApiUrl) && isEnvVarValid(envIdentityUrl)) {
41-
this.apiUrl = envApiUrl;
42-
this.identityUrl = envIdentityUrl;
43-
}
36+
var envApiUrl = System.getenv("BITWARDEN_API_URL");
37+
var envIdentityUrl = System.getenv("BITWARDEN_IDENTITY_URL");
38+
39+
var endpoint = resolveEndpoint(envApiUrl, envIdentityUrl);
40+
41+
this.apiUrl = endpoint.apiUrl;
42+
this.identityUrl = endpoint.identityUrl;
4443

4544
if (isEnvVarValid(accessToken) && isEnvVarValid(boID)) {
4645
try {
@@ -164,6 +163,47 @@ private Optional<SecretIdentifierResponse> getSecret(String vault) throws Bitwar
164163
.findFirst();
165164
}
166165

166+
/**
167+
* Resolve the Bitwarden endpoint based on the provided environment variables.
168+
* @param apiUrlEnv The API URL environment variable.
169+
* @param identityUrlEnv The Identity URL environment variable.
170+
* @return The resolved Bitwarden endpoint.
171+
*/
172+
private BitwardenEndpoint resolveEndpoint(String apiUrlEnv, String identityUrlEnv) {
173+
if (isEnvVarValid(apiUrlEnv) && isEnvVarValid(identityUrlEnv)) {
174+
for (BitwardenEndpoint endpoint : BitwardenEndpoint.values()) {
175+
if (endpoint.apiUrl.equals(apiUrlEnv) && endpoint.identityUrl.equals(identityUrlEnv)) {
176+
return endpoint;
177+
}
178+
}
179+
LOG.warn("Provided API/Identity URLs are invalid. Falling back to default (US).");
180+
} else {
181+
LOG.info("API/Identity URLs not set. Falling back to default (US).");
182+
}
183+
return BitwardenEndpoint.US;
184+
}
185+
186+
/**
187+
* Enum to represent the Bitwarden endpoints.
188+
*/
189+
enum BitwardenEndpoint {
190+
US("https://api.bitwarden.com", "https://identity.bitwarden.com"),
191+
EU("https://api.bitwarden.eu", "https://identity.bitwarden.eu");
192+
193+
private final String apiUrl;
194+
private final String identityUrl;
195+
196+
BitwardenEndpoint(String apiUrl, String identityUrl) {
197+
this.apiUrl = apiUrl;
198+
this.identityUrl = identityUrl;
199+
}
200+
}
201+
202+
/**
203+
* Check if the given environment variable is valid.
204+
* @param var The environment variable to check.
205+
* @return true if the variable is valid, false otherwise.
206+
*/
167207
private boolean isEnvVarValid(String var) {
168208
return null != var && !var.isEmpty() && !var.isBlank();
169209
}

0 commit comments

Comments
 (0)