|
| 1 | +package com.twilio.jwt.validation; |
| 2 | + |
| 3 | +import com.google.common.base.Charsets; |
| 4 | +import com.google.common.base.Function; |
| 5 | +import com.google.common.base.Joiner; |
| 6 | +import com.google.common.hash.HashFunction; |
| 7 | +import com.google.common.hash.Hashing; |
| 8 | +import com.google.common.io.CharStreams; |
| 9 | +import com.twilio.http.HttpMethod; |
| 10 | +import com.twilio.jwt.Jwt; |
| 11 | +import io.jsonwebtoken.SignatureAlgorithm; |
| 12 | +import org.apache.http.Header; |
| 13 | +import org.apache.http.HttpEntity; |
| 14 | +import org.apache.http.HttpEntityEnclosingRequest; |
| 15 | +import org.apache.http.HttpRequest; |
| 16 | +import org.apache.http.message.BasicHeader; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStreamReader; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.Date; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | + |
| 29 | +public class ValidationToken extends Jwt { |
| 30 | + |
| 31 | + private static final HashFunction HASH_FUNCTION = Hashing.sha256(); |
| 32 | + private static final String NEW_LINE = "\n"; |
| 33 | + |
| 34 | + private final String method; |
| 35 | + private final String uri; |
| 36 | + private final String queryString; |
| 37 | + private final Header[] headers; |
| 38 | + private final List<String> signedHeaders; |
| 39 | + private final String requestBody; |
| 40 | + |
| 41 | + private ValidationToken(Builder b) { |
| 42 | + super( |
| 43 | + SignatureAlgorithm.HS256, |
| 44 | + b.privateKey, |
| 45 | + b.credentialSid, |
| 46 | + new Date(new Date().getTime() + b.ttl * 1000) |
| 47 | + ); |
| 48 | + this.method = b.method; |
| 49 | + this.uri = b.uri; |
| 50 | + this.queryString = b.queryString; |
| 51 | + this.headers = b.headers; |
| 52 | + this.signedHeaders = b.signedHeaders; |
| 53 | + this.requestBody = b.requestBody; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Map<String, Object> getHeaders() { |
| 58 | + return Collections.emptyMap(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Map<String, Object> getClaims() { |
| 63 | + Map<String, Object> payload = new HashMap<>(); |
| 64 | + |
| 65 | + // Sort the signed headers |
| 66 | + Collections.sort(signedHeaders); |
| 67 | + String includedHeaders = Joiner.on(";").join(signedHeaders); |
| 68 | + payload.put("hrh", includedHeaders); |
| 69 | + |
| 70 | + // Add the method and uri |
| 71 | + StringBuilder signature = new StringBuilder(); |
| 72 | + signature.append(method).append(NEW_LINE); |
| 73 | + signature.append(uri).append(NEW_LINE); |
| 74 | + |
| 75 | + // Get the query args, sort and rejoin |
| 76 | + String[] queryArgs = queryString.split("&"); |
| 77 | + Arrays.sort(queryArgs); |
| 78 | + String sortedQueryString = Joiner.on("&").join(queryArgs); |
| 79 | + signature.append(sortedQueryString).append(NEW_LINE); |
| 80 | + |
| 81 | + // Normalize all the headers |
| 82 | + Header[] lowercaseHeaders = LOWERCASE_KEYS.apply(headers); |
| 83 | + Arrays.sort(lowercaseHeaders, new Comparator<Header>() { |
| 84 | + @Override |
| 85 | + public int compare(Header o1, Header o2) { |
| 86 | + return o1.getName().compareTo(o2.getName()); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + // Add the headers that we care about |
| 91 | + for (Header header: lowercaseHeaders) { |
| 92 | + if (signedHeaders.contains(header.getName().toLowerCase())) { |
| 93 | + signature.append(header.getName().toLowerCase().trim()) |
| 94 | + .append(":") |
| 95 | + .append(header.getValue().trim()) |
| 96 | + .append(NEW_LINE); |
| 97 | + } |
| 98 | + } |
| 99 | + signature.append(NEW_LINE); |
| 100 | + |
| 101 | + // Mark the headers that we care about |
| 102 | + signature.append(includedHeaders).append(NEW_LINE); |
| 103 | + |
| 104 | + // Hash and hex the request payload |
| 105 | + String hashedPayload = HASH_FUNCTION.hashString(requestBody, Charsets.UTF_8).toString(); |
| 106 | + signature.append(hashedPayload).append(NEW_LINE); |
| 107 | + |
| 108 | + // Hash and hex the canonical request |
| 109 | + String hashedSignature = HASH_FUNCTION.hashString(signature.toString(), Charsets.UTF_8).toString(); |
| 110 | + payload.put("rqh", hashedSignature); |
| 111 | + |
| 112 | + return payload; |
| 113 | + } |
| 114 | + |
| 115 | + public static ValidationToken fromHttpRequest( |
| 116 | + String credentialSid, |
| 117 | + String privateKey, |
| 118 | + HttpRequest request, |
| 119 | + List<String> signedHeaders |
| 120 | + ) throws IOException { |
| 121 | + Builder builder = new Builder(credentialSid, privateKey); |
| 122 | + |
| 123 | + String method = request.getRequestLine().getMethod(); |
| 124 | + builder.method(method); |
| 125 | + |
| 126 | + String uri = request.getRequestLine().getUri(); |
| 127 | + if (uri.contains("?")) { |
| 128 | + String[] uriParts = uri.split("\\?"); |
| 129 | + builder.uri(uriParts[0]); |
| 130 | + builder.queryString(uriParts[1]); |
| 131 | + } else { |
| 132 | + builder.uri(uri); |
| 133 | + } |
| 134 | + |
| 135 | + builder.headers(request.getAllHeaders()); |
| 136 | + builder.signedHeaders(signedHeaders); |
| 137 | + |
| 138 | + if (HttpMethod.POST.toString().equals(method.toUpperCase())) { |
| 139 | + HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity(); |
| 140 | + builder.requestBody(CharStreams.toString(new InputStreamReader(entity.getContent(), Charsets.UTF_8))); |
| 141 | + } |
| 142 | + |
| 143 | + return builder.build(); |
| 144 | + } |
| 145 | + |
| 146 | + private static Function<Header[], Header[]> LOWERCASE_KEYS = new Function<Header[], Header[]>() { |
| 147 | + @Override |
| 148 | + public Header[] apply(Header[] headers) { |
| 149 | + Header[] lowercaseHeaders = new Header[headers.length]; |
| 150 | + for (int i = 0; i < headers.length; i++) { |
| 151 | + lowercaseHeaders[i] = new BasicHeader(headers[i].getName().toLowerCase(), headers[i].getValue()); |
| 152 | + } |
| 153 | + |
| 154 | + return lowercaseHeaders; |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + public static class Builder { |
| 159 | + |
| 160 | + private String credentialSid; |
| 161 | + private String privateKey; |
| 162 | + private String method; |
| 163 | + private String uri; |
| 164 | + private String queryString = ""; |
| 165 | + private Header[] headers; |
| 166 | + private List<String> signedHeaders = Collections.emptyList(); |
| 167 | + private String requestBody = ""; |
| 168 | + private int ttl = 3600; |
| 169 | + |
| 170 | + public Builder(String credentialSid, String privateKey) { |
| 171 | + this.credentialSid = credentialSid; |
| 172 | + this.privateKey = privateKey; |
| 173 | + } |
| 174 | + |
| 175 | + public Builder method(String method) { |
| 176 | + this.method = method; |
| 177 | + return this; |
| 178 | + } |
| 179 | + |
| 180 | + public Builder uri(String uri) { |
| 181 | + this.uri = uri; |
| 182 | + return this; |
| 183 | + } |
| 184 | + |
| 185 | + public Builder queryString(String queryString) { |
| 186 | + this.queryString = queryString; |
| 187 | + return this; |
| 188 | + } |
| 189 | + |
| 190 | + public Builder headers(Header[] headers) { |
| 191 | + this.headers = headers; |
| 192 | + return this; |
| 193 | + } |
| 194 | + |
| 195 | + public Builder signedHeaders(List<String> signedHeaders) { |
| 196 | + this.signedHeaders = signedHeaders; |
| 197 | + return this; |
| 198 | + } |
| 199 | + |
| 200 | + public Builder requestBody(String requestBody) { |
| 201 | + this.requestBody = requestBody; |
| 202 | + return this; |
| 203 | + } |
| 204 | + |
| 205 | + public Builder ttl(int ttl) { |
| 206 | + this.ttl = ttl; |
| 207 | + return this; |
| 208 | + } |
| 209 | + |
| 210 | + public ValidationToken build() { |
| 211 | + return new ValidationToken(this); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments