Skip to content

Commit 2b9abac

Browse files
committed
Add query param authentication
Add warning about QueryParam auth: it is not recommended
1 parent 9fc3a41 commit 2b9abac

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Authentication;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\UriInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class QueryParamSpec extends ObjectBehavior
10+
{
11+
use AuthenticationBehavior;
12+
13+
function let()
14+
{
15+
$this->beConstructedWith([
16+
'username' => 'username',
17+
'password' => 'password',
18+
]);
19+
}
20+
21+
function it_is_initializable()
22+
{
23+
$this->shouldHaveType('Http\Message\Authentication\QueryParam');
24+
}
25+
26+
function it_authenticates_a_request(
27+
RequestInterface $request,
28+
UriInterface $uri,
29+
RequestInterface $newRequest,
30+
UriInterface $newUri
31+
) {
32+
$request->getUri()->willReturn($uri);
33+
$uri->getQuery()->willReturn('param1=value1&param2[]=value2');
34+
$uri->withQuery('param1=value1&param2%5B0%5D=value2&username=username&password=password')->will(
35+
function ($args) use ($newUri) {
36+
$newUri->getQuery()->willReturn($args[0]);
37+
38+
return $newUri;
39+
}
40+
);
41+
42+
$request->withUri($newUri)->will(function ($args) use ($newRequest) {
43+
$newRequest->getUri()->willReturn($args[0]);
44+
45+
return $newRequest;
46+
});
47+
48+
$authenticatedRequest = $this->authenticate($request);
49+
$authenticatedRequest->shouldBe($newRequest);
50+
51+
$authenticatedUri = $authenticatedRequest->getUri();
52+
$authenticatedUri->shouldBe($newUri);
53+
$authenticatedUri->getQuery()->shouldReturn('param1=value1&param2%5B0%5D=value2&username=username&password=password');
54+
}
55+
}

src/Authentication/QueryParam.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Http\Message\Authentication;
4+
5+
use Http\Message\Authentication;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
/**
9+
* Authenticate a PSR-7 Request by adding parameters to its query.
10+
*
11+
* Note: Although in some cases it can be useful, we do not recommend using query parameters for authentication.
12+
* Credentials in the URL is generally unsafe as they are not encrypted, anyone can see them.
13+
*
14+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
15+
*/
16+
final class QueryParam implements Authentication
17+
{
18+
/**
19+
* @var array
20+
*/
21+
private $params = [];
22+
23+
/**
24+
* @param array $params
25+
*/
26+
public function __construct(array $params)
27+
{
28+
$this->params = $params;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function authenticate(RequestInterface $request)
35+
{
36+
$uri = $request->getUri();
37+
$query = $uri->getQuery();
38+
$params = [];
39+
40+
parse_str($query, $params);
41+
42+
$params = array_merge($params, $this->params);
43+
44+
$query = http_build_query($params);
45+
46+
$uri = $uri->withQuery($query);
47+
48+
return $request->withUri($uri);
49+
}
50+
}

0 commit comments

Comments
 (0)