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:
parent
35e90a553d
commit
a03f7a58c3
@ -144,6 +144,7 @@ public class ApiClient {
|
|||||||
protected String tempFolderPath = null;
|
protected String tempFolderPath = null;
|
||||||
|
|
||||||
protected Map<String, Authentication> authentications;
|
protected Map<String, Authentication> authentications;
|
||||||
|
protected Map<String, String> authenticationLookup;
|
||||||
|
|
||||||
protected DateFormat dateFormat;
|
protected DateFormat dateFormat;
|
||||||
|
|
||||||
@ -164,6 +165,10 @@ public class ApiClient {
|
|||||||
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
|
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
|
||||||
// Prevent the authentications from being modified.
|
// Prevent the authentications from being modified.
|
||||||
authentications = Collections.unmodifiableMap(authentications);
|
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!");
|
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.
|
* Helper method to set API key prefix for the first API key authentication.
|
||||||
* @param apiKeyPrefix API key prefix
|
* @param apiKeyPrefix API key prefix
|
||||||
|
@ -80,6 +80,7 @@ public class ApiClient {
|
|||||||
protected String tempFolderPath = null;
|
protected String tempFolderPath = null;
|
||||||
|
|
||||||
protected Map<String, Authentication> authentications;
|
protected Map<String, Authentication> authentications;
|
||||||
|
protected Map<String, String> authenticationLookup;
|
||||||
|
|
||||||
protected DateFormat dateFormat;
|
protected DateFormat dateFormat;
|
||||||
|
|
||||||
@ -100,6 +101,9 @@ public class ApiClient {
|
|||||||
authentications.put("petstore_auth", new OAuth());
|
authentications.put("petstore_auth", new OAuth());
|
||||||
// Prevent the authentications from being modified.
|
// Prevent the authentications from being modified.
|
||||||
authentications = Collections.unmodifiableMap(authentications);
|
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!");
|
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.
|
* Helper method to set API key prefix for the first API key authentication.
|
||||||
* @param apiKeyPrefix API key prefix
|
* @param apiKeyPrefix API key prefix
|
||||||
|
@ -81,6 +81,7 @@ public class ApiClient {
|
|||||||
protected String tempFolderPath = null;
|
protected String tempFolderPath = null;
|
||||||
|
|
||||||
protected Map<String, Authentication> authentications;
|
protected Map<String, Authentication> authentications;
|
||||||
|
protected Map<String, String> authenticationLookup;
|
||||||
|
|
||||||
protected DateFormat dateFormat;
|
protected DateFormat dateFormat;
|
||||||
|
|
||||||
@ -101,6 +102,9 @@ public class ApiClient {
|
|||||||
authentications.put("petstore_auth", new OAuth());
|
authentications.put("petstore_auth", new OAuth());
|
||||||
// Prevent the authentications from being modified.
|
// Prevent the authentications from being modified.
|
||||||
authentications = Collections.unmodifiableMap(authentications);
|
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!");
|
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.
|
* Helper method to set API key prefix for the first API key authentication.
|
||||||
* @param apiKeyPrefix API key prefix
|
* @param apiKeyPrefix API key prefix
|
||||||
|
@ -81,6 +81,7 @@ public class ApiClient {
|
|||||||
protected String tempFolderPath = null;
|
protected String tempFolderPath = null;
|
||||||
|
|
||||||
protected Map<String, Authentication> authentications;
|
protected Map<String, Authentication> authentications;
|
||||||
|
protected Map<String, String> authenticationLookup;
|
||||||
|
|
||||||
protected DateFormat dateFormat;
|
protected DateFormat dateFormat;
|
||||||
|
|
||||||
@ -101,6 +102,9 @@ public class ApiClient {
|
|||||||
authentications.put("petstore_auth", new OAuth());
|
authentications.put("petstore_auth", new OAuth());
|
||||||
// Prevent the authentications from being modified.
|
// Prevent the authentications from being modified.
|
||||||
authentications = Collections.unmodifiableMap(authentications);
|
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!");
|
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.
|
* Helper method to set API key prefix for the first API key authentication.
|
||||||
* @param apiKeyPrefix API key prefix
|
* @param apiKeyPrefix API key prefix
|
||||||
|
Loading…
x
Reference in New Issue
Block a user