[java] Support aliasing of API keys (#4966)

* [java] Support aliasing of API keys

* Rebuild Java Jersey2 sample client

* x-lookup to x-auth-id-alias

* Regenerated
This commit is contained in:
Jiri Kuncar 2020-03-01 09:33:18 +01:00 committed by GitHub
parent 35e90a553d
commit a03f7a58c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 0 deletions

View File

@ -144,6 +144,7 @@ public class ApiClient {
protected String tempFolderPath = null;
protected Map<String, Authentication> authentications;
protected Map<String, String> authenticationLookup;
protected DateFormat dateFormat;
@ -164,6 +165,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-auth-id-alias}}
authenticationLookup.put("{{name}}", "{{.}}");{{/vendorExtensions.x-auth-id-alias}}{{/authMethods}}
}
/**
@ -279,6 +284,25 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}
/**
* Helper method to configure authentications which respects aliases of API keys.
*
* @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-auth-id-alias 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

View File

@ -80,6 +80,7 @@ public class ApiClient {
protected String tempFolderPath = null;
protected Map<String, Authentication> authentications;
protected Map<String, String> authenticationLookup;
protected DateFormat dateFormat;
@ -100,6 +101,9 @@ public class 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>();
}
/**
@ -215,6 +219,25 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}
/**
* Helper method to configure authentications which respects aliases of API keys.
*
* @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-auth-id-alias 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

View File

@ -81,6 +81,7 @@ public class ApiClient {
protected String tempFolderPath = null;
protected Map<String, Authentication> authentications;
protected Map<String, String> authenticationLookup;
protected DateFormat dateFormat;
@ -101,6 +102,9 @@ public class 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>();
}
/**
@ -216,6 +220,25 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}
/**
* Helper method to configure authentications which respects aliases of API keys.
*
* @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-auth-id-alias 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

View File

@ -81,6 +81,7 @@ public class ApiClient {
protected String tempFolderPath = null;
protected Map<String, Authentication> authentications;
protected Map<String, String> authenticationLookup;
protected DateFormat dateFormat;
@ -101,6 +102,9 @@ public class 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>();
}
/**
@ -216,6 +220,25 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}
/**
* Helper method to configure authentications which respects aliases of API keys.
*
* @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-auth-id-alias 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