EAuth extension allows to authenticate users by the OpenID, OAuth 1.0 and OAuth 2.0 providers.
EAuth extension allows to authenticate users by the OpenID, OAuth 1.0 and OAuth 2.0 providers.
EAuth extension allows to authenticate users with accounts on other websites. Supported protocols: OpenID, OAuth 1.0 and OAuth 2.0.
EAuth is an extension to provide a unified (does not depend on the selected service) method to authenticate the user. The extension itself does not perform login, does not register the user and does not bind the user accounts from different providers.
The implementation of the authorization on your own server has several advantages:
This library can be found on Packagist. The recommended way to install this is through composer.
Edit your composer.json and add:
{
"require": {
"nodge/yii2-eauth": "~2.0"
}
}And install dependencies:
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar installThe source code of the demo is available here.
Add the following in your config:
…You need to modify your User model to login with EAuth services. Example from demo project:
…Then you can access to EAuth attributes through:
getUser()->getIdentity();
if (isset($identity->profile)) {
VarDumper::dump($identity->profile, 10, true);
}Attach OpenID Controller behavior to disable CSRF validation for OpenID callbacks. Or you can disable CSRF validation by yourself.
[
// required to disable csrf validation on OpenID requests
'class' => \nodge\eauth\openid\ControllerBehavior::className(),
'only' => ['login'],
],
];
}
...Add the following to your Login action:
…...
getSession()->hasFlash('error')) {
echo '
';
}
?>
...
Do you already have an account on one of these sites? Click the logo to log in with it here:
'site/login']); ?>
...To receive all the necessary data to your application, you can override the base class of any provider.
Base classes are stored in @eauth/src/services.
Examples of extended classes can be found in @eauth/src/services/extended/.
After overriding the base class, you need to update your configuration file with a new class name.
You can extend base classes with necessary methods and then write something like this:
eauth->getIdentity('facebook');
// to get protected resources user should be authenticated:
if ($eauth->getIsAuthenticated()) {
$eauth->callProtectedApiMethod();
$eauth->callAnotherProtectedApiMethod();
}
// or you can get public resources at any time:
$eauth->callPublicApiMethod();
$eauth->callAnotherPublicApiMethod();Example of an API call method:
makeSignedRequest($api_method, [
'query' => [ 'foo' => 'bar' ], // GET arguments
'data' => [ 'foo' => 'bar' ], // POST arguments
'headers' => [ 'X-Foo' => 'bar' ], // Extra HTTP headers
]);
// you can get public resources with the same API:
//$response = $this->makeRequest($api_method, $options);
// process $response
$data = process($response);
// return results
return $data;
}
}API calls are performed if the current user has a valid access token (saved during the authentication). You can save access_token to your database by using custom token storage in your config:
[
'eauth' => [
'class' => 'nodge\eauth\EAuth',
'tokenStorage' => [
'class' => '@app\eauth\DatabaseTokenStorage',
],
],
...
],
...To use translations, add the following in your config:
[
'i18n' => [
'translations' => [
'eauth' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@eauth/messages',
],
],
],
...
],
...Available translations can be found in @eauth/src/messages.
The extension was released under the New BSD License, so you'll find the latest version on GitHub.
No open issues yet, or sync has not completed.