forked from loafle/openapi-generator-original
[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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user