/** * Creates a token for connecting to an OpenTok session. In order to authenticate a user * connecting to an OpenTok session that user must pass an authentication token along with * the API key. * * @param role The role for the token. Valid values are defined in the Role enum: * <ul> * <li> <code>Role.SUBSCRIBER</code> — A subscriber can only subscribe to * streams.</li> * * <li> <code>Role.PUBLISHER</code> — A publisher can publish streams, subscribe to * streams, and signal. (This is the default value if you do not specify a role.)</li> * * <li> <code>Role.MODERATOR</code> — In addition to the privileges granted to a * publisher, in clients using the OpenTok.js library, a moderator can call the * <code>forceUnpublish()</code> and <code>forceDisconnect()</code> method of the * Session object.</li> * </ul> * * @param expireTime The expiration time of the token, in seconds since the UNIX epoch. * Pass in 0 to use the default expiration time of 24 hours after the token creation time. * The maximum expiration time is 30 days after the creation time. * * @param data A string containing connection metadata describing the end-user. For example, * you can pass the user ID, name, or other data describing the end-user. The length of the * string is limited to 1000 characters. This data cannot be updated once it is set. * * @return The token string. */ public string GenerateToken(Role role = Role.PUBLISHER, double expireTime = 0, string data = null) { double createTime = OpenTokUtils.GetCurrentUnixTimeStamp(); int nonce = OpenTokUtils.GetRandomNumber(); string dataString = BuildDataString(role, expireTime, data, createTime, nonce); return(BuildTokenString(dataString)); }
public void GenerateComplexTokenTest() { OpenTok opentok = new OpenTok(apiKey, apiSecret); Session session = opentok.CreateSession(); double expireTime = OpenTokUtils.GetCurrentUnixTimeStamp() + 10; string token = session.GenerateToken(role: Role.MODERATOR, expireTime: expireTime, data: "Connection data"); CheckToken(token, apiKey); }
public void GenerateTokenWithExpireTimeTest() { OpenTok opentok = new OpenTok(apiKey, apiSecret); Session session = opentok.CreateSession(mediaMode: MediaMode.RELAY); double expireTime = OpenTokUtils.GetCurrentUnixTimeStamp() + 10; string token = session.GenerateToken(expireTime: expireTime); CheckToken(token, apiKey); }
private bool CheckExpireTime(double expireTime, double createTime) { if (expireTime == 0) { return(false); } else if (expireTime > createTime && expireTime <= OpenTokUtils.GetCurrentUnixTimeStamp() + 2592000) { return(true); } else { throw new OpenTokArgumentException("Invalid expiration time for token " + expireTime + ". Expiration time " + " has to be positive and less than 30 days"); } }
public void GenerateTokenWithInvalidExpiryTimeTest() { OpenTok opentok = new OpenTok(apiKey, apiSecret); Session session = opentok.CreateSession(); double expireTime = OpenTokUtils.GetCurrentUnixTimeStamp() - 1; string token; try { token = session.GenerateToken(role: Role.MODERATOR, expireTime: expireTime, data: "Connection data"); Assert.False(true); } catch (OpenTokArgumentException) { Assert.True(true); } }
public void GenerateTokenWithConnectionDataTest() { OpenTok opentok = new OpenTok(apiKey, apiSecret); double expireTime = OpenTokUtils.GetCurrentUnixTimeStamp() + 10; string connectionData = "Somedatafortheconnection"; String sessionId = "1_MX4xMjM0NTZ-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4"; string token = opentok.GenerateToken(sessionId, data: connectionData); Assert.NotNull(token); var data = CheckToken(token, apiKey); Assert.Equal(data["partner_id"], apiKey.ToString()); Assert.NotNull(data["sig"]); Assert.NotNull(data["create_time"]); Assert.NotNull(data["nonce"]); Assert.Equal(data["role"], Role.PUBLISHER.ToString()); Assert.Equal(data["connection_data"], connectionData); }