8000 Refactor JWT code and add TaskRouterCapability · senthgit/twilio-java@4cf1430 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4cf1430

Browse files
committed
Refactor JWT code and add TaskRouterCapability
1 parent f084b91 commit 4cf1430

File tree

4 files changed

+296
-78
lines changed

4 files changed

+296
-78
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.twilio.sdk;
2+
3+
import org.apache.commons.codec.binary.Base64;
4+
import org.apache.commons.lang.StringUtils;
5+
import org.json.simple.JSONValue;
6+
7+
import javax.crypto.Mac;
8+
import javax.crypto.spec.SecretKeySpec;
9+
import java.io.UnsupportedEncodingException;
10+
import java.security.InvalidKeyException;
11+
import java.security.NoSuchAlgorithmException;
12+
import java.util.ArrayList;
13+
import java.util.LinkedHashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
18+
public class CapabilityToken {
19+
protected static String jwtEncode(Map<String, Object> payload, String key)
20+
throws InvalidKeyException, NoSuchAlgorithmException,
21+
UnsupportedEncodingException {
22+
23+
Map<String, Object> header = new LinkedHashMap<String, Object>();
24+
header.put("typ", "JWT");
25+
header.put("alg", "HS256");
26+
27+
List<String> segments = new ArrayList<String>();
28+
segments.add(encodeBase64(jsonEncode(header)));
29+
segments.add(encodeBase64(jsonEncode(payload)));
30+
31+
String signingInput = StringUtils.join(segments, ".");
32+
String signature = sign(signingInput, key);
33+
segments.add(signature);
34+
35+
return StringUtils.join(segments, ".");
36+
}
37+
38+
private static String jsonEncode(Object object) {
39+
String json = JSONValue.toJSONString(object);
40+
return json.replace("\\/", "/");
41+
}
42+
43+
private static String encodeBase64(String data)
44+
throws UnsupportedEncodingException {
45+
return encodeBase64(data.getBytes("UTF-8"));
46+
}
47+
48+
private static String encodeBase64(byte[] data)
49+
throws UnsupportedEncodingException {
50+
String encodedString = new String(Base64.encodeBase64(data));
51+
String safeString = encodedString.replace('+', '-').replace('/', '_')
52+
.replace("=", "");
53+
return safeString;
54+
}
55+
56+
// see
57+
// http://discussion.forum.nokia.com/forum/showthread.php?130974-Help-required-How-to-generate-a-MAC-(HMAC-SHA1)-with-Java
58+
private static String sign(String data, String key)
59+
throws NoSuchAlgorithmException, InvalidKeyException,
60+
UnsupportedEncodingException {
61+
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"),
62+
"HmacSHA256");
63+
Mac mac = Mac.getInstance("HmacSHA256");
64+
mac.init(signingKey);
65+
byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
66+
String result = encodeBase64(rawHmac);
67+
return result;
68+
}
69+
70+
@SuppressWarnings("serial")
71+
public static class DomainException extends Exception {
72+
public DomainException(String message) {
73+
super(message);
74+
}
75+
76+
public DomainException(Exception e) {
77+
super(e);
78+
}
79+
}
80+
}

src/main/java/com/twilio/sdk/client/TwilioCapability.java

Lines changed: 10 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
package com.twilio.sdk.client;
22

3-
import org.apache.commons.codec.binary.Base64;
43
import org.apache.commons.lang.StringUtils;
5-
import org.json.simple.JSONValue;
64

7-
import javax.crypto.Mac;
8-
import javax.crypto.spec.SecretKeySpec;
95
import java.io.UnsupportedEncodingException;
106
import java.net.URLEncoder;
11-
import java.security.InvalidKeyException;
12-
import java.security.NoSuchAlgorithmException;
137
import java.util.*;
148

