8000 http: add PemReader, update to latest JWT & JWS specs · robccan/google-http-java-client@81e46f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81e46f6

Browse files
author
Yaniv Inbar
committed
http: add PemReader, update to latest JWT & JWS specs
MockHttpTransport: change buildRequest to public for ease of mocking JsonWebToken: update to http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06 JsonWebSignature: add verifySignature SecurityTestUtils: add private/public key pairs SecurityUtils: add verify & remove PEM reading methods https://codereview.appspot.com/7330045/
1 parent 1e1901c commit 81e46f6

File tree

13 files changed

+523
-328
lines changed

13 files changed

+523
-328
lines changed

clirr-ignored-differences.xml

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,69 @@
22
<!-- see http://mojo.codehaus.org/clirr-maven-plugin/examples/ignored-differences.html -->
33

44
<differences>
5+
<!-- 6001 (removed field): className, field) -->
56
<difference>
6-
<!-- 8001 (Class removed): className -->
7-
<differenceType>8001</differenceType>
8-
<className>com/google/api/client/repackaged/**</className>
9-
</difference>
10-
<difference>
11-
<!-- 7006 (Method Return Type changed): className, method, to (to is just the return type) -->
12-
<differenceType>7006</differenceType>
13-
<className>com/google/api/client/testing/http/MockLowLevelHttpResponse</className>
14-
<method>void addHeader(java.lang.String, java.lang.String)</method>
15-
<to>com.google.api.client.testing.http.MockLowLevelHttpResponse</to>
16-
</difference>
17-
<difference>
18-
<!-- 6001 (removed field): className, field) -->
197
<differenceType>6001</differenceType>
208
<className>com/google/api/client/util/Data</className>
219
<field>NULL_UNSIGNED_INTEGER</field>
2210
</difference>
2311
<difference>
24-
<!-- 6001 (removed field): className, field) -->
2512
<differenceType>6001</differenceType>
2613
<className>com/google/api/client/util/Data</className>
2714
<field>NULL_UNSIGNED_LONG</field>
2815
</difference>
16+
<!-- 7002 (method removed): className, method -->
2917
<difference>
30-
<!-- 7002 (method removed): className, method -->
3118
<differenceType>7002</differenceType>
3219
<className>com/google/api/client/json/JsonGenerator</className>
3320
<method>void writeNumber(com.google.common.primitives.UnsignedInteger)</method>
3421
</difference>
3522
<difference>
36-
<!-- 7002 (method removed): className, method -->
3723
<differenceType>7002</differenceType>
3824
<className>com/google/api/client/json/JsonGenerator</className>
3925
<method>void writeNumber(com.google.common.primitives.UnsignedLong)</method>
4026
</difference>
4127
<difference>
42-
<!-- 7002 (method removed): className, method -->
4328
<differenceType>7002</differenceType>
4429
<className>com/google/api/client/json/JsonParser</className>
4530
<method>com.google.common.primitives.UnsignedInteger getUnsignedIntegerValue()</method>
4631
</difference>
4732
<difference>
48-
<!-- 7002 (method removed): className, method -->
4933
<differenceType>7002</differenceType>
5034
<className>com/google/api/client/json/JsonParser</className>
5135
<method>com.google.common.primitives.UnsignedLong getUnsignedLongValue()</method>
5236
</difference>
5337
<difference>
54-
<!-- 7002 (method removed): className, method -->
5538
<differenceType>7002</differenceType>
5639
<className>com/google/api/client/testing/http/json/MockJsonGenerator</className>
5740
<method>void writeNumber(com.google.common.primitives.UnsignedInteger)</method>
5841
</difference>
5942
<difference>
60-
<!-- 7002 (method removed): className, method -->
6143
<differenceType>7002</differenceType>
6244
<className>com/google/api/client/testing/http/json/MockJsonGenerator</className>
6345
<method>void writeNumber(com.google.common.primitives.UnsignedLong)</method>
6446
</difference>
6547
<difference>
66-
<!-- 7002 (method removed): className, method -->
6748
<differenceType>7002</differenceType>
6849
<className>com/google/api/client/testing/http/json/MockJsonParser</className>
6950
<method>com.google.common.primitives.UnsignedInteger getUnsignedIntegerValue()</method>
7051
</difference>
7152
<difference>
72-
<!-- 7002 (method removed): className, method -->
7353
<differenceType>7002</differenceType>
7454
<className>com/google/api/client/testing/http/json/MockJsonParser</className>
7555
<method>com.google.common.primitives.UnsignedLong getUnsignedLongValue()</method>
7656
</difference>
57+
<!-- 7006 (Method Return Type changed): className, method, to (to is just the return type) -->
58+
<difference>
59+
<differenceType>7006</differenceType>
60+
<className>com/google/api/client/testing/http/MockLowLevelHttpResponse</className>
61+
<method>void addHeader(java.lang.String, java.lang.String)</method>
62+
<to>com.google.api.client.testing.http.MockLowLevelHttpResponse</to>
63+
</difference>
64+
<!-- 8001 (Class removed): className -->
65+
<difference>
66+
<differenceType>8001</differenceType>
67+
<className>com/google/api/client/repackaged/**</className>
68+
</difference>
7769
</differences>
7870

findbugs-exclude.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,11 @@
5353
<!-- Comparison of String parameter using == or != -->
5454
<Class name="com.google.api.client.util.ClassInfo$1"/>
5555
</And>
56-
<And>
57-
<Bug pattern="EI_EXPOSE_REP"/>
58-
<!-- May expose internal representation by returning reference to mutable object -->
59-
<Class name="com.google.api.client.testing.http.MockHttpContent"/>
60-
</And>
6156
<And>
6257
<Bug pattern="SS_SHOULD_BE_STATIC"/>
6358
<!-- Unread field: should this field be static? -->
6459
<Class name="com.google.api.client.json.rpc2.JsonRpcRequest"/>
6560
</And>
66-
<And>
67-
<Bug pattern="EI_EXPOSE_REP2"/>
68-
<!-- May expose internal representation by incorporating reference to mutable object -->
69-
<Class name="com.google.api.client.util.io.ByteArrayStreamingContent"/>
70-
</And>
7161
<And>
7262
<Bug pattern="DM_DEFAULT_ENCODING"/>
7363
<!-- Reliance on default encoding -->
@@ -78,6 +68,5 @@
7868
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2"/>
7969
<!-- May expose internal representation by returning reference to mutable object -->
8070
<!-- May expose internal representation by incorporating reference to mutable object -->
81-
<Class name="com.google.api.client.json.webtoken.JsonWebSignature"/>
8271
</And>
8372
</FindBugsFilter>

google-http-client/src/main/java/com/google/api/client/json/webtoken/JsonWebSignature.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.io.IOException;
2626
import java.security.GeneralSecurityException;
2727
import java.security.PrivateKey;
28+
import java.security.PublicKey;
29+
import java.security.Signature;
2830

2931
/**
3032
* <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-08">JSON Web Signature
@@ -313,6 +315,29 @@ public Header getHeader() {
313315
return (Header) super.getHeader();
314316
}
315317

318+
/**
319+
* Verifies the signature of the content.
320+
*
321+
* <p>
322+
* Currently only {@code "RS256"} algorithm is verified, but others may be added in the future.
323+
* For any other algorithm it returns {@code false}.
324+
* </p>
325+
*
326+
* @param publicKey public key
327+
* @return whether the algorithm is recognized and it is verified
328+
* @throws GeneralSecurityException
329+
*/
330+
public final boolean verifySignature(PublicKey publicKey) throws GeneralSecurityException {
331+
Signature signatureAlg = null;
332+
String algorithm = getHeader().getAlgorithm();
333+
if ("RS256".equals(algorithm)) {
334+
signatureAlg = SecurityUtils.getSha256WithRsaSignatureAlgorithm();
335+
} else {
336+
return false;
337+
}
338+
return SecurityUtils.verify(signatureAlg, publicKey, signatureBytes, signedContentBytes);
339+
}
340+
316341
/** Returns the modifiable array of bytes of the signature. */
317342
public final byte[] getSignatureBytes() {
318343
return signatureBytes;
@@ -424,7 +449,7 @@ public JsonWebSignature parse(String tokenString) throws IOException {
424449
* Signs a given JWS header and payload based on the given private key using RSA and SHA-256 as
425450
* described in <a
426451
* href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-08#appendix-A.2">JWS using
427-
* RSA SHA-256</a>
452+
* RSA SHA-256</a>.
428453
*
429454
* @param privateKey private key
430455
* @param jsonFactory JSON factory

0 commit comments

Comments
 (0)
0