2

Anyone can describe how i generate token in php using developer key, application id and username. Videyo provide only html js, they haven't any support for php. Please hint me. The token length is 200 plus.

Chirayu Vyas
  • 67
  • 11
  • have you look at this look at Scott answer https://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string – Maytham Fahmi Nov 14 '17 at 05:18
  • @maytham-ɯɐɥʇʎɐɯ I have three things developer key, application id and username how i merge all three segment and generate a token, any function you have provide me, i already check this link, but it is simple generate token. – Chirayu Vyas Nov 14 '17 at 05:33

3 Answers3

5

Here is a sample php code to generate tokens. Don't forget to replace the $DEV_KEY and $APP_ID with your developer key and app Id, which you can find on - https://developer.vidyo.io/dashboard. For username, don't add any special characters especially the "@" sysmbol. The code appends the appId to the username using "@", so if using that in the username that will mess up the decoding of the app Id. For shorter or longer token duration change the $expiresInSecs value.

<!DOCTYPE html>
    <html>
      <head>
        <title>Token Generation php script</title>
      </head>
      <body>
        <p><?php echo "Token Generation Sample <br />";
        $DEV_KEY = "XXXXX" ;            // Copy your dev key from vidyo.io dashboard
        $APP_ID = "XXXXX.vidyo.io" ;    // Copy your app Id from vidyo.io dashboard
        $username = "Batman" ;          // Username, hard coded for debug purposes
        $expiresInSecs = 1000 ;         // Generated token will expire after these many seconds

        // time() by default subtracts datetime(1970, 1, 1) from the datetime
        // on which we call it, therefore the number of seconds is smaller
        // by (pseudocode!) seconds("1970-01-01").
        // In Erlang, on the other hand, we get the actual number of seconds,
        // hence we adjust for this difference here.
        // IMPORTANT! A 64bit architecture is assumed! Otherwise, the timestamp
        // might be stored as a 32bit integer, therefore limiting the "capacity"
        // to 2038 (see https://en.wikipedia.org/wiki/Year_2038_problem).
        $EPOCH_SECONDS = 62167219200 ;
        $expires = $EPOCH_SECONDS + $expiresInSecs + time();

        echo "<br />" ;
        echo "Developer key" . "\t" ."\t" ."\t" . ":" . $DEV_KEY . "<br />" ;
        echo "App ID          : " . $APP_ID . "<br />" ;
        echo "Username        : " . $username . "<br />" ;
        echo "Expires         : " . date("F j, Y, g:i a", $expiresInSecs + time()) . "<br />" ;

        $jid = $username . "@" . $APP_ID ;
        //echo "JID: " . $jid . "<br />" ;

        // Must place \0 within double quotes, not single quotes.
        $body = "provision" . "\0" . $jid . "\0" . $expires . "\0" . "" ;
        echo "BODY: " . $body . "<br />" ;

        // Important to convert to UTF8. I found this is what made the difference.
        $utf8_body = utf8_encode( $body ) ;
        echo "UTF8 BODY: " . $utf8_body . "<br />" ;

        // Ensure the SHA384 Algo is being used.
        $mac_hash = hash_hmac( "sha384", $utf8_body, $DEV_KEY ) ;
        echo "HMAC (384): " . $mac_hash . "<br />" ;

        // Again, ensure \0 are encapsulated with double quotes. Single quotes does not append the null character to the string
        $serialized = $utf8_body . "\0" . $mac_hash ;
        echo "SERIALIZED: " . $serialized . "<br />" ;

        // Base64 Encode the serialized string
        $b64_encoded = base64_encode( $serialized ) ;
        echo "<br /> B64 ENCODED TOKEN :<br /><br />" . $b64_encoded . "<br />" ;
    ?></p>
      </body>
    </html>
Sachin Hegde
  • 320
  • 1
  • 8
2

You can use below code to generate token:



    class secureToken{
       private static $secretKey = 'Your Desired key(Hash)'; 
       private static $secretIv = 'www.domain.com';
       private static $encryptMethod = "AES-256-CBC"; 
       public static function tokenencrypt($data) {
          $key = hash('sha256', self::$secretKey);
          $iv = substr(hash('sha256', self::$secretIv), 0, 16);
          $result = openssl_encrypt($data, self::$encryptMethod, $key, 0, $iv);
          return $result= base64_encode($result);
       }
       public static function tokendecrypt($data) {
          $key = hash('sha256', self::$secretKey);
          $iv = substr(hash('sha256', self::$secretIv), 0, 16);
          $result = openssl_decrypt(base64_decode($data), self::$encryptMethod, $key, 0, $iv);
          return $result;
       }
    }



method to use :



    $tokenencrypt = secureToken::tokenencrypt(Your APP ID and Userid);
    $tokendecrypt = secureToken::tokendecrypt($tokenencrypt);


0

You can use twilo php to generate your token with given parameters.

twilo docs

Prashant Pokhriyal
  • 3,727
  • 4
  • 28
  • 40
Nishantha Kumara
  • 345
  • 2
  • 16