1
1
package com .twilio .sdk .client ;
2
2
3
- import org .apache .commons .codec .binary .Base64 ;
4
3
import org .apache .commons .lang .StringUtils ;
5
- import org .json .simple .JSONValue ;
6
4
7
- import javax .crypto .Mac ;
8
- import javax .crypto .spec .SecretKeySpec ;
9
5
import java .io .UnsupportedEncodingException ;
10
6
import java .net .URLEncoder ;
11
- import java .security .InvalidKeyException ;
12
- import java .security .NoSuchAlgorithmException ;
13
7
import java .util .*;
14
8
15
9
/**
16
10
* This class represents a token that will grant someone access to resources
17
11
* within Twilio.
18
12
*/
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 {
31
14
32
15
private String accountSid ;
33
16
private String authToken ;
@@ -50,7 +33,7 @@ public DomainException(Exception e) {
50
33
* Create a new TwilioCapability with zero permissions. Next steps are to
51
34
* grant access to resources by configuring this token through the functions
52
35
* allowXXXX.
53
- *
36
+ *
F438
code>
54
37
* @param accountSid
55
38
* the account sid to which this token is granted access
56
39
* @param authToken
@@ -65,7 +48,7 @@ public TwilioCapability(String accountSid, String authToken) {
65
48
}
66
49
67
50
private String buildScopeString (String serivce , String priviledge ,
68
- Map <String , String > params ) {
51
+ Map <String , String > params ) {
69
52
70
53
StringBuilder scope = new StringBuilder ();
71
54
@@ -86,7 +69,7 @@ private String buildScopeString(String serivce, String priviledge,
86
69
87
70
/**
88
71
* Allow the user of this token to make outgoing connections.
89
- *
72
+ *
90
73
* @param appSid
91
74
* the application to which this token grants access
92
75
*/
@@ -96,7 +79,7 @@ public void allowClientOutgoing(String appSid) {
96
79
97
80
/**
98
81
* Allow the user of this token to make outgoing connections.
99
- *
82
+ *
100
83
* @param appSid
101
84
* the application to which this token grants access
102
85
* @param params
@@ -156,7 +139,7 @@ private String generateParamString(Map<String, String> params) {
156
139
* If the user of this token should be allowed to accept incoming
157
140
* connections then configure the TwilioCapability through this method and
158
141
* specify the client name.
159
- *
142
+ *
160
143
* @param clientName
161
144
*/
162
145
public void allowClientIncoming (String clientName ) {
@@ -167,7 +150,7 @@ public void allowClientIncoming(String clientName) {
167
150
168
151
/**
169
152
* Allow the user of this token to access their event stream.
170
- *
153
+ *
171
154
* @param filters
172
155
* key/value filters to apply to the event stream
173
156
*/
@@ -185,7 +168,7 @@ public void allowEventStream(Map<String, String> filters) {
185
168
/**
186
169
* Generates a new token based on the credentials and permissions that
187
170
* previously has been granted to this token.
188
- *
171
+ *
189
172
* @return the newly generated token that is valid for 3600 seconds
190
173
* @throws DomainException
191
174
*/
@@ -196,7 +179,7 @@ public String generateToken() throws DomainException {
196
179
/**
197
180
* Generates a new token based on the credentials and permissions that
198
181
* previously has been granted to this token.
199
- *
182
+ *
200
183
* @param ttl the number of seconds before this token expires
201
184
* @return the newly generated token that is valid for ttl seconds
202
185
* @throws DomainException
@@ -263,60 +246,9 @@ private void buildIncomingScope() {
263
246
}
264
247
}
265
248
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
-
317
249
/**
318
250
* Example usage
319
- *
251
+ *
320
252
* @param args
321
253
*/
322
254
public static void main (String [] args ) {
0 commit comments