public function getHeader()
 {
     if (null !== $this->header) {
         return $this->header;
     }
     $attributes = $this->attributes;
     if ($this->getMessage()) {
         $attributes['error'] = $this->getMessage();
     }
     return $this->header = HeaderFactory::create('WWW-Authenticate', $attributes);
 }
示例#2
0
 public function authenticate(CredentialsInterface $credentials, Request $request, $headerObjectOrString, array $options = array())
 {
     if (is_string($headerObjectOrString)) {
         $header = HeaderFactory::createFromString('Server-Authorization', $headerObjectOrString);
     } elseif ($headerObjectOrString instanceof Header) {
         $header = $headerObjectOrString;
     } else {
         throw new \InvalidArgumentException("Header must either be a string or an instance of 'Dflydev\\Hawk\\Header\\Header'");
     }
     if (isset($options['payload']) || isset($options['content_type'])) {
         if (isset($options['payload']) && isset($options['content_type'])) {
             $payload = $options['payload'];
             $contentType = $options['content_type'];
         } else {
             throw new \InvalidArgumentException("If one of 'payload' and 'content_type' are specified, both must be specified.");
         }
     } else {
         $payload = null;
         $contentType = null;
     }
     if ($ts = $header->attribute('ts')) {
         // do something with ts
     }
     $artifacts = new Artifacts($request->artifacts()->method(), $request->artifacts()->host(), $request->artifacts()->port(), $request->artifacts()->resource(), $request->artifacts()->timestamp(), $request->artifacts()->nonce(), $header->attribute('ext'), $payload, $contentType, $header->attribute('hash'), $request->artifacts()->app(), $request->artifacts()->dlg());
     $mac = $this->crypto->calculateMac('response', $credentials, $artifacts);
     if ($header->attribute('mac') !== $mac) {
         return false;
     }
     if (!$payload) {
         return true;
     }
     if (!$artifacts->hash()) {
         return false;
     }
     $hash = $this->crypto->calculatePayloadHash($payload, $credentials->algorithm(), $contentType);
     return $artifacts->hash() === $hash;
 }
示例#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);
 }