Skip to content

Commit 441e17c

Browse files
feat: Regional support for access token (#602)
Co-authored-by: Elise Shanholtz <[email protected]>
1 parent 0eaee80 commit 441e17c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/main/java/com/twilio/jwt/accesstoken/AccessToken.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class AccessToken extends Jwt {
2727
private final String id;
2828
private final String accountSid;
2929
private final String identity;
30+
private final String region;
3031
private final Date nbf;
3132
private final Set<Grant> grants;
3233

@@ -42,6 +43,7 @@ private AccessToken(Builder b) {
4243
this.id = b.keySid + "-" + (int)(Math.floor(now.getTime() / 1000.0f));
4344
this.accountSid = b.accountSid;
4445
this.identity = b.identity;
46+
this.region = b.region;
4547
this.nbf = b.nbf;
4648
this.grants = Collections.unmodifiableSet(b.grants);
4749
}
@@ -65,6 +67,9 @@ public String getSubject() {
6567
public Map<String, Object> getHeaders() {
6668
Map<String, Object> headers = new HashMap<>();
6769
headers.put("cty", CTY);
70+
if (this.region != null && !"".equals(this.region)) {
71+
headers.put("twr", this.region);
72+
}
6873
return headers;
6974
}
7075

@@ -91,6 +96,7 @@ public static class Builder {
9196
private String keySid;
9297
private String secret;
9398
private String identity;
99+
private String region;
94100
private Date nbf = null;
95101
private int ttl = 3600;
96102
private Set<Grant> grants = new HashSet<Grant>();
@@ -113,6 +119,11 @@ public Builder identity(String identity) {
113119
return this;
114120
}
115121

122+
public Builder region(String region) {
123+
this.region = region;
124+
return this;
125+
}
126+
116127
public Builder ttl(int ttl) {
117128
this.ttl = ttl;
118129
return this;

src/test/java/com/twilio/jwt/accesstoken/AccessTokenTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.twilio.jwt.Jwt;
44
import io.jsonwebtoken.Claims;
5+
import io.jsonwebtoken.JwsHeader;
56
import io.jsonwebtoken.Jwts;
67
import org.junit.Assert;
78
import org.junit.Test;
@@ -93,6 +94,32 @@ public void testOptionalValues() {
9394
Assert.assertTrue(claims.getNotBefore().getTime() <= new Date().getTime());
9495
}
9596

97+
@Test
98+
public void testRegion() {
99+
Jwt token = new AccessToken.Builder(ACCOUNT_SID, SIGNING_KEY_SID, SECRET)
100+
.region("foo")
101+
.build();
102+
103+
JwsHeader header = Jwts.parser()
104+
.setSigningKey(SECRET.getBytes())
105+
.parseClaimsJws(token.toJwt())
106+
.getHeader();
107+
108+
Assert.assertEquals("foo", header.get("twr"));
109+
}
110+
111+
@Test
112+
public void testEmptyRegion() {
113+
Jwt token = new AccessToken.Builder(ACCOUNT_SID, SIGNING_KEY_SID, SECRET).build();
114+
115+
JwsHeader header = Jwts.parser()
116+
.setSigningKey(SECRET.getBytes())
117+
.parseClaimsJws(token.toJwt())
118+
.getHeader();
119+
120+
Assert.assertEquals(null, header.get("twr"));
121+
}
122+
96123
@Test
97124
public void testVideoGrant() {
98125
VideoGrant cg = new VideoGrant().setRoom("RM123");

0 commit comments

Comments
 (0)