-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
216 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
build | ||
vendor | ||
composer.lock | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/QueryAuth/Request/Adapter/Incoming/Yii2RequestAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace QueryAuth\Request\Adapter\Incoming; | ||
|
||
use QueryAuth\Request\IncomingRequestInterface; | ||
use QueryAuth\Request\RequestInterface; | ||
use \yii\web\Request; | ||
|
||
/** | ||
* Incoming request adapter for Yii2 | ||
*/ | ||
class Yii2RequestAdapter implements IncomingRequestInterface, RequestInterface | ||
{ | ||
/** | ||
* @var Request request | ||
*/ | ||
protected $request; | ||
|
||
/** | ||
* Public constructor | ||
* | ||
* @param Request $request | ||
*/ | ||
public function __construct(Request $request) | ||
{ | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getMethod() | ||
{ | ||
return $this->request->getMethod(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getHost() | ||
{ | ||
return parse_url($this->request->getHostInfo())['host']; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getPath() | ||
{ | ||
return $this->request->getPathInfo(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getParams() | ||
{ | ||
if (strtolower($this->getMethod()) === 'get') { | ||
return $this->request->get(); | ||
} | ||
|
||
if (strtolower($this->getMethod()) === 'delete') { | ||
return array_merge($this->request->get(), $this->request->getBodyParams()); | ||
} | ||
|
||
return $this->request->post(); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/QueryAuth/Request/Adapter/Outgoing/GuzzleV6RequestAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/** | ||
* Query Auth: Signature generation and validation for REST API query authentication | ||
* | ||
* @copyright 2013-2014 Jeremy Kendall | ||
* @license https://github.com/jeremykendall/query-auth/blob/master/LICENSE MIT | ||
* @link https://github.com/jeremykendall/query-auth | ||
*/ | ||
|
||
namespace QueryAuth\Request\Adapter\Outgoing; | ||
|
||
use Psr\Http\Message\RequestInterface as GuzzleHttpRequestInterface; | ||
use QueryAuth\Request\OutgoingImmutableRequestInterface; | ||
use QueryAuth\Request\OutgoingRequestInterface; | ||
use QueryAuth\Request\RequestInterface; | ||
|
||
/** | ||
* Outgoing request adapter for Guzzle v6 | ||
*/ | ||
class GuzzleV6RequestAdapter implements OutgoingRequestInterface, OutgoingImmutableRequestInterface, RequestInterface | ||
{ | ||
/** | ||
* @var GuzzleHttpRequestInterface Guzzle request interface | ||
*/ | ||
protected $request; | ||
|
||
/** | ||
* Public constructor | ||
* | ||
* @param GuzzleHttpRequestInterface $request | ||
*/ | ||
public function __construct(GuzzleHttpRequestInterface $request) | ||
{ | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getMethod() | ||
{ | ||
return $this->request->getMethod(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getHost() | ||
{ | ||
return $this->request->getUri()->getHost(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getPath() | ||
{ | ||
return $this->request->getUri()->getPath(); | ||
} | ||
|
||
/** | ||
* Gets params | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
public function getParams() | ||
{ | ||
if ($this->getMethod() === 'POST') { | ||
return []; | ||
} | ||
|
||
return \GuzzleHttp\Psr7\parse_query($this->request->getUri()->getQuery()); | ||
} | ||
|
||
/** | ||
* Adds parameter to request | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
public function addParam($key, $value) | ||
{ | ||
$queryParams = \GuzzleHttp\Psr7\parse_query($this->request->getUri()->getQuery()); | ||
$queryParams[$key] = $value; | ||
|
||
$this->request = \GuzzleHttp\Psr7\modify_request($this->request, [ | ||
'query' => \GuzzleHttp\Psr7\build_query($queryParams) | ||
]); | ||
return $this->request; | ||
} | ||
|
||
/** | ||
* Replaces request params | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
public function replaceParams(array $params) | ||
{ | ||
$queryParams = \GuzzleHttp\Psr7\parse_query($this->request->getUri()->getQuery()); | ||
foreach ($params as $key => $value) { | ||
$queryParams[$key] = $value; | ||
} | ||
|
||
$this->request = \GuzzleHttp\Psr7\modify_request($this->request, [ | ||
'query' => \GuzzleHttp\Psr7\build_query($queryParams) | ||
]); | ||
return $this->request; | ||
} | ||
|
||
/** | ||
* @return GuzzleHttpRequestInterface | ||
*/ | ||
public function getRequest() | ||
{ | ||
return $this->request; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/QueryAuth/Request/OutgoingImmutableRequestInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* Query Auth: Signature generation and validation for REST API query authentication | ||
* | ||
* @copyright 2013-2014 Jeremy Kendall | ||
* @license https://github.com/jeremykendall/query-auth/blob/master/LICENSE MIT | ||
* @link https://github.com/jeremykendall/query-auth | ||
*/ | ||
|
||
namespace QueryAuth\Request; | ||
|
||
/** | ||
* Interface for outgoing requests. | ||
* | ||
* Used to facilitate request signing and differentiate from incoming requests. | ||
*/ | ||
interface OutgoingImmutableRequestInterface | ||
{ | ||
/** | ||
* @return mixed | ||
*/ | ||
public function getRequest(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters