Skip to content

Add auto BasicAuth credential detection based on URL #38

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

Merged
merged 1 commit into from
Mar 29, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions spec/Authentication/AuthenticationBehavior.php

This file was deleted.

84 changes: 84 additions & 0 deletions spec/Authentication/AutoBasicAuthSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace spec\Http\Message\Authentication;

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

class AutoBasicAuthSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Http\Message\Authentication\AutoBasicAuth');
}

function it_is_an_authentication()
{
$this->shouldImplement('Http\Message\Authentication');
}

function it_authenticates_a_request(
RequestInterface $request,
UriInterface $uri,
UriInterface $uriWithoutUserInfo,
RequestInterface $requestWithoutUserInfo,
RequestInterface $authenticatedRequest
) {
$request->getUri()->willReturn($uri);
$uri->getUserInfo()->willReturn('username:password');
$uri->withUserInfo('', null)->willReturn($uriWithoutUserInfo);
$request->withUri($uriWithoutUserInfo)->willReturn($requestWithoutUserInfo);
$requestWithoutUserInfo
->withHeader('Authorization', 'Basic '.base64_encode('username:password'))
->willReturn($authenticatedRequest)
;

$this->authenticate($request)->shouldReturn($authenticatedRequest);
}

function it_authenticates_a_request_without_password(
RequestInterface $request,
UriInterface $uri,
UriInterface $uriWithoutUserInfo,
RequestInterface $requestWithoutUserInfo,
RequestInterface $authenticatedRequest
) {
$request->getUri()->willReturn($uri);
$uri->getUserInfo()->willReturn('username');
$uri->withUserInfo('', null)->willReturn($uriWithoutUserInfo);
$request->withUri($uriWithoutUserInfo)->willReturn($requestWithoutUserInfo);
$requestWithoutUserInfo
->withHeader('Authorization', 'Basic '.base64_encode('username'))
->willReturn($authenticatedRequest)
;

$this->authenticate($request)->shouldReturn($authenticatedRequest);
}

function it_does_not_authenticate_a_request(RequestInterface $request, UriInterface $uri)
{
$request->getUri()->willReturn($uri);
$uri->getUserInfo()->willReturn('');

$this->authenticate($request)->shouldReturn($request);
}

function it_authenticates_a_request_without_user_info_removal(
RequestInterface $request,
UriInterface $uri,
RequestInterface $authenticatedRequest
) {
$this->beConstructedWith(false);

$request->getUri()->willReturn($uri);
$uri->getUserInfo()->willReturn('username:password');
$request
->withHeader('Authorization', 'Basic '.base64_encode('username:password'))
->willReturn($authenticatedRequest)
;

$this->authenticate($request)->shouldReturn($authenticatedRequest);
}
}
7 changes: 5 additions & 2 deletions spec/Authentication/BasicAuthSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

