Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add http signature auth template
  • Loading branch information
wing328 committed Apr 25, 2020
commit 99cdfd85f95e092deb09b7ae8f4c9cd8cd6bff63
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
if (JERSEY2_EXPERIMENTAL.equals(getLibrary())) {
supportingFiles.add(new SupportingFile("HttpSignatureAuth.mustache", authFolder, "HttpSignatureAuth.java"));
supportingFiles.add(new SupportingFile("auth/HttpSignatureAuth.mustache", authFolder, "HttpSignatureAuth.java"));
supportingFiles.add(new SupportingFile("AbstractOpenApiSchema.mustache", (sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar), "AbstractOpenApiSchema.java"));
}
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,6 @@ public class ApiClient {
* @throws ApiException API exception
*/
public <T> ApiResponse<T> invokeAPI(String operation, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType, AbstractOpenApiSchema schema) throws ApiException {
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);

// Not using `.target(targetURL).path(path)` below,
// to support (constant) query string in `path`, e.g. "/posts?draft=1"
Expand Down Expand Up @@ -934,6 +933,8 @@ public class ApiClient {
}

Entity<?> entity = serialize(body, formParams, contentType);

updateParamsForAuth(authNames, queryParams, headerParams, cookieParams, method, path);

Response response = null;

Expand Down Expand Up @@ -1061,12 +1062,14 @@ public class ApiClient {
* @param queryParams List of query parameters
* @param headerParams Map of header parameters
* @param cookieParams Map of cookie parameters
* @param method HTTP method (e.g. POST)
* @param uri HTTP URI
*/
protected void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
protected void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String method, String uri) throws ApiException {
for (String authName : authNames) {
Authentication auth = authentications.get(authName);
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
auth.applyToParams(queryParams, headerParams, cookieParams);
auth.applyToParams(queryParams, headerParams, cookieParams, method, uri);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{{>licenseInfo}}

package {{invokerPackage}}.auth;

import {{invokerPackage}}.Pair;
import {{invokerPackage}}.ApiException;

import java.util.Map;
import java.util.List;

{{>generatedAnnotation}}
public class ApiKeyAuth implements Authentication {
private final String location;
private final String paramName;

private String apiKey;
private String apiKeyPrefix;

public ApiKeyAuth(String location, String paramName) {
this.location = location;
this.paramName = paramName;
}

public String getLocation() {
return location;
}

public String getParamName() {
return paramName;
}

public String getApiKey() {
return apiKey;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

public String getApiKeyPrefix() {
return apiKeyPrefix;
}

public void setApiKeyPrefix(String apiKeyPrefix) {
this.apiKeyPrefix = apiKeyPrefix;
}

@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String method, String uri) throws ApiException {
if (apiKey == null) {
return;
}
String value;
if (apiKeyPrefix != null) {
value = apiKeyPrefix + " " + apiKey;
} else {
value = apiKey;
}
if ("query".equals(location)) {
queryParams.add(new Pair(paramName, value));
} else if ("header".equals(location)) {
headerParams.put(paramName, value);
} else if ("cookie".equals(location)) {
cookieParams.put(paramName, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{>licenseInfo}}

package {{invokerPackage}}.auth;

import {{invokerPackage}}.Pair;
import {{invokerPackage}}.ApiException;

import java.util.Map;
import java.util.List;

public interface Authentication {
/**
* Apply authentication settings to header and query params.
*
* @param queryParams List of query parameters
* @param headerParams Map of header parameters
* @param cookieParams Map of cookie parameters
*/
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String method, String uri) throws ApiException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{{>licenseInfo}}

package {{invokerPackage}}.auth;

import {{invokerPackage}}.Pair;
import {{invokerPackage}}.ApiException;

{{^java8}}
import com.migcomponents.migbase64.Base64;
{{/java8}}
{{#java8}}
import java.util.Base64;
import java.nio.charset.StandardCharsets;
{{/java8}}

import java.util.Map;
import java.util.List;

{{^java8}}
import java.io.UnsupportedEncodingException;
{{/java8}}

{{>generatedAnnotation}}
public class HttpBasicAuth implements Authentication {
private String username;
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String method, String uri) throws ApiException {
if (username == null && password == null) {
return;
}
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
{{^java8}}
try {
headerParams.put("Authorization", "Basic " + Base64.encodeToString(str.getBytes("UTF-8"), false));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
{{/java8}}
{{#java8}}
headerParams.put("Authorization", "Basic " + Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8)));
{{/java8}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{{>licenseInfo}}

package {{invokerPackage}}.auth;

import {{invokerPackage}}.Pair;
import {{invokerPackage}}.ApiException;

import java.util.Map;
import java.util.List;

{{>generatedAnnotation}}
public class HttpBearerAuth implements Authentication {
private final String scheme;
private String bearerToken;

public HttpBearerAuth(String scheme) {
this.scheme = scheme;
}

/**
* Gets the token, which together with the scheme, will be sent as the value of the Authorization header.
*
* @return The bearer token
*/
public String getBearerToken() {
return bearerToken;
}

/**
* Sets the token, which together with the scheme, will be sent as the value of the Authorization header.
*
* @param bearerToken The bearer token to send in the Authorization header
*/
public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
}

@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String method, String uri) throws ApiException {
if(bearerToken == null) {
return;
}

headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken);
}

private static String upperCaseBearer(String scheme) {
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{{>licenseInfo}}

package {{invokerPackage}}.auth;

import {{invokerPackage}}.Pair;
import {{invokerPackage}}.ApiException;

import java.io.ByteArrayInputStream;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import org.tomitribe.auth.signatures.*;

public class HttpSignatureAuth implements Authentication {

private final PrivateKey privateKey;

private Signer signer;

private Algorithm algorithm;

private String name;

public HttpSignatureAuth(String name, PrivateKey privateKey, Algorithm algorithm, String... sign) {
if (algorithm == null) {
this.algorithm = Algorithm.RSA_SHA512;
} else {
this.algorithm = algorithm;
}

if (name == null) {
this.name = "OpenApiDefault";
} else {
this.name = name;
}

if (privateKey == null) {
throw new ApiExcpetion("privateKey cannot be null when creating HttpSignatureAuth");
} else {
this.privateKey = privateKey;
}

signer = new Signer(this.privateKey, new Signature(this.name, this.algorithm, null, sign);
}

public String getSigner() {
return signer;
}

public void setSigner(String signer) {
this.signer = signer;
}

@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String method, String uri) throws ApiException {
try {
headerParams.put("Authorization", signer.sign(method, uri, headers).toString());
} catch (Exception ex) {
throw new ApiException("Failed to create signature in the HTTP request header: " + ex.toString());
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{>licenseInfo}}

package {{invokerPackage}}.auth;

import {{invokerPackage}}.Pair;
import {{invokerPackage}}.ApiException;

import java.util.Map;
import java.util.List;

{{>generatedAnnotation}}
public class OAuth implements Authentication {
private String accessToken;

public String getAccessToken() {
return accessToken;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String method, String uri) throws ApiException {
if (accessToken != null) {
headerParams.put("Authorization", "Bearer " + accessToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{>licenseInfo}}

package {{invokerPackage}}.auth;

public enum OAuthFlow {
accessCode, implicit, password, application
}
Loading