Skip to content

Make authentication methods immutable #12

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
Dec 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions spec/Authentication/BasicAuthSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,11 @@ function it_has_a_username()
$this->getUsername()->shouldReturn('john.doe');
}

function it_accepts_a_username()
{
$this->setUsername('jane.doe');

$this->getUsername()->shouldReturn('jane.doe');
}

function it_has_a_password()
{
$this->getPassword()->shouldReturn('secret');
}

function it_accepts_a_password()
{
$this->setPassword('very_secret');

$this->getPassword()->shouldReturn('very_secret');
}

function it_authenticates_a_request(RequestInterface $request, RequestInterface $newRequest)
{
$request->withHeader('Authorization', 'Basic '.base64_encode('john.doe:secret'))->willReturn($newRequest);
Expand Down
7 changes: 0 additions & 7 deletions spec/Authentication/BearerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ function it_has_a_token()
$this->getToken()->shouldReturn('token');
}

function it_accepts_a_token()
{
$this->setToken('another_token');

$this->getToken()->shouldReturn('another_token');
}

function it_authenticates_a_request(RequestInterface $request, RequestInterface $newRequest)
{
$request->withHeader('Authorization', 'Bearer token')->willReturn($newRequest);
Expand Down
31 changes: 3 additions & 28 deletions spec/Authentication/ChainSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,11 @@ function it_accepts_an_authentication_chain_in_the_constructor(Authentication $a
$this->getAuthenticationChain()->shouldReturn($chain);
}

function it_sets_the_authentication_chain(Authentication $auth1, Authentication $auth2)
function it_throws_an_exception_when_non_authentication_is_passed()
{
// This SHOULD be replaced
$this->beConstructedWith([$auth1]);
$this->beConstructedWith(['authentication']);

$this->setAuthenticationChain([$auth2]);

$this->getAuthenticationChain()->shouldReturn([$auth2]);
}

function it_adds_an_authentication_method(Authentication $auth1, Authentication $auth2)
{
// This SHOULD NOT be replaced
$this->beConstructedWith([$auth1]);

$this->addAuthentication($auth2);

$this->getAuthenticationChain()->shouldReturn([$auth1, $auth2]);
}

function it_clears_the_authentication_chain(Authentication $auth1, Authentication $auth2)
{
// This SHOULD be replaced
$this->beConstructedWith([$auth1]);

$this->clearAuthenticationChain();

$this->addAuthentication($auth2);

$this->getAuthenticationChain()->shouldReturn([$auth2]);
$this->shouldThrow('InvalidArgumentException')->duringInstantiation();
}

function it_authenticates_a_request(
Expand Down
18 changes: 1 addition & 17 deletions spec/Authentication/MatchingSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,11 @@ function it_has_an_authentication(Authentication $authentication)
$this->getAuthentication()->shouldReturn($authentication);
}

function it_accepts_an_authentication(Authentication $anotherAuthentication)
{
$this->setAuthentication($anotherAuthentication);

$this->getAuthentication()->shouldReturn($anotherAuthentication);
}

function it_has_a_matcher()
{
$this->getMatcher()->shouldReturn($this->matcher);
}

function it_accepts_a_matcher()
{
$matcher = function($request) { return false; };

$this->setMatcher($matcher);

$this->getMatcher()->shouldReturn($matcher);
}

function it_authenticates_a_request(Authentication $authentication, RequestInterface $request, RequestInterface $newRequest)
{
$authentication->authenticate($request)->willReturn($newRequest);
Expand All @@ -61,7 +45,7 @@ function it_does_not_authenticate_a_request(Authentication $authentication, Requ
{
$matcher = function($request) { return false; };

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

$authentication->authenticate($request)->shouldNotBeCalled();

Expand Down
14 changes: 0 additions & 14 deletions spec/Authentication/WsseSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,11 @@ function it_has_a_username()
$this->getUsername()->shouldReturn('john.doe');
}

function it_accepts_a_username()
{
$this->setUsername('jane.doe');

$this->getUsername()->shouldReturn('jane.doe');
}

function it_has_a_password()
{
$this->getPassword()->shouldReturn('secret');
}

function it_accepts_a_password()
{
$this->setPassword('very_secret');

$this->getPassword()->shouldReturn('very_secret');
}

function it_authenticates_a_request(
RequestInterface $request,
RequestInterface $newRequest,
Expand Down
10 changes: 0 additions & 10 deletions src/Authentication/Bearer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ public function getToken()
return $this->token;
}

/**
* Sets the token.
*
* @param string $token
*/
public function setToken($token)
{
$this->token = $token;
}

/**
* {@inheritdoc}
*/
Expand Down
42 changes: 8 additions & 34 deletions src/Authentication/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@ final class Chain implements Authentication
*/
public function __construct(array $authenticationChain = [])
{
$this->setAuthenticationChain($authenticationChain);
}
foreach ($authenticationChain as $authentication) {
if (!$authentication instanceof Authentication) {
throw new \InvalidArgumentException(
'Members of the authentication chain must be of type Http\Message\Authentication'
);
}
}

/**
* Adds an Authentication method to the chain.
*
* The order of authentication methods SHOULD NOT matter.
*
* @param Authentication $authentication
*/
public function addAuthentication(Authentication $authentication)
{
$this->authenticationChain[] = $authentication;
$this->authenticationChain = $authenticationChain;
}

/**
Expand All @@ -47,28 +43,6 @@ public function getAuthenticationChain()
return $this->authenticationChain;
}

/**
* Replaces the current authentication chain.
*
* @param array $authenticationChain
*/
public function setAuthenticationChain(array $authenticationChain)
{
$this->clearAuthenticationChain();

foreach ($authenticationChain as $authentication) {
$this->addAuthentication($authentication);
}
}

/**
* Clears the authentication chain.
*/
public function clearAuthenticationChain()
{
$this->authenticationChain = [];
}

/**
* {@inheritdoc}
*/
Expand Down
20 changes: 0 additions & 20 deletions src/Authentication/Matching.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ public function getAuthentication()
return $this->authentication;
}

/**
* Sets the authentication.
*
* @param Authentication $authentication
*/
public function setAuthentication(Authentication $authentication)
{
$this->authentication = $authentication;
}

/**
* Returns the matcher.
*
Expand All @@ -68,16 +58,6 @@ public function getMatcher()
return $this->matcher;
}

/**
* Sets the matcher.
*
* @param callable $matcher
*/
public function setMatcher(callable $matcher)
{
$this->matcher = $matcher;
}

/**
* {@inheritdoc}
*/
Expand Down
20 changes: 0 additions & 20 deletions src/Authentication/UserPasswordPair.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ public function getUsername()
return $this->username;
}

/**
* Sets the username.
*
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}

/**
* Returns the password.
*
Expand All @@ -48,14 +38,4 @@ public function getPassword()
{
return $this->password;
}

/**
* Sets the password.
*
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
}