百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Y

yii2-eauth

> 编程语言
开源

EAuth 扩展允许通过 OpenID、OAuth 1.0 和 OAuth 2.0 提供程序对用户进行身份验证。

191 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

EAuth 扩展允许通过 OpenID、OAuth 1.0 和 OAuth 2.0 提供程序对用户进行身份验证。

Yii2 EAuth extension

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.

  • Demo
  • Demo project
  • Installation
  • Version for yii 1.1

Why own extension and not a third-party service?

The implementation of the authorization on your own server has several advantages:

  • Full control over the process: What will be written in the authorization window, what data we get, etc.
  • Ability to change the appearance of the widget.
  • When logging in via OAuth, it is possible to invoke methods on the API.
  • Fewer dependencies on third-party services - more reliable application.

The extension allows you to:

  • Ignore the nuances of authorization through the different types of services and use the class based adapters for each service.
  • Get a unique user ID that can be used to register the user in your application.
  • Extend the standard authorization classes to obtain additional data about the user.
  • Work with the API of social networks by extending the authorization classes.
  • Set up a list of supported services, customize the appearance of the widget, use the popup window without closing your application.

Extension includes:

  • The component that contains utility functions.
  • A widget that displays a list of services in the form of icons and allowing authorization in the popup window.
  • Base classes to create your own services.
  • Ready to authenticate via Google, Twitter, Facebook and other providers.

Included services:

  • OpenID:
    • Yahoo
    • Steam
  • OAuth1:
    • Twitter
    • LinkedIn
  • OAuth2:
    • Google
    • Facebook
    • Live
    • GitHub
    • LinkedIn
    • Instagram
    • Yandex (ru)
    • VKontake (ru)
    • Mail.ru (ru)
    • Odnoklassniki (ru)

Resources

  • Yii EAuth
  • Demo
  • Demo project
  • Yii Framework
  • OpenID
  • OAuth
  • OAuth 2.0
  • LightOpenID
  • PHPoAuthLib

Requirements

  • Yii 2.0 or above
  • curl php extension
  • LightOpenId
  • PHPoAuthLib

Installation

This library can be found on Packagist. The recommended way to install this is through composer.

Edit your composer.json and add:

json
{
		"require": {
				"nodge/yii2-eauth": "~2.0"
		}
}

And install dependencies:

bash
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install

Usage

Demo project

The source code of the demo is available here.

Basic setup

Configuration

Add the following in your config:

…

User model

You need to modify your User model to login with EAuth services. Example from demo project:

…

Then you can access to EAuth attributes through:

php
getUser()->getIdentity();
	if (isset($identity->profile)) {
		VarDumper::dump($identity->profile, 10, true);
	}

Controller

Attach OpenID Controller behavior to disable CSRF validation for OpenID callbacks. Or you can disable CSRF validation by yourself.

php
 [
						// required to disable csrf validation on OpenID requests
						'class' => \nodge\eauth\openid\ControllerBehavior::className(),
						'only' => ['login'],
					],
				];
			}
...

Add the following to your Login action:

…

View

php
...
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']); ?>
...

Extending

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.

Working with OAuth API

You can extend base classes with necessary methods and then write something like this:

php
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:

php
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:

php
 [
		'eauth' => [
			'class' => 'nodge\eauth\EAuth',
			'tokenStorage' => [
				'class' => '@app\eauth\DatabaseTokenStorage',
			],
		],
		...
	],
...

Translation

To use translations, add the following in your config:

php
 [
		'i18n' => [
			'translations' => [
				'eauth' => [
					'class' => 'yii\i18n\PhpMessageSource',
					'basePath' => '@eauth/messages',
				],
			],
		],
		...
	],
...

Available translations can be found in @eauth/src/messages.

License

The extension was released under the New BSD License, so you'll find the latest version on GitHub.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言