public function testGenerateNonceStr()
 {
     $nonce_array = array();
     for ($i = 0; $i < 10000; $i++) {
         $nonce_array[] = OAuth2Util::generateNonceStr(time() - 10);
         $this->assertNotNull($nonce_array[$i]);
         $this->assertEquals(strlen($nonce_array[$i]), 35);
     }
     $this->assertEquals($nonce_array, array_unique($nonce_array));
 }
 /**
  * Generate MAC String
  * @param string $key_id MAC key identifier
  * @param string $key MAC key
  * @param string $algorithm MAC algorithm
  * @param int $iss Issue time
  * @param string $nonce
  * @param string $method
  * @param string $url
  * @param string $bodyhash request payload body hash
  * @param string $ext "ext" "Authorization" request header field attribute
  * @return string
  */
 public static function generateMac($key_id, $key, $algorithm, $iss, $nonce = null, $method, $url, $bodyhash = null, $ext = null)
 {
     // Check MAC Credentials
     if (empty($key_id) || empty($key) || empty($algorithm) || empty($nonce) && empty($iss)) {
         throw new Exception('Missing MAC Credentials');
     }
     // Process nonce
     if (empty($nonce)) {
         $nonce = OAuth2Util::generateNonceStr($iss);
     }
     // Check request data
     if (empty($method) || empty($url)) {
         throw new Exception('Missing Params');
     }
     $host = "";
     $port = "";
     $request_uri = "";
     $urlinfo = parse_url($url);
     if (!$urlinfo) {
         throw new Exception('Invalid URL');
     } else {
         if ($urlinfo['scheme'] != 'https' && $urlinfo['scheme'] != 'http') {
             throw new Exception('Invalid URL Scheme');
         }
         $host = $urlinfo['host'];
         if (isset($urlinfo['port']) && !empty($urlinfo['port'])) {
             $port = $urlinfo['port'];
         } else {
             if ($urlinfo['scheme'] == 'https') {
                 $port = '443';
             } else {
                 if ($urlinfo['scheme'] == 'http') {
                     $port = '80';
                 }
             }
         }
         $request_uri = substr($url, strpos($url, $urlinfo['path']));
     }
     $basestr = $nonce . "\n" . $method . "\n" . $request_uri . "\n" . $host . "\n" . $port . "\n" . $bodyhash . "\n" . $ext . "\n";
     return self::_calculateMac($basestr, $key, $algorithm);
 }
 /**
  * Validate timestamp param
  * @param string $validsec
  */
 public function validateTimestamp($validsec)
 {
     if ($this->_timestamp > OAuth2Util::generateTimestamp() + (int) $validsec || $this->_timestamp < OAuth2Util::generateTimestamp() - (int) $validsec) {
         $this->_enabled = false;
         $this->_code = 'HTTP/1.1 400 Bad Request';
         $this->_error = 'invalid_timestamp';
     }
 }