You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[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.
Copy file name to clipboardExpand all lines: modules/openapi-generator/src/main/resources/Java/libraries/jersey2-experimental/auth/HttpSignatureAuth.mustache
+87-12Lines changed: 87 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -19,42 +19,110 @@ import java.util.List;
19
19
20
20
import org.tomitribe.auth.signatures.*;
21
21
22
+
/**
23
+
* A Configuration object for the HTTP message signature security scheme.
24
+
*/
22
25
public class HttpSignatureAuth implements Authentication {
23
26
24
27
private Signer signer;
25
28
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;
27
31
32
+
// The HTTP signature algorithm.
28
33
private Algorithm algorithm;
29
34
35
+
// The list of HTTP headers that should be included in the HTTP signature.
30
36
private List<String> headers;
31
37
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;
34
50
this.algorithm = algorithm;
35
51
this.headers = headers;
52
+
this.digestAlgorithm = "SHA-256";
36
53
}
37
54
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;
40
62
}
41
63
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;
44
71
}
45
72
73
+
/**
74
+
* Returns the HTTP signature algorithm which is used to sign HTTP requests.
75
+
*/
46
76
public Algorithm getAlgorithm() {
47
77
return algorithm;
48
78
}
49
79
80
+
/**
81
+
* Sets the HTTP signature algorithm which is used to sign HTTP requests.
82
+
*
83
+
* @param algorithm The HTTP signature algorithm.
84
+
*/
50
85
public void setAlgorithm(Algorithm algorithm) {
51
86
this.algorithm = algorithm;
52
87
}
53
88
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
+
*/
54
117
public List<String> getHeaders() {
55
118
return headers;
56
119
}
57
120
121
+
/**
122
+
* Sets the list of HTTP headers that should be included in the HTTP signature.
123
+
*
124
+
* @param headers The HTTP headers.
125
+
*/
58
126
public void setHeaders(List<String> headers) {
59
127
this.headers = headers;
60
128
}
@@ -67,12 +135,17 @@ public class HttpSignatureAuth implements Authentication {
67
135
this.signer = signer;
68
136
}
69
137
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 {
71
144
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");
73
146
}
74
147
75
-
signer = new Signer(key, new Signature(name, algorithm, null, headers));
148
+
signer = new Signer(key, new Signature(keyId, algorithm, null, headers));
76
149
}
77
150
78
151
@Override
@@ -88,11 +161,13 @@ public class HttpSignatureAuth implements Authentication {
88
161
}
89
162
90
163
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()))));
92
167
}
93
168
94
169
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");
0 commit comments