Add authentication helper methods and test cases

This commit is contained in:
xhh
2015-05-29 11:28:03 +08:00
parent 4d25d264c4
commit 6eb1a89205
3 changed files with 144 additions and 0 deletions

View File

@@ -24,4 +24,48 @@ public class Configuration {
public static Authentication getAuthentication(String authName) {
return AUTH.get(authName);
}
/** Set username for the first HTTP basic authentication. */
public static void setUsername(String username) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setUsername(username);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/** Set password for the first HTTP basic authentication. */
public static void setPassword(String password) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setPassword(password);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/** Set API key value for the first API key authentication. */
public static void setApiKey(String apiKey) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKey(apiKey);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}
/** Set API key prefix for the first API key authentication. */
public static void setApiKeyPrefix(String apiKeyPrefix) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}
}