Skip to content
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

Added Oauth2ClientInterface for service decorating #137

Merged
merged 5 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,43 @@ knpu_oauth2_client:
That's it! Now you'll have a `knpu.oauth2.client.foo_bar_oauth` service
you can use.

## Extending/Decorating Client Classes

Maybe you need some extra services inside your client class? No problem! You can
decorate existing client class with your own implementation. All you need is
new class that implement OAuth2ClientInterface:

```php
namespace App\Client;

use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface;
use KnpU\OAuth2ClientBundle\Client\Provider\AzureClient;
use Symfony\Component\Cache\Adapter\AdapterInterface;

class CacheableAzureClient implements OAuth2ClientInterface
{
private $client;
private $cache;

public function __construct(AzureClient $client, AdapterInterface $cache)
{
// ...
}

// override all public functions and call the method on the internal $this->client object
// but add caching wherever you need it
}
```

and configure it:

```yml
# config/services.yaml
services:
App\Client\CacheableAzureClient:
decorates: knpu.oauth2.client.azure
```

## Contributing

Of course, open source is fueled by everyone's ability to give just a little
Expand Down
2 changes: 1 addition & 1 deletion src/Client/OAuth2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;

class OAuth2Client
class OAuth2Client implements OAuth2ClientInterface
{
const OAUTH2_SESSION_STATE_KEY = 'knpu.oauth2_client_state';

Expand Down
67 changes: 67 additions & 0 deletions src/Client/OAuth2ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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;

use League\OAuth2\Client\Token\AccessToken;

interface OAuth2ClientInterface
{
/**
* Call this to avoid using and checking "state".
*/
public function setAsStateless();

/**
* Creates a RedirectResponse that will send the user to the
* OAuth2 server (e.g. send them to Facebook).
*
* @param array $scopes The scopes you want (leave empty to use default)
* @param array $options Extra options to pass to the "Provider" class
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function redirect(array $scopes, array $options);

/**
* Call this after the user is redirected back to get the access token.
*
* @return \League\OAuth2\Client\Token\AccessToken
*
* @throws \KnpU\OAuth2ClientBundle\Exception\InvalidStateException
* @throws \KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException If token cannot be fetched
*/
public function getAccessToken();

/**
* Returns the "User" information (called a resource owner).
*
* @param AccessToken $accessToken
* @return \League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken);

/**
* Shortcut to fetch the access token and user all at once.
*
* Only use this if you don't need the access token, but only
* need the user.
*
* @return \League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser();

/**
* Returns the underlying OAuth2 provider.
*
* @return \League\OAuth2\Client\Provider\AbstractProvider
*/
public function getOAuth2Provider();
}