Skip to content

Commit 77d6c04

Browse files
[Java] Enhancements for HTTP message signature (#6090)
* Add code comments * Change 'setup' method to setPrivateKey * Add support for configuring digest algorithm * run script in bin directory * format generated code * Revert "format generated code" This reverts commit 3b52778.
1 parent 03c3c64 commit 77d6c04

75 files changed

Lines changed: 4203 additions & 3624 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/resources/Java/libraries/jersey2-experimental/auth/HttpSignatureAuth.mustache

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,110 @@ import java.util.List;
1919

2020
import org.tomitribe.auth.signatures.*;
2121

22+
/**
23+
* A Configuration object for the HTTP message signature security scheme.
24+
*/
2225
public class HttpSignatureAuth implements Authentication {
2326
2427
private Signer signer;
2528
26-
private String name;
29+
// An opaque string that the server can use to look up the component they need to validate the signature.
30+
private String keyId;
2731
32+
// The HTTP signature algorithm.
2833
private Algorithm algorithm;
2934
35+
// The list of HTTP headers that should be included in the HTTP signature.
3036
private List<String> headers;
3137
32-
public HttpSignatureAuth(String name, Algorithm algorithm, List<String> headers) {
33-
this.name = name;
38+
// The digest algorithm which is used to calculate a cryptographic digest of the HTTP request body.
39+
private String digestAlgorithm;
40+
41+
/**
42+
* Construct a new HTTP signature auth configuration object.
43+
*
44+
* @param keyId An opaque string that the server can use to look up the component they need to validate the signature.
45+
* @param algorithm The signature algorithm.
46+
* @param headers The list of HTTP headers that should be included in the HTTP signature.
47+
*/
48+
public HttpSignatureAuth(String keyId, Algorithm algorithm, List<String> headers) {
49+
this.keyId = keyId;
3450
this.algorithm = algorithm;
3551
this.headers = headers;
52+
this.digestAlgorithm = "SHA-256";
3653
}
3754

38-
public String getName() {
39-
return name;
55+
/**
56+
* Returns the opaque string that the server can use to look up the component they need to validate the signature.
57+
*
58+
* @return The keyId.
59+
*/
60+
public String getKeyId() {
61+
return keyId;
4062
}
4163

42-
public void setName(String name) {
43-
this.name = name;
64+
/**
65+
* Set the HTTP signature key id.
66+
*
67+
* @param keyId An opaque string that the server can use to look up the component they need to validate the signature.
68+
*/
69+
public void setKeyId(String keyId) {
70+
this.keyId = keyId;
4471
}
4572

73+
/**
74+
* Returns the HTTP signature algorithm which is used to sign HTTP requests.
75+
*/
4676
public Algorithm getAlgorithm() {
4777
return algorithm;
4878
}
4979

80+
/**
81+
* Sets the HTTP signature algorithm which is used to sign HTTP requests.
82+
*
83+
* @param algorithm The HTTP signature algorithm.
84+
*/
5085
public void setAlgorithm(Algorithm algorithm) {
5186
this.algorithm = algorithm;
5287
}
5388

89+
/**
90+
* Returns the digest algorithm which is used to calculate a cryptographic digest of the HTTP request body.
91+
*
92+
* @see java.security.MessageDigest
93+
*/
94+
public String getDigestAlgorithm() {
95+
return digestAlgorithm;
96+
}
97+
98+
/**
99+
* Sets the digest algorithm which is used to calculate a cryptographic digest of the HTTP request body.
100+
*
101+
* The exact list of supported digest algorithms depends on the installed security providers.
102+
* Every implementation of the Java platform is required to support "MD5", "SHA-1" and "SHA-256".
103+
* Do not use "MD5" and "SHA-1", they are vulnerable to multiple known attacks.
104+
* By default, "SHA-256" is used.
105+
*
106+
* @param digestAlgorithm The digest algorithm.
107+
*
108+
* @see java.security.MessageDigest
109+
*/
110+
public void setDigestAlgorithm(String digestAlgorithm) {
111+
this.digestAlgorithm = digestAlgorithm;
112+
}
113+
114+
/**
115+
* Returns the list of HTTP headers that should be included in the HTTP signature.
116+
*/
54117
public List<String> getHeaders() {
55118
return headers;
56119
}
57120

121+
/**
122+
* Sets the list of HTTP headers that should be included in the HTTP signature.
123+
*
124+
* @param headers The HTTP headers.
125+
*/
58126
public void setHeaders(List<String> headers) {
59127
this.headers = headers;
60128
}
@@ -67,12 +135,17 @@ public class HttpSignatureAuth implements Authentication {
67135
this.signer = signer;
68136
}
69137

70-
public void setup(Key key) throws ApiException {
138+
/**
139+
* Set the private key used to sign HTTP requests using the HTTP signature scheme.
140+
*
141+
* @param key The private key.
142+
*/
143+
public void setPrivateKey(Key key) throws ApiException {
71144
if (key == null) {
72-
throw new ApiException("key (java.security.Key) cannot be null");
145+
throw new ApiException("Private key (java.security.Key) cannot be null");
73146
}
74147

75-
signer = new Signer(key, new Signature(name, algorithm, null, headers));
148+
signer = new Signer(key, new Signature(keyId, algorithm, null, headers));
76149
}
77150

78151
@Override
@@ -88,11 +161,13 @@ public class HttpSignatureAuth implements Authentication {
88161
}
89162

90163
if (headers.contains("digest")) {
91-
headerParams.put("digest", "SHA-256=" + new String(Base64.getEncoder().encode(MessageDigest.getInstance("SHA-256").digest(payload.getBytes()))));
164+
headerParams.put("digest",
165+
this.digestAlgorithm + "=" +
166+
new String(Base64.getEncoder().encode(MessageDigest.getInstance(this.digestAlgorithm).digest(payload.getBytes()))));
92167
}
93168

94169
if (signer == null) {
95-
throw new ApiException("Signer cannot be null. Please run the method `setup` to set it up correctly");
170+
throw new ApiException("Signer cannot be null. Please call the method `setPrivateKey` to set it up correctly");
96171
}
97172

98173
// construct the path with the URL query string

0 commit comments

Comments
 (0)