class BasicAuthSpec extends ObjectBehavior
{
use AuthenticationBehavior;

function let()
{
$this->beConstructedWith('john.doe', 'secret');
Expand All @@ -19,6 +17,11 @@ function it_is_initializable()
$this->shouldHaveType('Http\Message\Authentication\BasicAuth');
}

function it_is_an_authentication()
{
$this->shouldImplement('Http\Message\Authentication');
}

function it_authenticates_a_request(RequestInterface $request, RequestInterface $newRequest)
{
$request->withHeader('Authorization', 'Basic '.base64_encode('john.doe:secret'))->willReturn($newRequest);
Expand Down
7 changes: 5 additions & 2 deletions spec/Authentication/BearerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

class BearerSpec extends ObjectBehavior
{
use AuthenticationBehavior;

function let()
{
$this->beConstructedWith('token');
Expand All @@ -19,6 +17,11 @@ function it_is_initializable()
$this->shouldHaveType('Http\Message\Authentication\Bearer');
}

function it_is_an_authentication()
{
$this->shouldImplement('Http\Message\Authentication');
}

function it_authenticates_a_request(RequestInterface $request, RequestInterface $newRequest)
{
$request->withHeader('Authorization', 'Bearer token')->willReturn($newRequest);
Expand Down
7 changes: 5 additions & 2 deletions spec/Authentication/ChainSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

class ChainSpec extends ObjectBehavior
{
use AuthenticationBehavior;

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

function it_is_an_authentication()
{
$this->shouldImplement('Http\Message\Authentication');
}

function it_throws_an_exception_when_non_authentication_is_passed()
{
$this->beConstructedWith(['authentication']);
Expand Down
13 changes: 7 additions & 6 deletions spec/Authentication/MatchingSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@

class MatchingSpec extends ObjectBehavior
{
use AuthenticationBehavior;

private $matcher;

function let(Authentication $authentication)
{
$this->matcher = function($request) { return true; };
$matcher = function($request) { return true; };

$this->beConstructedWith($authentication, $this->matcher);
$this->beConstructedWith($authentication, $matcher);
}

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

function it_is_an_authentication()
{
$this->shouldImplement('Http\Message\Authentication');
}

function it_authenticates_a_request(Authentication $authentication, RequestInterface $request, RequestInterface $newRequest)
{
$authentication->authenticate($request)->willReturn($newRequest);
Expand Down
7 changes: 5 additions & 2 deletions spec/Authentication/QueryParamSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class QueryParamSpec extends ObjectBehavior
{
use AuthenticationBehavior;

function let()
{
$this->beConstructedWith([
Expand All @@ -23,6 +21,11 @@ function it_is_initializable()
$this->shouldHaveType('Http\Message\Authentication\QueryParam');
}

function it_is_an_authentication()
{
$this->shouldImplement('Http\Message\Authentication');
}

function it_authenticates_a_request(
RequestInterface $request,
UriInterface $uri,
Expand Down
7 changes: 5 additions & 2 deletions spec/Authentication/RequestConditionalSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

class RequestConditionalSpec extends ObjectBehavior
{
use AuthenticationBehavior;

function let(RequestMatcher $requestMatcher, Authentication $authentication)
{
$this->beConstructedWith($requestMatcher, $authentication);
Expand All @@ -21,6 +19,11 @@ function it_is_initializable()
$this->shouldHaveType('Http\Message\Authentication\RequestConditional');
}

function it_is_an_authentication()
{
$this->shouldImplement('Http\Message\Authentication');
}

function it_authenticates_a_request(
Authentication $authentication,
RequestMatcher $requestMatcher,
Expand Down
7 changes: 5 additions & 2 deletions spec/Authentication/WsseSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class WsseSpec extends ObjectBehavior
{
use AuthenticationBehavior;

function let()
{
$this->beConstructedWith('john.doe', 'secret');
Expand All @@ -20,6 +18,11 @@ function it_is_initializable()
$this->shouldHaveType('Http\Message\Authentication\Wsse');
}

function it_is_an_authentication()
{
$this->shouldImplement('Http\Message\Authentication');
}

function it_authenticates_a_request(
RequestInterface $request,
RequestInterface $newRequest,
Expand Down
48 changes: 48 additions & 0 deletions src/Authentication/AutoBasicAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Http\Message\Authentication;

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

/**
* Authenticate a PSR-7 Request using Basic Auth based on credentials in the URI.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
final class AutoBasicAuth implements Authentication
{
/**
* Whether user info should be removed from the URI.
*
* @var bool
*/
private $shouldRemoveUserInfo;

/**
* @param bool|true $shouldRremoveUserInfo
*/
public function __construct($shouldRremoveUserInfo = true)
{
$this->shouldRemoveUserInfo = (bool) $shouldRremoveUserInfo;
}

/**
* {@inheritdoc}
*/
public function authenticate(RequestInterface $request)
{
$uri = $request->getUri();
$userInfo = $uri->getUserInfo();

if (!empty($userInfo)) {
if ($this->shouldRemoveUserInfo) {
$request = $request->withUri($uri->withUserInfo(''));
}

$request = $request->withHeader('Authorization', sprintf('Basic %s', base64_encode($userInfo)));
}

return $request;
}
}