Skip to content

Commit

Permalink
Merge pull request #6 from paragonie/adapter-injectable
Browse files Browse the repository at this point in the history
Proposal: Inject adapters, punt to adapter where appropriate.
  • Loading branch information
paragonie-scott authored Jun 22, 2017
2 parents 395886a + 922bb96 commit eac0503
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 136 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use ParagonIE\Sapient\Exception\InvalidMessageException;
$http = new Client([
'base_uri' => 'https://your-api.example.com'
]);
$adapter = new GuzzleAdapter($http);
$sapient = new Sapient(new GuzzleAdapter($http));

// Keys
$clientSigningKey = new SigningSecretKey(
Expand All @@ -83,7 +83,7 @@ $myMessage = [
];

// Create the signed request:
$request = $adapter->createSignedJsonRequest(
$request = $sapient->createSignedJsonRequest(
'POST',
'/my/api/endpoint',
$myMessage,
Expand All @@ -93,7 +93,7 @@ $request = $adapter->createSignedJsonRequest(
$response = $http->send($request);
try {
/** @var array $verifiedResponse */
$verifiedResponse = Sapient::decodeSignedJsonResponse(
$verifiedResponse = $sapient->decodeSignedJsonResponse(
$response,
$serverPublicKey
);
Expand Down Expand Up @@ -121,7 +121,7 @@ use ParagonIE\Sapient\Exception\InvalidMessageException;
$http = new Client([
'base_uri' => 'https://your-api.example.com'
]);
$adapter = new GuzzleAdapter($http);
$sapient = new Sapient(new GuzzleAdapter($http));

$clientPublicKey = new SigningPublicKey(
Base64UrlSafe::decode(
Expand All @@ -131,7 +131,7 @@ $clientPublicKey = new SigningPublicKey(
$request = ServerRequest::fromGlobals();
try {
/** @var array $decodedRequest */
$decodedRequest = Sapient::decodeSignedJsonRequest(
$decodedRequest = $sapient->decodeSignedJsonRequest(
$request,
$clientPublicKey
);
Expand All @@ -157,7 +157,7 @@ $responseMessage = [
]
];

$response = $adapter->createSignedJsonResponse(
$response = $sapient->createSignedJsonResponse(
200,
$responseMessage,
$serverSignSecret
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
"type": "library",
"require": {
"php": "^7",
"guzzlehttp/guzzle": "^6",
"guzzlehttp/psr7": "^1",
"paragonie/constant_time_encoding": "^2",
"paragonie/sodium_compat": "^1"
"paragonie/sodium_compat": "^1",
"psr/http-message": "^1"
},
"require-dev": {
"guzzlehttp/guzzle": "^6",
"phpunit/phpunit": "^6",
"vimeo/psalm": "dev-master"
},
"suggest": {
"guzzlehttp/guzzle": ">= 6.0 -- Sapient can be used a Guzzle adapter"
},
"autoload": {
"psr-4": {
"ParagonIE\\Sapient\\": "src"
Expand Down
11 changes: 10 additions & 1 deletion src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
};
use Psr\Http\Message\{
RequestInterface,
ResponseInterface
ResponseInterface,
StreamInterface
};


Expand Down Expand Up @@ -336,4 +337,12 @@ public function createSignedResponse(
array $headers = [],
string $version = '1.1'
);

/**
* Adapter-specific way of converting a string into a StreamInterface
*
* @param string $input
* @return StreamInterface
*/
public function stringToStream(string $input): StreamInterface;
}
20 changes: 19 additions & 1 deletion src/Adapter/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Request,
Response
};
use function GuzzleHttp\Psr7\stream_for;
use ParagonIE\ConstantTime\Base64UrlSafe;
use ParagonIE\Sapient\Exception\{
InvalidMessageException
Expand All @@ -21,7 +22,8 @@
use ParagonIE\Sapient\Simple;
use Psr\Http\Message\{
RequestInterface,
ResponseInterface
ResponseInterface,
StreamInterface
};

/**
Expand Down Expand Up @@ -583,4 +585,20 @@ public function createSignedResponse(
$version
);
}

/**
* Adapter-specific way of converting a string into a StreamInterface
*
* @param string $input
* @return StreamInterface
* @throws \TypeError
*/
public function stringToStream(string $input): StreamInterface
{
$stream = stream_for($input);
if (!($stream instanceof StreamInterface)) {
throw new \TypeError('Could not convert string to a stream');
}
return $stream;
}
}
Loading

0 comments on commit eac0503

Please sign in to comment.