Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query param authentication #9

Merged
merged 1 commit into from
Dec 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions spec/Authentication/QueryParamSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace spec\Http\Message\Authentication;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use PhpSpec\ObjectBehavior;

class QueryParamSpec extends ObjectBehavior
{
use AuthenticationBehavior;

function let()
{
$this->beConstructedWith([
'username' => 'username',
'password' => 'password',
]);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\Authentication\QueryParam');
}

function it_authenticates_a_request(
RequestInterface $request,
UriInterface $uri,
RequestInterface $newRequest,
UriInterface $newUri
) {
$request->getUri()->willReturn($uri);
$uri->getQuery()->willReturn('param1=value1&param2[]=value2');
$uri->withQuery('param1=value1&param2%5B0%5D=value2&username=username&password=password')->will(
function ($args) use ($newUri) {
$newUri->getQuery()->willReturn($args[0]);

return $newUri;
}
);

$request->withUri($newUri)->will(function ($args) use ($newRequest) {
$newRequest->getUri()->willReturn($args[0]);

return $newRequest;
});

$authenticatedRequest = $this->authenticate($request);
$authenticatedRequest->shouldBe($newRequest);

$authenticatedUri = $authenticatedRequest->getUri();
$authenticatedUri->shouldBe($newUri);
$authenticatedUri->getQuery()->shouldReturn('param1=value1&param2%5B0%5D=value2&username=username&password=password');
}
}
50 changes: 50 additions & 0 deletions src/Authentication/QueryParam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Http\Message\Authentication;

use Http\Message\Authentication;
use Psr\Http\Message\RequestInterface;

/**
* Authenticate a PSR-7 Request by adding parameters to its query.
*
* Note: Although in some cases it can be useful, we do not recommend using query parameters for authentication.
* Credentials in the URL is generally unsafe as they are not encrypted, anyone can see them.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
final class QueryParam implements Authentication
{
/**
* @var array
*/
private $params = [];

/**
* @param array $params
*/
public function __construct(array $params)
{
$this->params = $params;
}

/**
* {@inheritdoc}
*/
public function authenticate(RequestInterface $request)
{
$uri = $request->getUri();
$query = $uri->getQuery();
$params = [];

parse_str($query, $params);

$params = array_merge($params, $this->params);

$query = http_build_query($params);

$uri = $uri->withQuery($query);

return $request->withUri($uri);
}
}