|
13 | 13 | */ |
14 | 14 | package com.example.iap; |
15 | 15 |
|
16 | | -import com.auth0.jwt.JWT; |
17 | | -import com.auth0.jwt.JWTVerifier; |
18 | | -import com.auth0.jwt.algorithms.Algorithm; |
19 | | -import com.auth0.jwt.exceptions.JWTVerificationException; |
20 | | -import com.auth0.jwt.interfaces.DecodedJWT; |
21 | | -import com.auth0.jwt.interfaces.ECDSAKeyProvider; |
22 | 16 | import com.fasterxml.jackson.core.type.TypeReference; |
23 | 17 | import com.fasterxml.jackson.databind.ObjectMapper; |
24 | 18 | import com.google.api.client.http.GenericUrl; |
|
28 | 22 | import com.google.api.client.http.javanet.NetHttpTransport; |
29 | 23 | import com.google.api.client.util.PemReader; |
30 | 24 | import com.google.api.client.util.PemReader.Section; |
| 25 | +import io.jsonwebtoken.Claims; |
| 26 | +import io.jsonwebtoken.JwsHeader; |
| 27 | +import io.jsonwebtoken.Jwt; |
| 28 | +import io.jsonwebtoken.Jwts; |
| 29 | +import io.jsonwebtoken.SigningKeyResolver; |
| 30 | +import io.jsonwebtoken.impl.DefaultClaims; |
| 31 | + |
31 | 32 | import java.io.IOException; |
32 | 33 | import java.io.StringReader; |
33 | 34 | import java.net.URL; |
| 35 | +import java.security.Key; |
34 | 36 | import java.security.KeyFactory; |
35 | 37 | import java.security.NoSuchAlgorithmException; |
36 | 38 | import java.security.PublicKey; |
37 | | -import java.security.interfaces.ECPrivateKey; |
38 | 39 | import java.security.interfaces.ECPublicKey; |
39 | 40 | import java.security.spec.InvalidKeySpecException; |
40 | 41 | import java.security.spec.X509EncodedKeySpec; |
|
43 | 44 |
|
44 | 45 | /** Verify IAP authorization JWT token in incoming request. */ |
45 | 46 | public class VerifyIapRequestHeader { |
| 47 | + |
46 | 48 | // [START verify_iap_request] |
47 | 49 | private static final String PUBLIC_KEY_VERIFICATION_URL = |
48 | 50 | "https://www.gstatic.com/iap/verify/public_key"; |
49 | 51 | private static final String IAP_ISSUER_URL = "https://cloud.google.com/iap"; |
50 | 52 |
|
51 | | - private final Map<String, ECPublicKey> keyCache = new HashMap<>(); |
| 53 | + private final Map<String, Key> keyCache = new HashMap<>(); |
52 | 54 | private final ObjectMapper mapper = new ObjectMapper(); |
53 | 55 | private final TypeReference<HashMap<String, String>> typeRef = |
54 | 56 | new TypeReference<HashMap<String, String>>() {}; |
55 | 57 |
|
56 | | - private ECDSAKeyProvider keyProvider = |
57 | | - new ECDSAKeyProvider() { |
| 58 | + private SigningKeyResolver resolver = |
| 59 | + new SigningKeyResolver() { |
| 60 | + @Override |
| 61 | + public Key resolveSigningKey(JwsHeader header, Claims claims) { |
| 62 | + return resolveSigningKey(header); |
| 63 | + } |
| 64 | + |
58 | 65 | @Override |
59 | | - public ECPublicKey getPublicKeyById(String kid) { |
60 | | - ECPublicKey key = keyCache.get(kid); |
| 66 | + public Key resolveSigningKey(JwsHeader header, String payload) { |
| 67 | + return resolveSigningKey(header); |
| 68 | + } |
| 69 | + |
| 70 | + private Key resolveSigningKey(JwsHeader header) { |
| 71 | + String keyId = header.getKeyId(); |
| 72 | + Key key = keyCache.get(keyId); |
61 | 73 | if (key != null) { |
62 | 74 | return key; |
63 | 75 | } |
@@ -72,33 +84,20 @@ public ECPublicKey getPublicKeyById(String kid) { |
72 | 84 | } |
73 | 85 | Map<String, String> keys = mapper.readValue(response.parseAsString(), typeRef); |
74 | 86 | for (Map.Entry<String, String> keyData : keys.entrySet()) { |
75 | | - if (!keyData.getKey().equals(kid)) { |
| 87 | + if (!keyData.getKey().equals(keyId)) { |
76 | 88 | continue; |
77 | 89 | } |
78 | 90 | key = getKey(keyData.getValue()); |
79 | 91 | if (key != null) { |
80 | | - keyCache.putIfAbsent(kid, key); |
| 92 | + keyCache.putIfAbsent(keyId, key); |
81 | 93 | } |
82 | 94 | } |
83 | 95 |
|
84 | 96 | } catch (IOException e) { |
85 | 97 | // ignore exception |
86 | 98 | } |
87 | | - |
88 | 99 | return key; |
89 | 100 | } |
90 | | - |
91 | | - @Override |
92 | | - public ECPrivateKey getPrivateKey() { |
93 | | - // ignore : only required for signing requests |
94 | | - return null; |
95 | | - } |
96 | | - |
97 | | - @Override |
98 | | - public String getPrivateKeyId() { |
99 | | - // ignore : only required for signing requests |
100 | | - return null; |
101 | | - } |
102 | 101 | }; |
103 | 102 |
|
104 | 103 | private static String getBaseUrl(URL url) throws Exception { |
@@ -108,7 +107,7 @@ private static String getBaseUrl(URL url) throws Exception { |
108 | 107 | return (url.getProtocol() + "://" + url.getHost() + path).trim(); |
109 | 108 | } |
110 | 109 |
|
111 | | - DecodedJWT verifyJWTToken(HttpRequest request) throws Exception { |
| 110 | + Jwt verifyJWTToken(HttpRequest request) throws Exception { |
112 | 111 | // Check for iap jwt header in incoming request |
113 | 112 | String jwtToken = |
114 | 113 | request.getHeaders().getFirstHeaderStringValue("x-goog-authenticated-user-jwt"); |
@@ -119,24 +118,25 @@ DecodedJWT verifyJWTToken(HttpRequest request) throws Exception { |
119 | 118 | return verifyJWTToken(jwtToken, baseUrl); |
120 | 119 | } |
121 | 120 |
|
122 | | - DecodedJWT verifyJWTToken(String jwtToken, String baseUrl) throws Exception { |
123 | | - Algorithm algorithm = Algorithm.ECDSA256(keyProvider); |
124 | | - |
125 | | - // Time constraints are automatically checked, use acceptLeeway to specify a leeway window |
| 121 | + Jwt verifyJWTToken(String jwtToken, String baseUrl) throws Exception { |
| 122 | + // Time constraints are automatically checked, use setAllowedClockSkewSeconds |
| 123 | + // to specify a leeway window |
126 | 124 | // The token was issued in a past date "iat" < TODAY |
127 | 125 | // The token hasn't expired yet "exp" > TODAY |
128 | | - JWTVerifier verifier = |
129 | | - JWT.require(algorithm).withAudience(baseUrl).withIssuer(IAP_ISSUER_URL).build(); |
130 | | - |
131 | | - DecodedJWT decodedJWT = verifier.verify(jwtToken); |
132 | | - |
133 | | - if (decodedJWT.getSubject() == null) { |
134 | | - throw new JWTVerificationException("Subject expected, not found"); |
| 126 | + Jwt jwt = |
| 127 | + Jwts.parser() |
| 128 | + .setSigningKeyResolver(resolver) |
| 129 | + .requireAudience(baseUrl) |
| 130 | + .requireIssuer(IAP_ISSUER_URL) |
| 131 | + .parse(jwtToken); |
| 132 | + DefaultClaims claims = (DefaultClaims) jwt.getBody(); |
| 133 | + if (claims.getSubject() == null) { |
| 134 | + throw new Exception("Subject expected, not found."); |
135 | 135 | } |
136 | | - if (decodedJWT.getClaim("email") == null) { |
137 | | - throw new JWTVerificationException("Email expected, not found"); |
| 136 | + if (claims.get("email") == null) { |
| 137 | + throw new Exception("Email expected, not found."); |
138 | 138 | } |
139 | | - return decodedJWT; |
| 139 | + return jwt; |
140 | 140 | } |
141 | 141 |
|
142 | 142 | private ECPublicKey getKey(String keyText) throws IOException { |
|