Ejemplo n.º 1
0
 /**
  * Creates a token for connecting to an OpenTok session. In order to authenticate a user
  * connecting to an OpenTok session, the client passes a token when connecting to the session.
  * <p>
  * For testing, you can also use the <a href="https://dashboard.tokbox.com/projects">OpenTok
  * dashboard</a> page to generate test tokens.
  *
  * @param string $sessionId The session ID corresponding to the session to which the user
  * will connect.
  *
  * @param array $options This array defines options for the token. This array includes the
  * following keys, all of which are optional:
  *
  * <ul>
  *
  *    <li><code>'role'</code> (string) &mdash; One of the constants defined in the RoleConstants
  *    class. The default role is publisher</li>
  *
  *    <li><code>'expireTime'</code> (int) &mdash; The timestamp for when the token expires,
  *    in milliseconds since the Unix epoch. The default expiration time is 24 hours
  *    after the token creation time. The maximum expiration time is 30 days after the
  *    token creation time.</li>
  *
  *    <li><code>'data'</code> (string) &mdash; 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.</li>
  *
  * </ul>
  *
  * @return string The token string.
  */
 public function generateToken($sessionId, $options = array())
 {
     // unpack optional arguments (merging with default values) into named variables
     $defaults = array('role' => Role::PUBLISHER, 'expireTime' => null, 'data' => null);
     $options = array_merge($defaults, array_intersect_key($options, $defaults));
     list($role, $expireTime, $data) = array_values($options);
     // additional token data
     $createTime = time();
     $nonce = microtime(true) . mt_rand();
     // validate arguments
     Validators::validateSessionIdBelongsToKey($sessionId, $this->apiKey);
     Validators::validateRole($role);
     Validators::validateExpireTime($expireTime, $createTime);
     Validators::validateData($data);
     $dataString = "session_id={$sessionId}&create_time={$createTime}&role={$role}&nonce={$nonce}" . ($expireTime ? "&expire_time={$expireTime}" : '') . ($data ? "&connection_data=" . urlencode($data) : '');
     $sig = $this->_sign_string($dataString, $this->apiSecret);
     return "T1==" . base64_encode("partner_id={$this->apiKey}&sig={$sig}:{$dataString}");
 }