Skip to content

Commit 1883ec4

Browse files
authored
Merge pull request #113 from madhavc/remove-jwt-from-client-token
Replaces the Partner Auth with the new Token Auth
2 parents 6bd6461 + f93c1a5 commit 1883ec4

File tree

8 files changed

+305
-228
lines changed

8 files changed

+305
-228
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ repositories {
3838
dependencies {
3939
testCompile group: 'junit', name: 'junit', version: '[4.3,5.0['
4040
testCompile group: 'com.github.tomakehurst', name: 'wiremock', version: '[1.45,1.99999)'
41-
testCompile group: 'commons-lang', name: 'commons-lang', version: '[2.6,2.99999)'
41+
compile group: 'commons-lang', name: 'commons-lang', version: '[2.6,2.99999)'
4242
compile group: 'com.ning', name: 'async-http-client', version: '[1.9,2.0['
4343
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '[2.3.1,2.99999)'
4444
compile group: 'commons-validator', name: 'commons-validator', version: '[1.4.0,1.99999)'
4545
compile group: 'commons-codec', name: 'commons-codec', version: '[1.9,1.99999]'
46+
compile group: 'org.bitbucket.b_c', name: 'jose4j', version: '0.5.1'
4647
// TODO: find out how to initialize these dependencies properly, or remove them
4748
//compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5'
4849
//compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.5'

src/main/java/com/opentok/OpenTok.java

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77
*/
88
package com.opentok;
99

10+
import com.fasterxml.jackson.core.JsonProcessingException;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.databind.ObjectReader;
13+
import com.opentok.exception.InvalidArgumentException;
14+
import com.opentok.exception.OpenTokException;
15+
import com.opentok.exception.RequestException;
16+
import com.opentok.util.Crypto;
17+
import com.opentok.util.HttpClient;
18+
import com.opentok.util.TokenGenerator;
19+
import org.xml.sax.InputSource;
20+
21+
import javax.xml.xpath.XPath;
22+
import javax.xml.xpath.XPathExpressionException;
23+
import javax.xml.xpath.XPathFactory;
1024
import java.io.IOException;
1125
import java.io.StringReader;
1226
import java.io.UnsupportedEncodingException;
@@ -15,23 +29,6 @@
1529
import java.util.List;
1630
import java.util.Map;
1731

18-
import javax.xml.xpath.XPath;
19-
import javax.xml.xpath.XPathExpressionException;
20-
import javax.xml.xpath.XPathFactory;
21-
22-
import com.fasterxml.jackson.core.JsonParseException;
23-
import com.fasterxml.jackson.core.JsonProcessingException;
24-
import com.fasterxml.jackson.databind.JsonMappingException;
25-
import com.opentok.exception.OpenTokException;
26-
import com.opentok.exception.InvalidArgumentException;
27-
import com.opentok.exception.RequestException;
28-
import com.opentok.util.Crypto;
29-
import com.opentok.util.HttpClient;
30-
31-
import com.fasterxml.jackson.databind.ObjectMapper;
32-
import com.fasterxml.jackson.databind.ObjectReader;
33-
import org.xml.sax.InputSource;
34-
3532
/**
3633
* Contains methods for creating OpenTok sessions, generating tokens, and working with archives.
3734
* <p>
@@ -48,9 +45,9 @@ public class OpenTok {
4845
private String apiSecret;
4946
protected HttpClient client;
5047
static protected ObjectReader archiveReader = new ObjectMapper()
51-
.reader(Archive.class);
48+
.readerFor(Archive.class);
5249
static protected ObjectReader archiveListReader = new ObjectMapper()
53-
.reader(ArchiveList.class);
50+
.readerFor(ArchiveList.class);
5451
static final String defaultApiUrl = "https://api.opentok.com";
5552

5653
/**
@@ -245,11 +242,8 @@ public Session createSession(SessionProperties properties) throws OpenTokExcepti
245242
String xpathQuery = "/sessions/Session/session_id";
246243

247244
// NOTE: doing this null check twice is kind of ugly
248-
if (properties != null) {
249-
params = properties.toMap();
250-
} else {
251-
params = new SessionProperties.Builder().build().toMap();
252-
}
245+
params = properties != null ? properties.toMap() :
246+
new SessionProperties.Builder().build().toMap();
253247

254248
String xmlResponse = this.client.createSession(params);
255249

@@ -358,12 +352,6 @@ public ArchiveList listArchives(int offset, int count) throws OpenTokException {
358352
String archives = this.client.getArchives(offset, count);
359353
try {
360354
return archiveListReader.readValue(archives);
361-
362-
// if we only wanted Java 7 and above, we could DRY this into one catch clause
363-
} catch (JsonMappingException e) {
364-
throw new RequestException("Exception mapping json: " + e.getMessage());
365-
} catch (JsonParseException e) {
366-
throw new RequestException("Exception mapping json: " + e.getMessage());
367355
} catch (JsonProcessingException e) {
368356
throw new RequestException("Exception mapping json: " + e.getMessage());
369357
} catch (IOException e) {

src/main/java/com/opentok/Session.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package com.opentok;
99

10+
import com.opentok.exception.OpenTokException;
1011
import java.io.UnsupportedEncodingException;
1112
import java.net.URLEncoder;
1213
import java.security.InvalidKeyException;
@@ -18,7 +19,6 @@
1819
import com.opentok.util.Crypto;
1920
import org.apache.commons.codec.binary.Base64;
2021

21-
import com.opentok.exception.OpenTokException;
2222

2323
/**
2424
* Represents an OpenTok session. Use the {@link OpenTok#createSession(SessionProperties properties)}
@@ -103,7 +103,6 @@ public String generateToken() throws OpenTokException {
103103
* @return The token string.
104104
*/
105105
public String generateToken(TokenOptions tokenOptions) throws OpenTokException {
106-
107106
// Token format
108107
//
109108
// | ------------------------------ tokenStringBuilder ----------------------------- |
@@ -177,18 +176,12 @@ public String generateToken(TokenOptions tokenOptions) throws OpenTokException {
177176
Base64.encodeBase64String(
178177
innerBuilder.toString().getBytes("UTF-8")
179178
)
180-
.replace("+", "-")
181-
.replace("/", "_")
179+
.replace("+", "-")
180+
.replace("/", "_")
182181
);
183182

184-
// if we only wanted Java 7 and above, we could DRY this into one catch clause
185-
} catch (SignatureException e) {
186-
throw new OpenTokException("Could not generate token, a signing error occurred.", e);
187-
} catch (NoSuchAlgorithmException e) {
188-
throw new OpenTokException("Could not generate token, a signing error occurred.", e);
189-
} catch (InvalidKeyException e) {
190-
throw new OpenTokException("Could not generate token, a signing error occurred.", e);
191-
} catch (UnsupportedEncodingException e) {
183+
} catch (SignatureException | NoSuchAlgorithmException
184+
| InvalidKeyException | UnsupportedEncodingException e) {
192185
throw new OpenTokException("Could not generate token, a signing error occurred.", e);
193186
}
194187

src/main/java/com/opentok/TokenOptions.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import com.opentok.exception.InvalidArgumentException;
1111

12+
import java.util.List;
13+
1214
/**
1315
* Defines values for the <code>tokenOptions</code> parameter of the
1416
* {@link OpenTok#generateToken(String sessionId, TokenOptions tokenOptions)} method
@@ -19,8 +21,9 @@
1921
public class TokenOptions {
2022

2123
private Role role;
22-
private double expireTime;
24+
private long expireTime;
2325
private String data;
26+
private List<String> initialLayoutClassList;
2427

2528
private TokenOptions(Builder builder) {
2629
this.role = builder.role != null ? builder.role : Role.PUBLISHER;
@@ -30,6 +33,9 @@ private TokenOptions(Builder builder) {
3033

3134
// default value of null means to omit the key "connection_data" from the token
3235
this.data = builder.data;
36+
37+
// default value of null means to omit the key "initialLayoutClassList" from the token
38+
this.initialLayoutClassList = builder.initialLayoutClassList;
3339
}
3440

3541
/**
@@ -41,9 +47,9 @@ public Role getRole() {
4147

4248
/**
4349
* Returns the expiration time the token, as the number of seconds since the UNIX epoch.
44-
* See {@link TokenOptions.Builder#expireTime(double expireTime)}.
50+
* See {@link TokenOptions.Builder#expireTime(long expireTime)}.
4551
*/
46-
public double getExpireTime() {
52+
public long getExpireTime() {
4753
return expireTime;
4854
}
4955

@@ -55,15 +61,20 @@ public String getData() {
5561
return data;
5662
}
5763

64+
public List<String> getInitialLayoutClassList() {
65+
return initialLayoutClassList;
66+
}
67+
5868
/**
5969
* Use this class to create a TokenOptions object.
6070
*
6171
* @see TokenOptions
6272
*/
6373
public static class Builder {
6474
private Role role;
65-
private double expireTime = 0;
75+
private long expireTime = 0;
6676
private String data;
77+
private List<String> initialLayoutClassList;
6778

6879
/**
6980
* Sets the role for the token. Each role defines a set of permissions granted to the token.
@@ -93,7 +104,7 @@ public Builder role(Role role) {
93104
* the default expiration time of 24 hours after the token creation time. The maximum
94105
* expiration time is 30 days after the creation time.
95106
*/
96-
public Builder expireTime(double expireTime) {
107+
public Builder expireTime(long expireTime) {
97108
// NOTE: since this object can be stored/cached, validation should occur at token generation time
98109
this.expireTime = expireTime;
99110
return this;
@@ -116,6 +127,11 @@ public Builder data(String data) throws InvalidArgumentException {
116127
return this;
117128
}
118129

130+
public Builder initialLayoutClassList (List<String> initialLayoutClassList) {
131+
this.initialLayoutClassList = initialLayoutClassList;
132+
return this;
133+
}
134+
119135
/**
120136
* Builds the TokenOptions object.
121137
*

src/main/java/com/opentok/util/HttpClient.java

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,7 @@ public String createSession(Map<String, Collection<String>> params) throws Reque
6060
throw new RequestException("Could not create an OpenTok Session. The server response was invalid." +
6161
" response code: " + response.getStatusCode());
6262
}
63-
64-
// if we only wanted Java 7 and above, we could DRY this into one catch clause
65-
} catch (InterruptedException e) {
66-
throw new RequestException("Could not create an OpenTok Session", e);
67-
} catch (ExecutionException e) {
68-
throw new RequestException("Could not create an OpenTok Session", e);
69-
} catch (IOException e) {
63+
} catch (InterruptedException | ExecutionException | IOException e) {
7064
throw new RequestException("Could not create an OpenTok Session", e);
7165
}
7266
return responseString;
@@ -94,13 +88,7 @@ public String getArchive(String archiveId) throws RequestException {
9488
throw new RequestException("Could not get an OpenTok Archive. The server response was invalid." +
9589
" response code: " + response.getStatusCode());
9690
}
97-
98-
// if we only wanted Java 7 and above, we could DRY this into one catch clause
99-
} catch (InterruptedException e) {
100-
throw new RequestException("Could not get an OpenTok Archive", e);
101-
} catch (ExecutionException e) {
102-
throw new RequestException("Could not get an OpenTok Archive", e);
103-
} catch (IOException e) {
91+
} catch (InterruptedException | ExecutionException | IOException e) {
10492
throw new RequestException("Could not get an OpenTok Archive", e);
10593
}
10694

@@ -137,13 +125,7 @@ public String getArchives(int offset, int count) throws RequestException {
137125
throw new RequestException("Could not get an OpenTok Archive. The server response was invalid." +
138126
" response code: " + response.getStatusCode());
139127
}
140-
141-
// if we only wanted Java 7 and above, we could DRY this into one catch clause
142-
} catch (InterruptedException e) {
143-
throw new RequestException("Could not get OpenTok Archives", e);
144-
} catch (ExecutionException e) {
145-
throw new RequestException("Could not get OpenTok Archives", e);
146-
} catch (IOException e) {
128+
} catch (InterruptedException | ExecutionException | IOException e) {
147129
throw new RequestException("Could not get OpenTok Archives", e);
148130
}
149131

@@ -197,13 +179,7 @@ public String startArchive(String sessionId, ArchiveProperties properties)
197179
throw new RequestException("Could not start an OpenTok Archive. The server response was invalid." +
198180
" response code: " + response.getStatusCode());
199181
}
200-
201-
// if we only wanted Java 7 and above, we could DRY this into one catch clause
202-
} catch (InterruptedException e) {
203-
throw new RequestException("Could not start an OpenTok Archive.", e);
204-
} catch (ExecutionException e) {
205-
throw new RequestException("Could not start an OpenTok Archive.", e);
206-
} catch (IOException e) {
182+
} catch (InterruptedException | ExecutionException | IOException e) {
207183
throw new RequestException("Could not start an OpenTok Archive.", e);
208184
}
209185
return responseString;
@@ -239,13 +215,7 @@ public String stopArchive(String archiveId) throws RequestException {
239215
throw new RequestException("Could not stop an OpenTok Archive. The server response was invalid." +
240216
" response code: " + response.getStatusCode());
241217
}
242-
243-
// if we only wanted Java 7 and above, we could DRY this into one catch clause
244-
} catch (InterruptedException e) {
245-
throw new RequestException("Could not stop an OpenTok Archive.", e);
246-
} catch (ExecutionException e) {
247-
throw new RequestException("Could not stop an OpenTok Archive.", e);
248-
} catch (IOException e) {
218+
} catch (InterruptedException | ExecutionException | IOException e) {
249219
throw new RequestException("Could not stop an OpenTok Archive.", e);
250220
}
251221
return responseString;
@@ -273,13 +243,7 @@ public String deleteArchive(String archiveId) throws RequestException {
273243
throw new RequestException("Could not get an OpenTok Archive. The server response was invalid." +
274244
" response code: " + response.getStatusCode());
275245
}
276-
277-
// if we only wanted Java 7 and above, we could DRY this into one catch clause
278-
} catch (InterruptedException e) {
279-
throw new RequestException("Could not delete an OpenTok Archive. archiveId = " + archiveId, e);
280-
} catch (ExecutionException e) {
281-
throw new RequestException("Could not delete an OpenTok Archive. archiveId = " + archiveId, e);
282-
} catch (IOException e) {
246+
} catch (InterruptedException | ExecutionException | IOException e) {
283247
throw new RequestException("Could not delete an OpenTok Archive. archiveId = " + archiveId, e);
284248
}
285249

@@ -311,7 +275,7 @@ public Builder proxy(Proxy proxy) {
311275
public HttpClient build() {
312276
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder()
313277
.setUserAgent("Opentok-Java-SDK/" + Version.VERSION + " JRE/" + System.getProperty("java.version"))
314-
.addRequestFilter(new PartnerAuthRequestFilter(this.apiKey, this.apiSecret));
278+
.addRequestFilter(new TokenAuthRequestFilter(this.apiKey, this.apiSecret));
315279
if (this.apiUrl == null) {
316280
this.apiUrl=DefaultApiUrl.DEFAULT_API_URI;
317281
}
@@ -352,22 +316,29 @@ static ProxyServer createProxyServer(final Proxy proxy) {
352316
}
353317
}
354318

355-
static class PartnerAuthRequestFilter implements RequestFilter {
319+
static class TokenAuthRequestFilter implements RequestFilter {
356320

357321
private int apiKey;
358322
private String apiSecret;
323+
private final String authHeader = "X-OPENTOK-AUTH";
359324

360-
public PartnerAuthRequestFilter(int apiKey, String apiSecret) {
325+
public TokenAuthRequestFilter(int apiKey, String apiSecret) {
361326
this.apiKey = apiKey;
362327
this.apiSecret = apiSecret;
363328
}
364329

365330
public FilterContext filter(FilterContext ctx) throws FilterException {
366-
return new FilterContext.FilterContextBuilder(ctx)
367-
.request(new RequestBuilder(ctx.getRequest())
368-
.addHeader("X-TB-PARTNER-AUTH", this.apiKey+":"+this.apiSecret)
369-
.build())
370-
.build();
331+
try {
332+
return new FilterContext.FilterContextBuilder(ctx)
333+
.request(new RequestBuilder(ctx.getRequest())
334+
.addHeader(authHeader,
335+
TokenGenerator.generateToken(apiKey, apiSecret))
336+
.build())
337+
.build();
338+
} catch (OpenTokException e) {
339+
e.printStackTrace();
340+
return null;
341+
}
371342
}
372343
}
373344
}

0 commit comments

Comments
 (0)