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
 private function generateNormalizedString($type, Artifacts $attributes)
 {
     $normalized = 'hawk.' . self::HEADER_VERSION . '.' . $type . "\n" . $attributes->timestamp() . "\n" . $attributes->nonce() . "\n" . strtoupper($attributes->method()) . "\n" . $attributes->resource() . "\n" . strtolower($attributes->host()) . "\n" . $attributes->port() . "\n" . $attributes->hash() . "\n";
     if ($attributes->ext()) {
         // TODO: escape ext
         $normalized .= $attributes->ext();
     }
     $normalized .= "\n";
     if ($attributes->app()) {
         $normalized .= $attributes->app() . "\n" . $attributes->dlg() . "\n";
     }
     return $normalized;
 }
Example #3
0
 public function createHeader(CredentialsInterface $credentials, Artifacts $artifacts, array $options = array())
 {
     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;
     $responseArtifacts = new Artifacts($artifacts->method(), $artifacts->host(), $artifacts->port(), $artifacts->resource(), $artifacts->timestamp(), $artifacts->nonce(), $ext, $payload, $contentType, $hash, $artifacts->app(), $artifacts->dlg());
     $attributes = array('mac' => $this->crypto->calculateMac('response', $credentials, $responseArtifacts));
     if ($hash) {
         $attributes['hash'] = $hash;
     }
     if ($ext) {
         $attributes['ext'] = $ext;
     }
     return HeaderFactory::create('Server-Authorization', $attributes);
 }