Example #1
0
 public function createRequest(CredentialsInterface $credentials, $uri, $method, array $options = array())
 {
     $timestamp = isset($options['timestamp']) ? $options['timestamp'] : $this->timeProvider->createTimestamp();
     if ($this->localtimeOffset) {
         $timestamp += $this->localtimeOffset;
     }
     $parsed = parse_url($uri);
     $host = $parsed['host'];
     $resource = isset($parsed['path']) ? $parsed['path'] : '';
     if (isset($parsed['query'])) {
         $resource .= '?' . $parsed['query'];
     }
     $port = isset($parsed['port']) ? $parsed['port'] : ($parsed['scheme'] === 'https' ? 443 : 80);
     $nonce = isset($options['nonce']) ? $options['nonce'] : $this->nonceProvider->createNonce();
     if (isset($options['payload']) || isset($options['content_type'])) {
         if (isset($options['payload']) && isset($options['content_type'])) {
             $payload = $options['payload'];
             $contentType = $options['content_type'];
             $hash = $this->crypto->calculatePayloadHash($payload, $credentials->algorithm(), $contentType);
         } else {
             throw new \InvalidArgumentException("If one of 'payload' and 'content_type' are specified, both must be specified.");
         }
     } else {
         $payload = null;
         $contentType = null;
         $hash = null;
     }
     $ext = isset($options['ext']) ? $options['ext'] : null;
     $app = isset($options['app']) ? $options['app'] : null;
     $dlg = isset($options['dlg']) ? $options['dlg'] : null;
     $artifacts = new Artifacts($method, $host, $port, $resource, $timestamp, $nonce, $ext, $payload, $contentType, $hash, $app, $dlg);
     $attributes = array('id' => $credentials->id(), 'ts' => $artifacts->timestamp(), 'nonce' => $artifacts->nonce());
     if (null !== $hash) {
         $attributes['hash'] = $hash;
     }
     if (null !== $ext) {
         $attributes['ext'] = $ext;
     }
     $attributes['mac'] = $this->crypto->calculateMac('header', $credentials, $artifacts);
     if (null !== $app) {
         $attributes['app'] = $app;
     }
     if (null !== $dlg) {
         $attributes['dlg'] = $dlg;
     }
     return new Request(HeaderFactory::create('Authorization', $attributes), $artifacts);
 }
Example #2
0
 public function calculateTsMac($ts, CredentialsInterface $credentials)
 {
     $normalized = 'hawk.' . self::HEADER_VERSION . '.ts' . "\n" . $ts . "\n";
     return base64_encode(hash_hmac($credentials->algorithm(), $normalized, $credentials->key(), true));
 }
Example #3
0
 public function authenticatePayload(CredentialsInterface $credentials, $payload, $contentType, $hash)
 {
     $calculatedHash = $this->crypto->calculatePayloadHash($payload, $credentials->algorithm(), $contentType);
     return $this->crypto->fixedTimeComparison($calculatedHash, $hash);
 }