Skip to content

Commit c6754c3

Browse files
committed
Merge pull request #7 from manasdpradhan/develop
Enforced using correct sessionIds while generating tokens.
2 parents 9d158b0 + 35ffdc7 commit c6754c3

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

src/main/java/com/opentok/api/OpenTokSDK.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@
1212

1313
import java.io.UnsupportedEncodingException;
1414
import java.net.URLEncoder;
15-
import java.util.Arrays;
16-
import java.util.Date;
1715
import java.util.HashMap;
1816
import java.util.Iterator;
1917
import java.util.List;
20-
import java.util.Random;
2118
import java.util.Map;
19+
import java.util.Random;
20+
21+
import javax.xml.bind.DatatypeConverter;
2222

23-
import com.opentok.api.constants.SessionProperties;
2423
import com.opentok.api.constants.RoleConstants;
24+
import com.opentok.api.constants.SessionProperties;
2525
import com.opentok.exception.OpenTokException;
2626
import com.opentok.util.Base64;
2727
import com.opentok.util.GenerateMac;
2828
import com.opentok.util.TokBoxXML;
29-
import com.opentok.api.OpenTokSession;
3029

3130
public class OpenTokSDK {
3231

@@ -42,12 +41,35 @@ public OpenTokSDK(int api_key, String api_secret) {
4241
*
4342
* Generate a token which is passed to the JS API to enable widgets to connect to the Opentok api.
4443
*
45-
* @session_id: Specify a session_id to make this token only valid for that session_id.
44+
* * @session_id: Specify a session_id to make this token only valid for that session_id. Tokens generated without a valid sessionId will be rejected and the client might be disconnected.
4645
* @role: One of the constants defined in RoleConstants. Default is publisher, look in the documentation to learn more about roles.
4746
* @expire_time: Integer timestamp. You can override the default token expire time of 24h by choosing an explicit expire time. Can be up to 7d after create_time.
4847
*/
4948
public String generate_token(String session_id, String role, Long expire_time, String connection_data) throws OpenTokException {
50-
Long create_time = new Long(System.currentTimeMillis() / 1000).longValue();
49+
50+
if(session_id == null || session_id == "") {
51+
throw new OpenTokException("SessionId cannot be null or empty.");
52+
}
53+
String decodedSessionId = "";
54+
try {
55+
String subSessionId = session_id.substring(2);
56+
for (int i = 0; i<3; i++){
57+
String newSessionId = subSessionId.concat(repeatString("=",i));
58+
decodedSessionId = new String(DatatypeConverter.parseBase64Binary(
59+
newSessionId.replace('-', '+').replace('_', '/')), "ISO8859_1");
60+
if (decodedSessionId.contains("~")){
61+
break;
62+
}
63+
}
64+
if(!decodedSessionId.split("~")[1].equals(String.valueOf(api_key))) {
65+
throw new OpenTokException("SessionId does not belong to the same partnerId");
66+
}
67+
} catch (Exception e) {
68+
throw new OpenTokException("SessionId cannot be invalid.");
69+
}
70+
71+
72+
Long create_time = new Long(System.currentTimeMillis() / 1000).longValue();
5173
StringBuilder data_string_builder = new StringBuilder();
5274
//Build the string
5375
Random random = new Random();
@@ -110,6 +132,7 @@ public String generate_token(String session_id, String role, Long expire_time, S
110132

111133
return token_string_builder.toString();
112134
}
135+
113136

114137
/**
115138
* Creates a new session.
@@ -163,6 +186,12 @@ public OpenTokSession create_session(String location, Map<String, String> params
163186
String session_id = xmlResponse.getElementValue("session_id", "Session");
164187
return new OpenTokSession(session_id);
165188
}
189+
190+
private static String repeatString(String str, int times){
191+
StringBuilder ret = new StringBuilder();
192+
for(int i = 0;i < times;i++) ret.append(str);
193+
return ret.toString();
194+
}
166195

167196
protected TokBoxXML do_request(String url, Map<String, String> params) throws OpenTokException {
168197
TokBoxNetConnection n = new TokBoxNetConnection();

src/test/java/com/opentok/test/UnitTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void testRoleGarbageInput() {
183183
OpenTokException expected = null;
184184
try {
185185
String s= sdk.create_session().session_id;
186-
String t = sdk.generate_token(s, "asdfasdf");
186+
sdk.generate_token(s, "asdfasdf");
187187
} catch (OpenTokException e) {
188188
expected = e;
189189
}
@@ -202,6 +202,39 @@ public void testRoleNull() {
202202
Assert.assertNotNull("Java SDK tests: exception should be thrown for role null", expected);
203203
}
204204

205+
@Test
206+
public void testTokenNullSessionId() throws OpenTokException {
207+
OpenTokException expected = null;
208+
try {
209+
sdk.generate_token(null);
210+
} catch (OpenTokException e) {
211+
expected = e;
212+
}
213+
Assert.assertNotNull("Java SDK tests: exception should be thrown for null sessionId", expected);
214+
}
215+
216+
217+
public void testTokenEmptySessionId() throws OpenTokException {
218+
OpenTokException expected = null;
219+
try {
220+
sdk.generate_token("");
221+
} catch (OpenTokException e) {
222+
expected = e;
223+
}
224+
Assert.assertNotNull("Java SDK tests: exception should be thrown for empty sessionId", expected);
225+
}
226+
227+
@Test
228+
public void testTokenIncompleteSessionId() throws OpenTokException {
229+
OpenTokException expected = null;
230+
try {
231+
sdk.generate_token("jkasjda2ndasd");
232+
} catch (OpenTokException e) {
233+
expected = e;
234+
}
235+
Assert.assertNotNull("Java SDK tests: exception should be thrown for invalid sessionId", expected);
236+
}
237+
205238
@Test
206239
public void testTokenExpireTimeDefault() throws OpenTokException {
207240
String s= sdk.create_session().session_id;

0 commit comments

Comments
 (0)