Skip to content

Commit

Permalink
Merge pull request #45 from luchianenco/master
Browse files Browse the repository at this point in the history
Bitbucket Client added
  • Loading branch information
weaverryan authored Jan 8, 2017
2 parents 42dade4 + c0d585f commit cf9da92
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 23 deletions.
39 changes: 29 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ via Composer:

<a name="client-downloader-table"></a>

| OAuth2 Provider | Install |
| -------------------------------------------------------------- | -------------------------------------------- |
| [Facebook](https://github.com/thephpleague/oauth2-facebook) | composer require league/oauth2-facebook |
| [GitHub](https://github.com/thephpleague/oauth2-github) | composer require league/oauth2-github |
| [GitLab](https://github.com/omines/oauth2-gitlab) | composer require omines/oauth2-gitlab |
| [LinkedIn](https://github.com/thephpleague/oauth2-linkedin) | composer require league/oauth2-linkedin |
| [Google](https://github.com/thephpleague/oauth2-google) | composer require league/oauth2-google |
| [Eve Online](https://github.com/evelabs/oauth2-eveonline) | composer require evelabs/oauth2-eveonline |
| [Instagram](https://github.com/thephpleague/oauth2-instagram) | composer require league/oauth2-instagram |
| generic | configure any unsupported provider |
| OAuth2 Provider | Install |
| -------------------------------------------------------------- | ------------------------------------------------- |
| [Facebook](https://github.com/thephpleague/oauth2-facebook) | composer require league/oauth2-facebook |
| [GitHub](https://github.com/thephpleague/oauth2-github) | composer require league/oauth2-github |
| [GitLab](https://github.com/omines/oauth2-gitlab) | composer require omines/oauth2-gitlab |
| [LinkedIn](https://github.com/thephpleague/oauth2-linkedin) | composer require league/oauth2-linkedin |
| [Google](https://github.com/thephpleague/oauth2-google) | composer require league/oauth2-google |
| [Eve Online](https://github.com/evelabs/oauth2-eveonline) | composer require evelabs/oauth2-eveonline |
| [Instagram](https://github.com/thephpleague/oauth2-instagram) | composer require league/oauth2-instagram |
| [Bitbucket](https://github.com/stevenmaguire/oauth2-bitbucket) | composer require stevenmaguire/oauth2-bitbucket |
| generic | configure any unsupported provider |

<span name="end-client-downloader-table"></span>

Expand Down Expand Up @@ -427,6 +428,24 @@ knpu_oauth2_client:
# whether to check OAuth2 "state": defaults to true
# use_state: true
# will create service: "knpu.oauth2.client.bitbucket"
# an instance of: KnpU\OAuth2ClientBundle\Client\Provider\BitbucketClient
# composer require stevenmaguire/oauth2-bitbucket
bitbucket:
# must be "bitbucket" - it activates that type!
type: bitbucket
# add and configure client_id and client_secret in parameters.yml
client_id: %bitbucket_client_id%
client_secret: %bitbucket_client_secret%
# a route name you'll create
redirect_route: connect_bitbucket_check
redirect_params: {}
# whether to check OAuth2 "state": defaults to true
# use_state: true
```

## Configuring a Generic Provider
Expand Down
35 changes: 35 additions & 0 deletions src/Client/Provider/BitbucketClient.php
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();
}
}
2 changes: 2 additions & 0 deletions src/DependencyInjection/KnpUOAuth2ClientExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace KnpU\OAuth2ClientBundle\DependencyInjection;

use KnpU\OAuth2ClientBundle\DependencyInjection\Providers\BitbucketProviderConfigurator;
use KnpU\OAuth2ClientBundle\DependencyInjection\Providers\EveOnlineProviderConfigurator;
use KnpU\OAuth2ClientBundle\DependencyInjection\Providers\FacebookProviderConfigurator;
use KnpU\OAuth2ClientBundle\DependencyInjection\Providers\GenericProviderConfigurator;
Expand Down Expand Up @@ -46,6 +47,7 @@ class KnpUOAuth2ClientExtension extends Extension
'google' => GoogleProviderConfigurator::class,
'eve_online' => EveOnlineProviderConfigurator::class,
'instagram' => InstagramProviderConfigurator::class,
'bitbucket' => BitbucketProviderConfigurator::class,
'generic' => GenericProviderConfigurator::class,
];

Expand Down
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';
}
}
58 changes: 45 additions & 13 deletions tests/Client/OAuth2ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
7 changes: 7 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ public function testServicesAreUsable()
->getClient('my_facebook');
$this->assertSame($client, $client2);
$this->assertTrue($container->has('oauth2.registry'));

try {
$container->get('knpu.oauth2.registry')->getClient('');
$this->assertEquals(false, true);
} catch (\InvalidArgumentException $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e);
}
}
}
37 changes: 37 additions & 0 deletions tests/Security/Authenticator/SocialAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

class SocialAuthenticatorTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -43,6 +44,19 @@ public function testFetchAccessTokenThrowsAuthenticationException()

$authenticator->doFetchAccessToken($client->reveal());
}

public function testCheckCredentials()
{
$authenticator = new StubSocialAuthenticator();
$user = new SomeUser();
$this->assertEquals(true, $authenticator->checkCredentials('', $user));
}

public function testSupportsRememberMe()
{
$authenticator = new StubSocialAuthenticator();
$this->assertEquals(true, $authenticator->supportsRememberMe());
}
}

class StubSocialAuthenticator extends SocialAuthenticator
Expand All @@ -68,3 +82,26 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
{
}
}

class SomeUser implements UserInterface
{
public function getRoles()
{
}

public function getPassword()
{
}

public function getSalt()
{
}

public function getUsername()
{
}

public function eraseCredentials()
{
}
}
23 changes: 23 additions & 0 deletions tests/Security/Exception/FinishRegistrationExceptionTest.php
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 tests/Security/Exception/NoAuthCodeAuthenticationExceptionTest.php
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());
}
}
40 changes: 40 additions & 0 deletions tests/Security/Helper/FinishRegistrationBehaviorTest.php
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;
}
}
Loading

0 comments on commit cf9da92

Please sign in to comment.