Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 20afa87

Browse files
sebastien-rossetjimschubert
authored andcommitted
[CORE] Add support for HTTP signature (OpenAPITools#4993)
* Add support for HTTP signature * Add http-signature security scheme * add http_signature_test to security scheme * Add separate OAS file with support for HTTP signature * change URL of apache license to use https * add log warning to indicate the 'http signature' security scheme is still a draft
1 parent d8c9f25 commit 20afa87

File tree

5 files changed

+1818
-4
lines changed

5 files changed

+1818
-4
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenSecurity.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ public class CodegenSecurity {
2929
public String type;
3030
public String scheme;
3131
public Boolean hasMore, isBasic, isOAuth, isApiKey;
32-
// is Basic is true for all http authentication type. Those are to differentiate basic and bearer authentication
33-
public Boolean isBasicBasic, isBasicBearer;
32+
// is Basic is true for all http authentication type.
33+
// Those are to differentiate basic and bearer authentication
34+
// isHttpSignature is to support HTTP signature authorization scheme.
35+
// https://datatracker.ietf.org/doc/draft-cavage-http-signatures/
36+
public Boolean isBasicBasic, isBasicBearer, isHttpSignature;
3437
public String bearerFormat;
3538
public Map<String, Object> vendorExtensions = new HashMap<String, Object>();
3639
// ApiKey specific
@@ -50,6 +53,7 @@ public CodegenSecurity filterByScopeNames(List<String> filterScopes) {
5053
filteredSecurity.hasMore = false;
5154
filteredSecurity.isBasic = isBasic;
5255
filteredSecurity.isBasicBasic = isBasicBasic;
56+
filteredSecurity.isHttpSignature = isHttpSignature;
5357
filteredSecurity.isBasicBearer = isBasicBearer;
5458
filteredSecurity.isApiKey = isApiKey;
5559
filteredSecurity.isOAuth = isOAuth;
@@ -97,6 +101,7 @@ public boolean equals(Object o) {
97101
Objects.equals(isOAuth, that.isOAuth) &&
98102
Objects.equals(isApiKey, that.isApiKey) &&
99103
Objects.equals(isBasicBasic, that.isBasicBasic) &&
104+
Objects.equals(isHttpSignature, that.isHttpSignature) &&
100105
Objects.equals(isBasicBearer, that.isBasicBearer) &&
101106
Objects.equals(bearerFormat, that.bearerFormat) &&
102107
Objects.equals(vendorExtensions, that.vendorExtensions) &&
@@ -117,8 +122,9 @@ public boolean equals(Object o) {
117122
@Override
118123
public int hashCode() {
119124

120-
return Objects.hash(name, type, scheme, hasMore, isBasic, isOAuth, isApiKey, isBasicBasic, isBasicBearer,
121-
bearerFormat, vendorExtensions, keyParamName, isKeyInQuery, isKeyInHeader, isKeyInCookie, flow,
125+
return Objects.hash(name, type, scheme, hasMore, isBasic, isOAuth, isApiKey,
126+
isBasicBasic, isHttpSignature, isBasicBearer, bearerFormat, vendorExtensions,
127+
keyParamName, isKeyInQuery, isKeyInHeader, isKeyInCookie, flow,
122128
authorizationUrl, tokenUrl, scopes, isCode, isPassword, isApplication, isImplicit);
123129
}
124130

@@ -133,6 +139,7 @@ public String toString() {
133139
sb.append(", isOAuth=").append(isOAuth);
134140
sb.append(", isApiKey=").append(isApiKey);
135141
sb.append(", isBasicBasic=").append(isBasicBasic);
142+
sb.append(", isHttpSignature=").append(isHttpSignature);
136143
sb.append(", isBasicBearer=").append(isBasicBearer);
137144
sb.append(", bearerFormat='").append(bearerFormat).append('\'');
138145
sb.append(", vendorExtensions=").append(vendorExtensions);

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

+9
Original file line numberDiff line numberDiff line change
@@ -3617,6 +3617,7 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> securitySc
36173617
cs.name = key;
36183618
cs.type = securityScheme.getType().toString();
36193619
cs.isCode = cs.isPassword = cs.isApplication = cs.isImplicit = false;
3620+
cs.isHttpSignature = false;
36203621
cs.isBasicBasic = cs.isBasicBearer = false;
36213622
cs.scheme = securityScheme.getScheme();
36223623
if (securityScheme.getExtensions() != null) {
@@ -3638,6 +3639,14 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> securitySc
36383639
} else if ("bearer".equals(securityScheme.getScheme())) {
36393640
cs.isBasicBearer = true;
36403641
cs.bearerFormat = securityScheme.getBearerFormat();
3642+
} else if ("signature".equals(securityScheme.getScheme())) {
3643+
// HTTP signature as defined in https://datatracker.ietf.org/doc/draft-cavage-http-signatures/
3644+
// The registry of security schemes is maintained by IANA.
3645+
// https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml
3646+
// As of January 2020, the "signature" scheme has not been registered with IANA yet.
3647+
// This scheme may have to be changed when it is officially registered with IANA.
3648+
cs.isHttpSignature = true;
3649+
LOGGER.warn("Security scheme 'HTTP signature' is a draft IETF RFC and subject to change.");
36413650
}
36423651
} else if (SecurityScheme.Type.OAUTH2.equals(securityScheme.getType())) {
36433652
cs.isKeyInHeader = cs.isKeyInQuery = cs.isKeyInCookie = cs.isApiKey = cs.isBasic = false;

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.openapitools.codegen.templating.MustacheEngineAdapter;
4343
import org.openapitools.codegen.utils.ImplementationVersion;
4444
import org.openapitools.codegen.utils.ModelUtils;
45+
import org.openapitools.codegen.utils.ProcessUtils;
4546
import org.openapitools.codegen.utils.URLPathUtils;
4647
import org.slf4j.Logger;
4748
import org.slf4j.LoggerFactory;
@@ -851,6 +852,9 @@ private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, L
851852
if (hasBearerMethods(authMethods)) {
852853
bundle.put("hasBearerMethods", true);
853854
}
855+
if (ProcessUtils.hasHttpSignatureMethods(authMethods)) {
856+
bundle.put("hasHttpSignatureMethods", true);
857+
}
854858
}
855859

856860
List<CodegenServer> servers = config.fromServers(openAPI.getServers());

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java

+18
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,22 @@ public static boolean hasBearerMethods(Map<String, Object> objs) {
9494
return false;
9595
}
9696

97+
/**
98+
* Returns true if the specified OAS model has at least one operation with the HTTP signature
99+
* security scheme.
100+
* The HTTP signature scheme is defined in https://datatracker.ietf.org/doc/draft-cavage-http-signatures/
101+
*
102+
* @param authMethods List of auth methods.
103+
* @return True if at least one operation has HTTP signature security schema defined
104+
*/
105+
public static boolean hasHttpSignatureMethods(List<CodegenSecurity> authMethods) {
106+
if (authMethods != null && !authMethods.isEmpty()) {
107+
for (CodegenSecurity cs : authMethods) {
108+
if (Boolean.TRUE.equals(cs.isHttpSignature)) {
109+
return true;
110+
}
111+
}
112+
}
113+
return false;
114+
}
97115
}

0 commit comments

Comments
 (0)