Contributte Oauth2-client
# Setup
Install package
composer require contributte/oauth2-client
# Supported flows
Take a look at integration for usage
- Implemented package league/oauth2-google (opens new window)
- Credentials source (opens new window)
- Flow registration
google:
clientId: '...'
clientSecret: '...'
options:
# optionally additional options passed to GoogleProvider
extensions:
google: Contributte\OAuth2Client\DI\GoogleAuthExtension
2
3
4
5
6
7
8
- Implemented package league/oauth2-facebook (opens new window)
- Credentials source (opens new window)
- Flow registration
facebook:
clientId: '...'
clientSecret: '...'
graphApiVersion: 'v14.0'
options:
# optionally additional options passed to FacebookProvider
extensions:
facebook: Contributte\OAuth2Client\DI\FacebookAuthExtension
2
3
4
5
6
7
8
9
# Gitlab
- Implemented package omines/oauth2-gitlab (opens new window)
- Credentials source (opens new window)
- Flow registration
gitlab:
clientId: '...'
clientSecret: '...'
domain: 'https://gitlab.com'
options:
# optionally additional options passed to GitlabProvider
extensions:
facebook: Contributte\OAuth2Client\DI\GitlabAuthExtension
2
3
4
5
6
7
8
9
# 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 (opens new window)
# Integration
This example uses Google as provider with integration through league/oauth2-google (opens new window)
# Install package
composer require league/oauth2-google
Get your oauth2 credentials (clientId
and clientSecret
) from Google website (opens new window)
# Register flow
google:
clientId: '...'
clientSecret: '...'
options:
# optionally additional options passed to GoogleProvider
extensions:
google: Contributte\OAuth2Client\DI\GoogleAuthExtension
2
3
4
5
6
7
8
# A) Create custom control
Create custom 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(string $authorizationUrl): void
{
$this->presenter->redirectUrl(
$this->flow->getAuthorizationUrl($authorizationUrl)
);
}
public function authorize(array $parameters = null): void
{
try {
$parameters = $parameters ?? $this->getPresenter()->getHttpRequest()->getQuery();
$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())
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Add control to sign presenter
use Nette\Application\UI\Presenter;
use Contributte\OAuth2Client\Flow\Google\GoogleAuthCodeFlow;
class SignPresenter extends Presenter
{
/** @inject */
public GoogleAuthCodeFlow $googleAuthCodeFlow;
public function actionGoogleAuthenticate(): void
{
$this['googleButton']->authenticate($this->presenter->link('//:Sign:googleAuthorize'));
}
public function actionGoogleAuthorize(): void
{
$this['googleButton']->authorize();
}
protected function createComponentGoogleButton(): GoogleButton
{
return new GoogleButton($this->googleAuthCodeFlow);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Create link to authentication action
<a href="{plink :Front:Sign:googleAuthenticate}">Sign in with Google</a>
# B) Use GenericAuthControl
Add GenericAuthControl
control to sign presenter
use Nette\Application\UI\Presenter;
use Contributte\OAuth2Client\Flow\Google\GoogleAuthCodeFlow;
use League\OAuth2\Client\Provider\GoogleUser;
use League\OAuth2\Client\Token\AccessToken;
class SignPresenter extends Presenter
{
public function actionGoogleAuthenticate(): void
{
$this['googleButton']->authenticate();
}
public function actionGoogleAuthorize(): void
{
$this['googleButton']->authorize();
}
protected function createComponentGoogleButton(): GoogleButton
{
$authControl = new GenericAuthControl(
$this->googleAuthFlow,
$this->presenter->link('//:Sign:googleAuthorize')
);
$authControl->setTemplate(__DIR__ . "/googleAuthLatte.latte");
$authControl->onAuthenticate[] = function(AccessToken $accessToken, GoogleUser $user) {
// TODO - try sign in user with it's email ($owner->getEmail())
}
$authControl->onFail[] = function() {
// TODO - Identity provider failure, cannot get information about user
}
return $authControl;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Create custom template for authentication control.
<a href="{link authenticate!}">Sign in with Google</a>
Use control in presenter template.
{control googleButton}
Or create link to authentication action in presenter template
<a href="{plink :Front:Sign:googleAuthenticate}">Sign in with Google</a>
That's all!