-
Notifications
You must be signed in to change notification settings - Fork 146
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 #45 from luchianenco/master
Bitbucket Client added
- Loading branch information
Showing
11 changed files
with
327 additions
and
23 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,35 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Client\Provider; | ||
|
||
use KnpU\OAuth2ClientBundle\Client\OAuth2Client; | ||
use League\OAuth2\Client\Token\AccessToken; | ||
use Stevenmaguire\OAuth2\Client\Provider\BitbucketResourceOwner; | ||
|
||
class BitbucketClient extends OAuth2Client | ||
{ | ||
/** | ||
* @param AccessToken $accessToken | ||
* @return BitbucketResourceOwner | ||
*/ | ||
public function fetchUserFromToken(AccessToken $accessToken) | ||
{ | ||
return parent::fetchUserFromToken($accessToken); | ||
} | ||
|
||
/** | ||
* @return BitbucketResourceOwner | ||
*/ | ||
public function fetchUser() | ||
{ | ||
return parent::fetchUser(); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
src/DependencyInjection/Providers/BitbucketProviderConfigurator.php
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,54 @@ | ||
<?php | ||
|
||
/* | ||
* OAuth2 Client Bundle | ||
* Copyright (c) KnpUniversity <http://knpuniversity.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\DependencyInjection\Providers; | ||
|
||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
|
||
class BitbucketProviderConfigurator implements ProviderConfiguratorInterface | ||
{ | ||
public function buildConfiguration(NodeBuilder $node) | ||
{ | ||
// no custom options | ||
} | ||
|
||
public function getProviderClass(array $config) | ||
{ | ||
return 'Stevenmaguire\OAuth2\Client\Provider\Bitbucket'; | ||
} | ||
|
||
public function getProviderOptions(array $config) | ||
{ | ||
return [ | ||
'clientId' => $config['client_id'], | ||
'clientSecret' => $config['client_secret'], | ||
]; | ||
} | ||
|
||
public function getPackagistName() | ||
{ | ||
return 'stevenmaguire/oauth2-bitbucket'; | ||
} | ||
|
||
public function getLibraryHomepage() | ||
{ | ||
return 'https://github.com/stevenmaguire/oauth2-bitbucket'; | ||
} | ||
|
||
public function getProviderDisplayName() | ||
{ | ||
return 'Bitbucket'; | ||
} | ||
|
||
public function getClientClass(array $config) | ||
{ | ||
return 'KnpU\OAuth2ClientBundle\Client\Provider\BitbucketClient'; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
namespace KnpU\OAuth2ClientBundle\Tests\Client; | ||
|
||
use KnpU\OAuth2ClientBundle\Client\OAuth2Client; | ||
use League\OAuth2\Client\Provider\FacebookUser; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
|
@@ -166,20 +167,51 @@ public function testGetAccessTokenThrowsInvalidStateException() | |
$client->getAccessToken(); | ||
} | ||
|
||
/** | ||
* @expectedException \KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException | ||
/** | ||
* @expectedException \KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException | ||
*/ | ||
public function testGetAccessTokenThrowsMissingAuthCodeException() | ||
{ | ||
$this->request->query->set('state', 'ACTUAL_STATE'); | ||
$this->session->get(OAuth2Client::OAUTH2_SESSION_STATE_KEY) | ||
public function testGetAccessTokenThrowsMissingAuthCodeException() | ||
{ | ||
$this->request->query->set('state', 'ACTUAL_STATE'); | ||
$this->session->get(OAuth2Client::OAUTH2_SESSION_STATE_KEY) | ||
->willReturn('ACTUAL_STATE'); | ||
|
||
// don't set a code query parameter | ||
$client = new OAuth2Client( | ||
$this->provider->reveal(), | ||
$this->requestStack | ||
); | ||
$client->getAccessToken(); | ||
} | ||
// don't set a code query parameter | ||
$client = new OAuth2Client( | ||
$this->provider->reveal(), | ||
$this->requestStack | ||
); | ||
$client->getAccessToken(); | ||
} | ||
|
||
public function testFetchUser() | ||
{ | ||
$this->request->request->set('code', 'CODE_ABC'); | ||
|
||
$expectedToken = $this->prophesize('League\OAuth2\Client\Token\AccessToken'); | ||
$this->provider->getAccessToken('authorization_code', ['code' => 'CODE_ABC']) | ||
->willReturn($expectedToken->reveal()); | ||
|
||
$client = new OAuth2Client( | ||
$this->provider->reveal(), | ||
$this->requestStack | ||
); | ||
|
||
$client->setAsStateless(); | ||
$actualToken = $client->getAccessToken(); | ||
|
||
$resourceOwner = new FacebookUser([ | ||
'id' => '1', | ||
'name' => 'testUser', | ||
'first_name' => 'John', | ||
'last_name' => 'Doe', | ||
'email' => '[email protected]' | ||
]); | ||
|
||
$this->provider->getResourceOwner($actualToken)->willReturn($resourceOwner); | ||
$user = $client->fetchUser($actualToken); | ||
|
||
$this->assertInstanceOf('League\OAuth2\Client\Provider\FacebookUser', $user); | ||
$this->assertEquals('testUser', $user->getName()); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
tests/Security/Exception/FinishRegistrationExceptionTest.php
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,23 @@ | ||
<?php | ||
/** | ||
* @author Serghei Luchianenco ([email protected]) | ||
* Date: 07/01/2017 | ||
* Time: 23:21 | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests\Security\Exception; | ||
|
||
use KnpU\OAuth2ClientBundle\Security\Exception\FinishRegistrationException; | ||
|
||
|
||
class FinishRegistrationExceptionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testException() | ||
{ | ||
$userInfo = ['id' => '1', 'name' => 'testUser']; | ||
$e = new FinishRegistrationException($userInfo, '', 0); | ||
|
||
$this->assertEquals($e->getUserInformation(), $userInfo); | ||
$this->assertInternalType('string', $e->getMessageKey()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/Security/Exception/NoAuthCodeAuthenticationExceptionTest.php
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,23 @@ | ||
<?php | ||
/** | ||
* @author Serghei Luchianenco ([email protected]) | ||
* Date: 07/01/2017 | ||
* Time: 23:27 | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests\Security\Exception; | ||
|
||
|
||
use KnpU\OAuth2ClientBundle\Security\Exception\NoAuthCodeAuthenticationException; | ||
|
||
|
||
class NoAuthCodeAuthenticationExceptionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testException() | ||
{ | ||
$e = new NoAuthCodeAuthenticationException(); | ||
|
||
$this->assertInternalType('string', $e->getMessageKey()); | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
/** | ||
* @author Serghei Luchianenco ([email protected]) | ||
* Date: 07/01/2017 | ||
* Time: 23:29 | ||
*/ | ||
|
||
namespace KnpU\OAuth2ClientBundle\Tests\Security\Helper; | ||
|
||
|
||
use KnpU\OAuth2ClientBundle\Security\Exception\FinishRegistrationException; | ||
|
||
class FinishRegistrationBehaviorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $traitObject; | ||
|
||
public function setUp() | ||
{ | ||
$this->traitObject = $this | ||
->getMockForTrait('KnpU\OAuth2ClientBundle\Security\Helper\FinishRegistrationBehavior'); | ||
} | ||
|
||
public function testGetUserInfoFromSession() | ||
{ | ||
$request = $this->prophesize('Symfony\Component\HttpFoundation\Request'); | ||
$request->getSession()->willReturn(new StubSession()); | ||
$res = $this->traitObject->getUserInfoFromSession($request->reveal()); | ||
|
||
$this->assertEquals($res, true); | ||
} | ||
|
||
} | ||
|
||
class StubSession | ||
{ | ||
public function get() | ||
{ | ||
return true; | ||
} | ||
} |
Oops, something went wrong.