Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Support aliasing of API keys #4966

Merged
merged 5 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class ApiClient {
protected String tempFolderPath = null;

protected Map<String, Authentication> authentications;
protected Map<String, String> authenticationLookup;

protected DateFormat dateFormat;

Expand All @@ -93,6 +94,10 @@ public class ApiClient {
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);

// Setup authentication lookup (key: authentication alias, value: authentication name)
authenticationLookup = new HashMap<String, String>();{{#authMethods}}{{#vendorExtensions.x-lookup}}
authenticationLookup.put("{{name}}", "{{.}}");{{/vendorExtensions.x-lookup}}{{/authMethods}}
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -181,6 +186,28 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}

/**
* Helper method to configure authentications.
*
* NOTE: This method respects API key aliases using "x-lookup" property
* from OpenAPI specification.
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
*
* @param secrets Hash map from authentication name to its secret.
*/
public void configureApiKeys(HashMap<String, String> secrets) {
for (Map.Entry<String, Authentication> authEntry : authentications.entrySet()) {
Authentication auth = authEntry.getValue();
if (auth instanceof ApiKeyAuth) {
String name = authEntry.getKey();
// respect x-lookup property
name = authenticationLookup.getOrDefault(name, name);
if (secrets.containsKey(name)) {
((ApiKeyAuth) auth).setApiKey(secrets.get(name));
}
}
}
}

/**
* Helper method to set API key prefix for the first API key authentication.
* @param apiKeyPrefix API key prefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class ApiClient {
protected String tempFolderPath = null;

protected Map<String, Authentication> authentications;
protected Map<String, String> authenticationLookup;

protected DateFormat dateFormat;

Expand All @@ -85,6 +86,9 @@ public ApiClient() {
authentications.put("petstore_auth", new OAuth());
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);

// Setup authentication lookup (key: authentication alias, value: authentication name)
authenticationLookup = new HashMap<String, String>();
}

/**
Expand Down Expand Up @@ -173,6 +177,28 @@ public void setApiKey(String apiKey) {
throw new RuntimeException("No API key authentication configured!");
}

/**
* Helper method to configure authentications.
*
* NOTE: This method respects API key aliases using "x-lookup" property
* from OpenAPI specification.
*
* @param secrets Hash map from authentication name to its secret.
*/
public void configureApiKeys(HashMap<String, String> secrets) {
for (Map.Entry<String, Authentication> authEntry : authentications.entrySet()) {
Authentication auth = authEntry.getValue();
if (auth instanceof ApiKeyAuth) {
String name = authEntry.getKey();
// respect x-lookup property
name = authenticationLookup.getOrDefault(name, name);
if (secrets.containsKey(name)) {
((ApiKeyAuth) auth).setApiKey(secrets.get(name));
}
}
}
}

/**
* Helper method to set API key prefix for the first API key authentication.
* @param apiKeyPrefix API key prefix
Expand Down