diff --git a/src/main/java/com/twilio/jwt/accesstoken/ChatGrant.java b/src/main/java/com/twilio/jwt/accesstoken/ChatGrant.java new file mode 100644 index 0000000000..03a658347e --- /dev/null +++ b/src/main/java/com/twilio/jwt/accesstoken/ChatGrant.java @@ -0,0 +1,84 @@ +package com.twilio.jwt.accesstoken; + +/** + * Grant used to access Twilio Chat. + * + *

+ * For more information see: + * + * https://www.twilio.com/docs/api/rest/access-tokens + * + *

+ */ +public class ChatGrant implements Grant { + + private String serviceSid; + private String deploymentRoleSid; + private String pushCredentialSid; + private String endpointId; + + public String getServiceSid() { + return serviceSid; + } + + public ChatGrant setServiceSid(String serviceSid) { + this.serviceSid = serviceSid; + return this; + } + + + public String getPushCredentialSid() { + return pushCredentialSid; + } + + public ChatGrant setPushCredentialSid(String pushCredentialSid) { + this.pushCredentialSid = pushCredentialSid; + return this; + } + + public String getDeploymentRoleSid() { + return deploymentRoleSid; + } + + public ChatGrant setDeploymentRoleSid(String deploymentRoleSid) { + this.deploymentRoleSid = deploymentRoleSid; + return this; + } + + public String getEndpointId() { + return endpointId; + } + + public ChatGrant setEndpointId(String endpointId) { + this.endpointId = endpointId; + return this; + } + + public String getGrantKey() { + return "chat"; + } + + public Object getPayload() { + return new Payload(this); + } + + @SuppressWarnings("checkstyle:membername") + public class Payload { + public final String service_sid; + public final String deployment_role_sid; + public final String endpoint_id; + public final String push_credential_sid; + + /** + * Create the grant payload. + * + * @param grant IP Messaging grant + */ + public Payload(ChatGrant grant) { + this.service_sid = grant.serviceSid; + this.deployment_role_sid = grant.deploymentRoleSid; + this.endpoint_id = grant.endpointId; + this.push_credential_sid = grant.pushCredentialSid; + } + } +} diff --git a/src/main/java/com/twilio/jwt/accesstoken/IpMessagingGrant.java b/src/main/java/com/twilio/jwt/accesstoken/IpMessagingGrant.java index b3f8fb96d1..61bda23845 100644 --- a/src/main/java/com/twilio/jwt/accesstoken/IpMessagingGrant.java +++ b/src/main/java/com/twilio/jwt/accesstoken/IpMessagingGrant.java @@ -10,6 +10,7 @@ * *

*/ +@Deprecated public class IpMessagingGrant implements Grant { private String serviceSid; diff --git a/src/test/java/com/twilio/jwt/accesstoken/AccessTokenTest.java b/src/test/java/com/twilio/jwt/accesstoken/AccessTokenTest.java index 1f0250e46f..d5f335ded9 100644 --- a/src/test/java/com/twilio/jwt/accesstoken/AccessTokenTest.java +++ b/src/test/java/com/twilio/jwt/accesstoken/AccessTokenTest.java @@ -140,6 +140,36 @@ public void testIpMessagingGrant() { Assert.assertEquals("IS123", grant.get("service_sid")); } + @Test + public void testChatGrant() { + ChatGrant cg = new ChatGrant() + .setDeploymentRoleSid("RL123") + .setEndpointId("foobar") + .setPushCredentialSid("CR123") + .setServiceSid("IS123"); + Jwt token = + new AccessToken.Builder(ACCOUNT_SID, SIGNING_KEY_SID, SECRET) + .grant(cg) + .build(); + + Claims claims = + Jwts.parser() + .setSigningKey(SECRET.getBytes()) + .parseClaimsJws(token.toJwt()) + .getBody(); + + validateToken(claims); + + Map decodedGrants = (Map) claims.get("grants"); + Assert.assertEquals(1, decodedGrants.size()); + + Map grant = (Map) decodedGrants.get("chat"); + Assert.assertEquals("RL123", grant.get("deployment_role_sid")); + Assert.assertEquals("foobar", grant.get("endpoint_id")); + Assert.assertEquals("CR123", grant.get("push_credential_sid")); + Assert.assertEquals("IS123", grant.get("service_sid")); + } + @Test public void testSyncGrant() { SyncGrant sg = new SyncGrant()