Skip to content

Commit

Permalink
Merge pull request #57 from orkes-io/fix_client_reconnect
Browse files Browse the repository at this point in the history
Handle reconnections when getting api tokens
  • Loading branch information
v1r3n authored Dec 7, 2022
2 parents d88f478 + a960e8b commit 6955af1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 93 deletions.
102 changes: 11 additions & 91 deletions src/main/java/io/orkes/conductor/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
import io.orkes.conductor.client.http.api.TokenResourceApi;
import io.orkes.conductor.client.http.auth.ApiKeyAuth;
import io.orkes.conductor.client.http.auth.Authentication;
import io.orkes.conductor.client.http.auth.HttpBasicAuth;
import io.orkes.conductor.client.http.auth.OAuth;
import io.orkes.conductor.client.model.GenerateTokenRequest;

import com.google.common.cache.Cache;
Expand Down Expand Up @@ -339,55 +337,6 @@ public ApiClient setLenientOnJson(boolean lenientOnJson) {
return this;
}

/**
* Get authentications (key: authentication name, value: authentication).
*
* @return Map of authentication objects
*/
public Map<String, Authentication> getAuthentications() {
return authentications;
}

/**
* Get authentication for the given name.
*
* @param authName The authentication name
* @return The authentication, null if not found
*/
public Authentication getAuthentication(String authName) {
return authentications.get(authName);
}

/**
* Helper method to set username for the first HTTP basic authentication.
*
* @param username Username
*/
public void setUsername(String username) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setUsername(username);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}

/**
* Helper method to set password for the first HTTP basic authentication.
*
* @param password Password
*/
public void setPassword(String password) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setPassword(password);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}

/**
* Helper method to set API key value for the first API key authentication.
*
Expand All @@ -403,36 +352,6 @@ public void setApiKey(String apiKey) {
throw new RuntimeException("No API key authentication configured!");
}

/**
* Helper method to set API key prefix for the first API key authentication.
*
* @param apiKeyPrefix API key prefix
*/
public void setApiKeyPrefix(String apiKeyPrefix) {
for (Authentication auth : authentications.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}

/**
* Helper method to set access token for the first OAuth2 authentication.
*
* @param accessToken Access token
*/
public void setAccessToken(String accessToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setAccessToken(accessToken);
return;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}

/**
* Set the User-Agent header's value (by adding to the default header map).
*
Expand Down Expand Up @@ -1068,7 +987,9 @@ public Request buildRequest(
String[] authNames,
ProgressRequestBody.ProgressRequestListener progressRequestListener)
throws ApiException {
updateParamsForAuth(authNames, queryParams, headerParams);
if(!"/token".equalsIgnoreCase(path)) {
updateParamsForAuth(authNames, queryParams, headerParams);
}

final String url = buildUrl(path, queryParams, collectionQueryParams);
final Request.Builder reqBuilder = new Request.Builder().url(url);
Expand Down Expand Up @@ -1187,8 +1108,11 @@ public void processHeaderParams(Map<String, String> headerParams, Request.Builde
* @param queryParams List of query parameters
* @param headerParams Map of header parameters
*/
public void updateParamsForAuth(
String[] authNames, List<Pair> queryParams, Map<String, String> headerParams) {
public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams) {
if(useSecurity() && authentications.isEmpty()) {
LOGGER.debug("No authentication set, will refresh token");
refreshToken();
}
for (String authName : authNames) {
Authentication auth = authentications.get(authName);
if (auth != null) {
Expand Down Expand Up @@ -1346,19 +1270,15 @@ public String getToken() {
}

private String refreshToken() {

if (secretsManager != null) {
keyId = secretsManager.getSecret(this.ssmKeyPath);
keySecret = secretsManager.getSecret(this.ssmSecretPath);
}
if (this.keyId == null || this.keySecret == null) {
throw new RuntimeException(
"KeyId and KeySecret must be set in order to get an authentication token");
throw new RuntimeException("KeyId and KeySecret must be set in order to get an authentication token");
}
GenerateTokenRequest generateTokenRequest =
new GenerateTokenRequest().keyId(this.keyId).keySecret(this.keySecret);
Map<String, String> response =
TokenResourceApi.generateTokenWithHttpInfo(this, generateTokenRequest).getData();
GenerateTokenRequest generateTokenRequest = new GenerateTokenRequest().keyId(this.keyId).keySecret(this.keySecret);
Map<String, String> response = TokenResourceApi.generateTokenWithHttpInfo(this, generateTokenRequest).getData();
String token = response.get("token");
this.setApiKeyHeader(token);
return token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ private static com.squareup.okhttp.Call generateTokenValidateBeforeCall(
*/
public static ApiResponse<Map<String, String>> generateTokenWithHttpInfo(
ApiClient apiClient, GenerateTokenRequest generateTokenRequest) throws ApiException {
com.squareup.okhttp.Call call =
generateTokenValidateBeforeCall(apiClient, generateTokenRequest, null, null);
com.squareup.okhttp.Call call = generateTokenValidateBeforeCall(apiClient, generateTokenRequest, null, null);
Type localVarReturnType = new TypeToken<Map<String, String>>() {}.getType();
return apiClient.execute(call, localVarReturnType);
}
Expand Down

0 comments on commit 6955af1

Please sign in to comment.