1919
2020import java .io .IOException ;
2121import java .io .InputStreamReader ;
22+ import java .security .PrivateKey ;
2223import java .util .Arrays ;
2324import java .util .Collections ;
2425import java .util .Date ;
3031public class ValidationToken extends Jwt {
3132
3233 private static final HashFunction HASH_FUNCTION = Hashing .sha256 ();
34+ private static final String CTY = "twilio-pkrv;v=1" ;
3335 private static final String NEW_LINE = "\n " ;
3436
37+ private final String accountSid ;
3538 private final String credentialSid ;
39+ private final String signingKeySid ;
3640 private final String method ;
3741 private final String uri ;
3842 private final String queryString ;
@@ -42,12 +46,14 @@ public class ValidationToken extends Jwt {
4246
4347 private ValidationToken (Builder b ) {
4448 super (
45- SignatureAlgorithm .HS256 ,
49+ SignatureAlgorithm .RS256 ,
4650 b .privateKey ,
4751 b .credentialSid ,
4852 new Date (new Date ().getTime () + b .ttl * 1000 )
4953 );
54+ this .accountSid = b .accountSid ;
5055 this .credentialSid = b .credentialSid ;
56+ this .signingKeySid = b .signingKeySid ;
5157 this .method = b .method ;
5258 this .uri = b .uri ;
5359 this .queryString = b .queryString ;
@@ -59,7 +65,7 @@ private ValidationToken(Builder b) {
5965 @ Override
6066 public Map <String , Object > getHeaders () {
6167 Map <String , Object > headers = new HashMap <>();
62- headers .put ("cty" , "twilio-pkrv;v=1" );
68+ headers .put ("cty" , CTY );
6369 headers .put ("kid" , this .credentialSid );
6470 return headers ;
6571 }
@@ -68,6 +74,9 @@ public Map<String, Object> getHeaders() {
6874 public Map <String , Object > getClaims () {
6975 Map <String , Object > payload = new HashMap <>();
7076
77+ payload .put ("iss" , this .signingKeySid );
78+ payload .put ("sub" , this .accountSid );
79+
7180 // Sort the signed headers
7281 Collections .sort (signedHeaders );
7382 List <String > lowercaseSignedHeaders = Lists .transform (signedHeaders , LOWERCASE_STRING );
@@ -122,12 +131,14 @@ public Map<String, Object> getClaims() {
122131 }
123132
124133 public static ValidationToken fromHttpRequest (
134+ String accountSid ,
125135 String credentialSid ,
126- String privateKey ,
136+ String signingKeySid ,
137+ PrivateKey privateKey ,
127138 HttpRequest request ,
128139 List <String > signedHeaders
129140 ) throws IOException {
130- Builder builder = new Builder (credentialSid , privateKey );
141+ Builder builder = new Builder (accountSid , credentialSid , signingKeySid , privateKey );
131142
132143 String method = request .getRequestLine ().getMethod ();
133144 builder .method (method );
@@ -190,18 +201,22 @@ public String apply(String s) {
190201
191202 public static class Builder {
192203
204+ private String accountSid ;
193205 private String credentialSid ;
194- private String privateKey ;
206+ private String signingKeySid ;
207+ private PrivateKey privateKey ;
195208 private String method ;
196209 private String uri ;
197210 private String queryString = "" ;
198211 private Header [] headers ;
199212 private List <String > signedHeaders = Collections .emptyList ();
200213 private String requestBody = "" ;
201- private int ttl = 3600 ;
214+ private int ttl = 300 ;
202215
203- public Builder (String credentialSid , String privateKey ) {
216+ public Builder (String accountSid , String credentialSid , String signingKeySid , PrivateKey privateKey ) {
217+ this .accountSid = accountSid ;
204218 this .credentialSid = credentialSid ;
219+ this .signingKeySid = signingKeySid ;
205220 this .privateKey = privateKey ;
206221 }
207222
0 commit comments