Skip to content

Commit

Permalink
Add ChatGrant, deprecate IpMessagingGrant (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingming authored and dougblack committed Aug 18, 2017
1 parent cd62281 commit 824b0cc
Showing 3 changed files with 115 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/main/java/com/twilio/jwt/accesstoken/ChatGrant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.twilio.jwt.accesstoken;

/**
* Grant used to access Twilio Chat.
*
* <p>
* For more information see:
* <a href="https://www.twilio.com/docs/api/rest/access-tokens">
* https://www.twilio.com/docs/api/rest/access-tokens
* </a>
* </p>
*/
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
* </a>
* </p>
*/
@Deprecated
public class IpMessagingGrant implements Grant {

private String serviceSid;
30 changes: 30 additions & 0 deletions src/test/java/com/twilio/jwt/accesstoken/AccessTokenTest.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> decodedGrants = (Map<String, Object>) claims.get("grants");
Assert.assertEquals(1, decodedGrants.size());

Map<String, Object> grant = (Map<String, Object>) 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()

0 comments on commit 824b0cc

Please sign in to comment.