Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
First pass at using PSR 7 interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
navitronic committed Jul 7, 2016
1 parent 27dc209 commit 2d9a9a5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 53 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
},
"require": {
"php": ">=5.5",
"symfony/http-foundation": "~2.8|~3.0"
"symfony/http-foundation": "~2.8|~3.0",
"psr/http-message": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.8",
Expand Down
30 changes: 9 additions & 21 deletions src/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,37 @@

namespace HttpSignatures;

use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\MessageInterface;

class Signature
{
/** @var Request|SymfonyRequestMessage */
private $message;

/** @var Key */
private $key;

/** @var HmacAlgorithm */
private $algorithm;

/** @var HeaderList */
private $headerList;
/** @var SigningString */
private $signingString;

/**
* @param Request|SymfonyRequestMessage $message
* @param Key $key
* @param HmacAlgorithm $algorithm
* @param HeaderList $headerList
* @param MessageInterface $message
* @param Key $key
* @param HmacAlgorithm $algorithm
* @param HeaderList $headerList
*/
public function __construct($message, $key, $algorithm, $headerList)
{
$this->message = $message;
$this->key = $key;
$this->algorithm = $algorithm;
$this->headerList = $headerList;
$this->signingString = new SigningString($headerList, $message);
}

public function string()
{
return $this->algorithm->sign(
$this->key->secret,
$this->signingString()->string()
);
}

private function signingString()
{
return new SigningString(
$this->headerList,
$this->message
$this->signingString->string()
);
}
}
10 changes: 5 additions & 5 deletions src/Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function sign($message)
private function signatureParameters($message)
{
return new SignatureParameters(
$this->key,
$this->algorithm,
$this->headerList,
$this->signature($message)
);
$this->key,
$this->algorithm,
$this->headerList,
$this->signature($message)
);
}

/**
Expand Down
24 changes: 10 additions & 14 deletions src/SigningString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

namespace HttpSignatures;

use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;

class SigningString
{
/** @var HeaderList */
private $headerList;

/** @var Request|SymfonyRequestMessage */
/** @var RequestInterface */
private $message;

/**
* @param HeaderList $headerList
* @param Request|SymfonyRequestMessage $message
* @param HeaderList $headerList
* @param MessageInterface $message
*/
public function __construct($headerList, $message)
{
$this->headerList = $headerList;
if ($message instanceof Request) {
$this->message = new SymfonyRequestMessage($message);
} else {
$this->message = $message;
}
$this->message = $message;
}

/**
Expand Down Expand Up @@ -70,9 +67,8 @@ private function line($name)
*/
private function headerValue($name)
{
$headers = $this->message->headers;
if ($headers->has($name)) {
return $headers->get($name);
if ($this->message->hasHeader($name)) {
return $this->message->getHeader($name);
} else {
throw new SignedHeaderNotPresentException("Header '$name' not in message");
}
Expand All @@ -95,8 +91,8 @@ private function requestTargetLine()
*/
private function getPathWithQueryString()
{
$path = $this->message->getPathInfo();
$qs = $this->message->getQueryString();
$path = $this->message->getUri()->getPath();
$qs = $this->message->getUri()->getQuery();
if ($qs === null) {
return $path;
} else {
Expand Down
16 changes: 7 additions & 9 deletions src/Verification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace HttpSignatures;

use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\MessageInterface;

class Verification
{
/** @var Request|SymfonyRequestMessage */
/** @var MessageInterface */
private $message;

/** @var KeyStoreInterface */
Expand All @@ -16,10 +16,10 @@ class Verification
private $_parameters;

/**
* @param Request|SymfonyRequestMessage $message
* @param KeyStoreInterface $keyStore
* @param MessageInterface $message
* @param KeyStoreInterface $keyStore
*/
public function __construct($message, KeyStoreInterface $keyStore)
public function __construct($message, $keyStore)
{
$this->message = $message;
$this->keyStore = $keyStore;
Expand Down Expand Up @@ -147,7 +147,7 @@ private function parameters()
*/
private function hasSignatureHeader()
{
return $this->message->headers->has('Signature') || $this->message->headers->has('Authorization');
return $this->message->hasHeader('Signature') || $this->message->hasHeader('Authorization');
}

/**
Expand All @@ -173,8 +173,6 @@ private function signatureHeader()
*/
private function fetchHeader($name)
{
$headers = $this->message->headers;

return $headers->has($name) ? $headers->get($name) : null;
return $this->message->getHeader($name);
}
}
5 changes: 2 additions & 3 deletions src/Verifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HttpSignatures;

use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\MessageInterface;

class Verifier
{
Expand All @@ -18,8 +18,7 @@ public function __construct(KeyStoreInterface $keyStore)
}

/**
* @param Request|SymfonyRequestMessage $message
*
* @param MessageInterface $message
* @return bool
*/
public function isValid($message)
Expand Down

0 comments on commit 2d9a9a5

Please sign in to comment.