Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
155 lines (115 sloc) 3.76 KB

Contributte OAuth2Client

Setup

Install package

composer require contributte/oauth2-client

Supported flows

Take a look at integration for usage

Google

  • Implemented package league/oauth2-google
  • Credentials source
  • Flow registration
    services:
    - Contributte\OAuth2Client\Flow\Google\GoogleProvider([
        clientId: 
        clientSecret:
      ])
    - Contributte\OAuth2Client\Flow\Google\GoogleAuthCodeFlow

Facebook

  • Implemented package league/oauth2-facebook
  • Credentials source
  • Flow registration
    services:
    - Contributte\OAuth2Client\Flow\Facebook\FacebookProvider([
        clientId: 
        clientSecret:
        graphApiVersion: v3.2
      ])
    - Contributte\OAuth2Client\Flow\Facebook\Facebook\AuthCodeFlow

Others

You could implement other providers which support auth code authentication by extending Contributte\OAuth2Client\Flow\AuthCodeFlow. Other authentication methods are currently not supported (PR is welcome).

List of all providers is here

Integration

This example uses Google as provider with integration through league/oauth2-google

Install package

composer require league/oauth2-google

Get your oauth2 credentials (clientId and clientSecret) from Google website

Register flow

services:
  - Contributte\OAuth2Client\Flow\Google\GoogleProvider([
      clientId: 
      clientSecret:
    ])
  - Contributte\OAuth2Client\Flow\Google\GoogleAuthCodeFlow

Create a control which can handle authentication and authorization

use Contributte\OAuth2Client\Flow\Google\GoogleAuthCodeFlow;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Provider\GoogleUser;
use Nette\Application\UI\Control;

class GoogleButton extends Control
{

	/** @var GoogleAuthCodeFlow */
	private $flow;

	public function __construct(GoogleAuthCodeFlow $flow)
	{
		parent::__construct();
		$this->flow = $flow;
	}

	public function authenticate(): void
	{
		$this->flow->getProvider()->setRedirectUri(
			$this->presenter->link('//:Sign:googleAuthorize')
		);
		$this->presenter->redirectUrl($this->flow->getAuthorizationUrl());
	}

	public function authorize(array $parameters): void
	{
		// Setup propel redirect URL
		$this->flow->getProvider()->setRedirectUri(
			$this->presenter->link('//:Sign:googleAuthorize')
		);

		try {
			$accessToken = $this->flow->getAccessToken($parameters);
		} catch (IdentityProviderException $e) {
			// TODO - Identity provider failure, cannot get information about user
		}

		/** @var GoogleUser $owner */
		$owner = $this->flow->getProvider()->getResourceOwner($accessToken);

		// TODO - try sign in user with it's email ($owner->getEmail())
	}

}

Add control to sign presenter

use Nette\Application\UI\Presenter;

class SignPresenter extends Presenter
{
	
	public function actionGoogleAuthenticate(): void
	{
		$this['googleButton']->authenticate();
	}
	
	public function actionGoogleAuthorize(): void
	{
		$this['googleButton']->authorize($this->getHttpRequest()->getQuery());
	}
	
	protected function createComponentGoogleButton(): GoogleButton
	{
		// TODO - create and return GoogleButton control
	}
	
}

Create link to authentication action

<a href="{plink :Front:Sign:googleAuthenticate}">Sign in with Google</a>

That's all!

You can’t perform that action at this time.