Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Basic Auth Controller for HTTP Basic Auth setup. Extending OAuth from this, and altered how the Bearer Token is configured, by using a method that can be overridden in any class that extends it. Changed DEFAULT AUTH ACTIONS to be protected, so that it can be overridden and defined by extended classes.
- Loading branch information
1 parent
2256af5
commit b8a2caa
Showing
6 changed files
with
146 additions
and
16 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
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,43 @@ | ||
<?php | ||
|
||
namespace MRussell\REST\Auth\Abstracts; | ||
|
||
use MRussell\Http\Request\RequestInterface; | ||
|
||
/** | ||
* Class AbstractBasicController | ||
* @package MRussell\REST\Auth\Abstracts | ||
*/ | ||
class AbstractBasicController extends AbstractAuthController | ||
{ | ||
const DEFAULT_AUTH_HEADER = 'Authorization'; | ||
|
||
const DEFAULT_AUTH_TYPE = 'Basic'; | ||
|
||
protected static $_AUTH_HEADER = self::DEFAULT_AUTH_HEADER; | ||
|
||
protected static $_AUTH_TYPE = self::DEFAULT_AUTH_TYPE; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function configureRequest(RequestInterface $Request) | ||
{ | ||
$Request->addHeader(static::$_AUTH_HEADER, $this->getAuthHeaderValue()); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Parse the Credentials or Token to build out the Auth Header Value | ||
* @return string | ||
*/ | ||
protected function getAuthHeaderValue() | ||
{ | ||
$value = ""; | ||
if (isset($this->credentials['username']) && isset($this->credentials['password'])){ | ||
$value = $this->credentials['username'].":".$this->credentials['password']; | ||
$value = base64_encode($value); | ||
} | ||
return static::$_AUTH_TYPE." ".$value; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* User: mrussell | ||
* Date: 8/15/17 | ||
* Time: 8:50 AM | ||
*/ | ||
|
||
namespace MRussell\REST\Tests\Auth; | ||
|
||
use MRussell\Http\Request\JSON; | ||
use MRussell\REST\Tests\Stubs\Auth\BasicController; | ||
|
||
|
||
/** | ||
* Class AbstractBasicControllerTest | ||
* @package MRussell\REST\Tests\Auth | ||
* @coversDefaultClass MRussell\REST\Auth\Abstracts\AbstractBasicController | ||
* @group AbstractBasicControllerTest | ||
*/ | ||
class AbstractBasicControllerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
//Add Setup for static properties here | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
//Add Tear Down for static properties here | ||
} | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* @covers ::configureRequest | ||
* @covers ::getAuthHeaderValue | ||
*/ | ||
public function testConfigureRequest() | ||
{ | ||
$Auth = new BasicController(); | ||
$Request = new JSON(); | ||
$this->assertEquals($Auth,$Auth->configureRequest($Request)); | ||
$headers = $Request->getHeaders(); | ||
$this->assertEquals("Basic ",$headers['Authorization']); | ||
$Auth->setCredentials(array( | ||
'username' => 'foo', | ||
'password' => 'bar' | ||
)); | ||
$this->assertEquals($Auth,$Auth->configureRequest($Request)); | ||
$headers = $Request->getHeaders(); | ||
$this->assertEquals('Basic '.base64_encode("foo:bar"),$headers['Authorization']); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace MRussell\REST\Tests\Stubs\Auth; | ||
|
||
|
||
use MRussell\REST\Auth\Abstracts\AbstractBasicController; | ||
|
||
class BasicController extends AbstractBasicController | ||
{ | ||
|
||
} |