-
Notifications
You must be signed in to change notification settings - Fork 216
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
Release 5.1.0 #230
Release 5.1.0 #230
Changes from 6 commits
b946173
1b1e1b5
721c2c8
9a1a03c
39fcab6
96bbf14
4246215
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Auth0\SDK\API\Helpers\State; | ||
|
||
/* | ||
* This file is part of Auth0-PHP package. | ||
* | ||
* (c) Auth0 | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Dummy implementation of the StateHandler | ||
* | ||
* @author Auth0 | ||
*/ | ||
class DummyStateHandler implements StateHandler | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't be but not consistent in the library (yet) |
||
/** | ||
* Generate state value to be used for the state param value during authorization. | ||
* | ||
* @return string | ||
*/ | ||
public function issue() { | ||
return null; | ||
} | ||
|
||
/** | ||
* Store state value to be used for the state param value during authorization. | ||
* | ||
*/ | ||
public function store($state) { | ||
} | ||
|
||
/** | ||
* Perform validation of the returned state with the previously generated state. | ||
* | ||
* @param string $state | ||
* | ||
* @return bool result | ||
* @throws exception | ||
*/ | ||
public function validate($state) { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Auth0\SDK\API\Helpers\State; | ||
|
||
use Auth0\SDK\Store\SessionStore; | ||
use Auth0\SDK\Exception\CoreException; | ||
|
||
/* | ||
* This file is part of Auth0-PHP package. | ||
* | ||
* (c) Auth0 | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Session based implementation of StateHandler. | ||
* | ||
* @author Auth0 | ||
*/ | ||
class SessionStateHandler implements StateHandler | ||
{ | ||
const STATE_NAME = 'webauth_state'; | ||
|
||
private $store; | ||
|
||
/** | ||
* @param SessionStore $store | ||
*/ | ||
public function __construct(SessionStore $store) { | ||
$this->store = $store; | ||
} | ||
|
||
/** | ||
* Generate state value to be used for the state param value during authorization. | ||
* | ||
* @return string | ||
*/ | ||
public function issue() { | ||
$state = uniqid('', true); | ||
$this->store($state); | ||
return $state; | ||
} | ||
|
||
/** | ||
* Store a given state value to be used for the state param value during authorization. | ||
* | ||
* @return string | ||
*/ | ||
public function store($state) { | ||
$this->store->set(self::STATE_NAME, $state); | ||
} | ||
|
||
/** | ||
* Perform validation of the returned state with the previously generated state. | ||
* | ||
* @param string $state | ||
* | ||
* @throws exception | ||
*/ | ||
public function validate($state) { | ||
$valid = $this->store->get(self::STATE_NAME) == $state; | ||
$this->store->delete(self::STATE_NAME); | ||
return $valid; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Auth0\SDK\API\Helpers\State; | ||
|
||
use Auth0\SDK\Store\StoreInterface; | ||
|
||
/* | ||
* This file is part of Auth0-PHP package. | ||
* | ||
* (c) Auth0 | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* This interface must be implemented by state handlers. | ||
* | ||
* @author Auth0 | ||
*/ | ||
interface StateHandler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inconsistent brace style 😛 |
||
|
||
/** | ||
* Generate state value to be used for the state param value during authorization. | ||
* | ||
* @return string || null | ||
*/ | ||
public function issue(); | ||
|
||
/** | ||
* Store a given state value to be used for the state param value during authorization. | ||
* | ||
* @return string | ||
*/ | ||
public function store($state); | ||
|
||
/** | ||
* Perform validation of the returned state with the previously generated state. | ||
* | ||
* @param string $state | ||
* | ||
* @return bool result | ||
* @throws exception | ||
*/ | ||
public function validate($state); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
use Auth0\SDK\Store\SessionStore; | ||
use Auth0\SDK\Store\StoreInterface; | ||
use Auth0\SDK\API\Authentication; | ||
use Auth0\SDK\API\Helpers\State\StateHandler; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's mentioned in a docblock, which is flagged in my IDE |
||
use Auth0\SDK\API\Helpers\State\SessionStateHandler; | ||
use Auth0\SDK\API\Helpers\State\DummyStateHandler; | ||
|
||
/** | ||
* This class provides access to Auth0 Platform. | ||
|
@@ -132,6 +135,12 @@ class Auth0 { | |
|
||
protected $guzzleOptions; | ||
|
||
/** | ||
* State Handler | ||
* @var StateHandler | ||
*/ | ||
protected $stateHandler; | ||
|
||
/** | ||
* BaseAuth0 Constructor. | ||
* | ||
|
@@ -229,6 +238,15 @@ public function __construct(array $config) { | |
} else { | ||
$this->setStore(new SessionStore()); | ||
} | ||
if (isset($config['state_handler'])) { | ||
if ($config['state_handler'] === false) { | ||
$this->stateHandler = new DummyStateHandler(); | ||
} else { | ||
$this->stateHandler = $config['state_handler']; | ||
} | ||
} else { | ||
$this->stateHandler = new SessionStateHandler(new SessionStore()); | ||
} | ||
|
||
$this->authentication = new Authentication ($this->domain, $this->client_id, $this->client_secret, $this->audience, $this->scope, $this->guzzleOptions); | ||
|
||
|
@@ -238,7 +256,8 @@ public function __construct(array $config) { | |
$this->refresh_token = $this->store->get("refresh_token"); | ||
} | ||
|
||
public function login($state = null, $connection = null) { | ||
public function login($state = null, $connection = null, $additional_params = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is NOT a breaking change because you're passing |
||
|
||
$params = []; | ||
if ($this->audience) { | ||
$params['audience'] = $this->audience; | ||
|
@@ -247,8 +266,18 @@ public function login($state = null, $connection = null) { | |
$params['scope'] = $this->scope; | ||
} | ||
|
||
if($state === null) { | ||
$state = $this->stateHandler->issue(); | ||
} else { | ||
$this->stateHandler->store($state); | ||
} | ||
|
||
$params['response_mode'] = $this->response_mode; | ||
|
||
if($additional_params) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if my statement is correct, then this will always be either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That if(!empty($additional_params) && is_array($additional_params)) {
$params = array_replace($params, $additional_params);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will accept the change, however as long as the parameter type is documented somewhere, users shouldn't be passing invalid (non-array) values. So next time: doc + |
||
$params = array_replace($params, $additional_params); | ||
} | ||
|
||
$url = $this->authentication->get_authorize_link($this->response_type, $this->redirect_uri, $connection, $state, $params); | ||
|
||
header("Location: $url"); | ||
|
@@ -298,6 +327,12 @@ public function exchange() { | |
return false; | ||
} | ||
|
||
$state = $this->getState(); | ||
|
||
if (!$this->stateHandler->validate($state)) { | ||
throw new CoreException('Invalid state'); | ||
} | ||
|
||
if ($this->user) { | ||
throw new CoreException('Can\'t initialize a new session while there is one active session already'); | ||
} | ||
|
@@ -387,6 +422,16 @@ protected function getAuthorizationCode() { | |
return null; | ||
} | ||
|
||
protected function getState() { | ||
if ($this->response_mode === 'query') { | ||
return (isset($_GET['state']) ? $_GET['state'] : null); | ||
} elseif ($this->response_mode === 'form_post') { | ||
return (isset($_POST['state']) ? $_POST['state'] : null); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function logout() { | ||
$this->deleteAllPersistentData(); | ||
$this->access_token = NULL; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
namespace Auth0\Tests\Api\Helpers\State; | ||
|
||
use Auth0\SDK\API\Helpers\State\SessionStateHandler; | ||
use Auth0\SDK\Store\SessionStore; | ||
|
||
class StateHandlerTest extends \PHPUnit_Framework_TestCase { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHP namespace thing, means 'use the global namespace' |
||
|
||
public function testStateStoredCorrectly() { | ||
// Suppress header sent error | ||
@$test_store = new SessionStore(); | ||
$test_state = new SessionStateHandler( $test_store ); | ||
$uniqid = uniqid(); | ||
$test_state->store( $uniqid ); | ||
|
||
$this->assertEquals( $uniqid, $test_store->get( SessionStateHandler::STATE_NAME ) ); | ||
} | ||
|
||
public function testStateIssuedCorrectly() { | ||
// Suppress header sent error | ||
@$test_store = new SessionStore(); | ||
$test_state = new SessionStateHandler( $test_store ); | ||
$uniqid_returned = $test_state->issue(); | ||
|
||
$this->assertEquals( $uniqid_returned, $test_store->get( SessionStateHandler::STATE_NAME ) ); | ||
} | ||
|
||
public function testStateValidatesCorrectly() { | ||
// Suppress header sent error | ||
@$test_store = new SessionStore(); | ||
$test_state = new SessionStateHandler( $test_store ); | ||
$uniqid_returned_1 = $test_state->issue(); | ||
|
||
$this->assertTrue( $test_state->validate( $uniqid_returned_1 ) ); | ||
$this->assertNull( $test_store->get( SessionStateHandler::STATE_NAME ) ); | ||
|
||
$uniqid_returned_2 = $test_state->issue(); | ||
$this->assertFalse( $test_state->validate( $uniqid_returned_2 . 'false' ) ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably not intended carriage return