-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from ADmad/identity-override
Prevent logged in user override.
- Loading branch information
Showing
5 changed files
with
178 additions
and
36 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
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 | ||
declare(strict_types=1); | ||
|
||
namespace ADmad\SocialAuth\Test\Fixture; | ||
|
||
use Cake\TestSuite\Fixture\TestFixture; | ||
|
||
/** | ||
* UsersFixture | ||
*/ | ||
class UsersFixture extends TestFixture | ||
{ | ||
/** | ||
* Fields | ||
* | ||
* @var array | ||
*/ | ||
// phpcs:disable | ||
public $fields = [ | ||
'id' => ['type' => 'integer', 'length' => null, 'unsigned' => false, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null], | ||
'email' => ['type' => 'string', 'length' => 255, 'null' => true, 'default' => null, 'collate' => 'utf8mb4_unicode_ci', 'comment' => '', 'precision' => null], | ||
'_constraints' => [ | ||
'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], | ||
], | ||
'_options' => [ | ||
'engine' => 'InnoDB', | ||
'collation' => 'utf8mb4_unicode_ci' | ||
], | ||
]; | ||
// phpcs:enable | ||
|
||
/** | ||
* Init method | ||
* | ||
* @return void | ||
*/ | ||
public function init(): void | ||
{ | ||
$this->records = []; | ||
|
||
parent::init(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,10 +4,16 @@ | |
namespace ADmad\SocialAuth\Test\TestCase\Middleware; | ||
|
||
use ADmad\SocialAuth\Middleware\SocialAuthMiddleware; | ||
use ADmad\SocialAuth\Model\Entity\SocialProfile; | ||
use ADmad\SocialAuth\Model\Table\SocialProfilesTable; | ||
use Cake\Event\EventInterface; | ||
use Cake\Event\EventManager; | ||
use Cake\Http\Exception\MethodNotAllowedException; | ||
use Cake\Http\Response; | ||
use Cake\Http\ServerRequestFactory; | ||
use Cake\TestSuite\TestCase; | ||
use SocialConnect\Common\Entity\User; | ||
use SocialConnect\Provider\AccessTokenInterface; | ||
use SocialConnect\Provider\Session\Dummy; | ||
use TestApp\Http\TestRequestHandler; | ||
|
||
|
@@ -17,6 +23,11 @@ | |
*/ | ||
class SocialAuthMiddlewareTest extends TestCase | ||
{ | ||
protected $fixtures = [ | ||
'plugin.ADmad/SocialAuth.Users', | ||
'plugin.ADmad/SocialAuth.SocialProfiles', | ||
]; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
@@ -97,4 +108,59 @@ public function testLoginUrlException() | |
$middleware = new SocialAuthMiddleware(); | ||
$middleware->process($request, $this->handler); | ||
} | ||
|
||
/** | ||
* Test that IDENTITY_MISMATCH_ERROR occurs when a social profile is already | ||
* associated with a user and that user does not match the user record set | ||
* in the session. | ||
* | ||
* @return void | ||
* @see https://github.com/ADmad/cakephp-social-auth/pull/108 | ||
*/ | ||
public function testIdentityMismatchError() | ||
{ | ||
$request = ServerRequestFactory::fromGlobals([ | ||
'REQUEST_URI' => '/social-auth/callback/facebook', | ||
]); | ||
$request = $request->withAttribute('params', [ | ||
'plugin' => 'ADmad/SocialAuth', | ||
'controller' => 'Auth', | ||
'action' => 'callback', | ||
'provider' => 'facebook', | ||
]); | ||
$request->getSession()->write('Auth', ['id' => 1]); | ||
|
||
$this->getTableLocator()->get(SocialProfilesTable::class) | ||
->save(new SocialProfile([ | ||
'user_id' => 2, | ||
'provider' => 'facebook', | ||
'identifier' => 'fbid', | ||
'email' => '[email protected]', | ||
'access_token' => '', | ||
])); | ||
|
||
$middleware = $this->getMockBuilder(SocialAuthMiddleware::class) | ||
->onlyMethods(['_getSocialIdentity']) | ||
->getMock(); | ||
|
||
$accessToken = $this->getMockBuilder(AccessTokenInterface::class) | ||
->getMock(); | ||
|
||
$identity = new User(); | ||
$identity->id = 'fbid'; | ||
$identity->email = '[email protected]'; | ||
|
||
$middleware->expects($this->once()) | ||
->method('_getSocialIdentity') | ||
->willReturn(['identity' => $identity, 'access_token' => $accessToken]); | ||
|
||
EventManager::instance()->on( | ||
SocialAuthMiddleware::EVENT_BEFORE_REDIRECT, | ||
function (EventInterface $event, $url, string $status) { | ||
$this->assertSame(SocialAuthMiddleware::AUTH_STATUS_IDENTITY_MISMATCH, $status); | ||
} | ||
); | ||
|
||
$middleware->process($request, $this->handler); | ||
} | ||
} |