159
/**
1610
* This class represents a token that will grant someone access to resources
1711
* within Twilio.
1812
*/
19-
public class TwilioCapability {
20-
21-
@SuppressWarnings("serial")
22-
public static class DomainException extends Exception {
23-
public DomainException(String message) {
24-
super(message);
25-
}
26-
27-
public DomainException(Exception e) {
28-
super(e);
29-
}
30-
}
13+
public class TwilioCapability extends com.twilio.sdk.CapabilityToken {
3114

3215
private String accountSid;
3316
private String authToken;
@@ -50,7 +33,7 @@ public DomainException(Exception e) {
5033
* Create a new TwilioCapability with zero permissions. Next steps are to
5134
* grant access to resources by configuring this token through the functions
5235
* allowXXXX.
53-
*
36+
*
5437
* @param accountSid
5538
* the account sid to which this token is granted access
5639
* @param authToken
@@ -65,7 +48,7 @@ public TwilioCapability(String accountSid, String authToken) {
6548
}
6649

6750
private String buildScopeString(String serivce, String priviledge,
68-
Map<String, String> params) {
51+
Map<String, String> params) {
6952

7053
StringBuilder scope = new StringBuilder();
7154

@@ -86,7 +69,7 @@ private String buildScopeString(String serivce, String priviledge,
8669

8770
/**
8871
* Allow the user of this token to make outgoing connections.
89-
*
72+
*
9073
* @param appSid
9174
* the application to which this token grants access
9275
*/
@@ -96,7 +79,7 @@ public void allowClientOutgoing(String appSid) {
9679

9780
/**
9881
* Allow the user of this token to make outgoing connections.
99-
*
82+
*
10083
* @param appSid
10184
* the application to which this token grants access
10285
* @param params
@@ -156,7 +139,7 @@ private String generateParamString(Map<String, String> params) {
156139
* If the user of this token should be allowed to accept incoming
157140
* connections then configure the TwilioCapability through this method and
158141
* specify the client name.
159-
*
142+
*
160143
* @param clientName
161144
*/
162145
public void allowClientIncoming(String clientName) {
@@ -167,7 +150,7 @@ public void allowClientIncoming(String clientName) {
167150

168151
/**
169152
* Allow the user of this token to access their event stream.
170-
*
153+
*
171154
* @param filters
172155
* key/value filters to apply to the event stream
173156
*/
@@ -185,7 +168,7 @@ public void allowEventStream(Map<String, String> filters) {
185168
/**
186169
* Generates a new token based on the credentials and permissions that
187170
* previously has been granted to this token.
188-
*
171+
*
189172
* @return the newly generated token that is valid for 3600 seconds
190173
* @throws DomainException
191174
*/
@@ -196,7 +179,7 @@ public String generateToken() throws DomainException {
196179
/**
197180
* Generates a new token based on the credentials and permissions that
198181
* previously has been granted to this token.
199-
*
182+
*
200183
* @param ttl the number of seconds before this token expires
201184
* @return the newly generated token that is valid for ttl seconds
202185
* @throws DomainException
@@ -263,60 +246,9 @@ private void buildIncomingScope() {
263246
}
264247
}
265248

266-
private static String jwtEncode(Map<String, Object> payload, String key)
267-
throws InvalidKeyException, NoSuchAlgorithmException,
268-
UnsupportedEncodingException {
269-
270-
Map<String, Object> header = new LinkedHashMap<String, Object>();
271-
header.put("typ", "JWT");
272-
header.put("alg", "HS256");
273-
274-
List<String> segments = new ArrayList<String>();
275-
segments.add(encodeBase64(jsonEncode(header)));
276-
segments.add(encodeBase64(jsonEncode(payload)));
277-
278-
String signingInput = StringUtils.join(segments, ".");
279-
String signature = sign(signingInput, key);
280-
segments.add(signature);
281-
282-
return StringUtils.join(segments, ".");
283-
}
284-
285-
private static String jsonEncode(Object object) {
286-
String json = JSONValue.toJSONString(object);
287-
return json.replace("\\/", "/");
288-
}
289-
290-
private static String encodeBase64(String data)
291-
throws UnsupportedEncodingException {
292-
return encodeBase64(data.getBytes("UTF-8"));
293-
}
294-
295-
private static String encodeBase64(byte[] data)
296-
throws UnsupportedEncodingException {
297-
String encodedString = new String(Base64.encodeBase64(data));
298-
String safeString = encodedString.replace('+', '-').replace('/', '_')
299-
.replace("=", "");
300-
return safeString;
301-
}
302-
303-
// see
304-
// http://discussion.forum.nokia.com/forum/showthread.php?130974-Help-required-How-to-generate-a-MAC-(HMAC-SHA1)-with-Java
305-
private static String sign(String data, String key)
306-
throws NoSuchAlgorithmException, InvalidKeyException,
307-
UnsupportedEncodingException {
308-
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"),
309-
"HmacSHA256");
310-
Mac mac = Mac.getInstance("HmacSHA256");
311-
mac.init(signingKey);
312-
byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
313-
String result = encodeBase64(rawHmac);
314-
return result;
315-
}
316-
317249
/**
318250
* Example usage
319-
*
251+
*
320252
* @param args
321253
*/
322254
public static void main(String[] args) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.twilio.sdk.taskrouter;
2+
3+
public enum FilterRequirement {
4+
REQUIRED,
5+
OPTIONAL;
6+
7+
public String toString() {
8+
return this.name().toLowerCase();
9+
}
10+
11+
}

0 commit comments

Comments
 (0)
0