itmages/yii-eauth

此包已被弃用,不再维护。未建议替代包。

EAuth 扩展允许通过 OpenID、OAuth 1.0 和 OAuth 2.0 提供商进行用户认证。

v1.0.1 2013-08-20 17:25 UTC

This package is not auto-updated.

Last update: 2018-04-29 12:19:42 UTC


README

EAuth 扩展允许用户使用其他网站的账户进行认证。支持的协议:OpenID、OAuth 1.0 和 OAuth 2.0。

EAuth 是一个扩展,提供了一种统一的(不依赖于所选服务)用户认证方法。因此,扩展本身不执行登录、不注册用户,也不绑定来自不同提供者的用户账户。

为什么使用自己的扩展而不是第三方服务?

在您自己的服务器上实现授权具有几个优点:

  • 完全控制过程:授权窗口中将写入什么,我们将获得什么数据等。
  • 可以更改小部件的外观。
  • 通过 OAuth 登录时可以调用 API 上的方法。
  • 对第三方服务的依赖性较少 - 应用程序更可靠。

此扩展允许您:

  • 忽略不同类型服务的授权细节,使用基于类的适配器为每个服务。
  • 获取一个唯一的用户 ID,可用于在您的应用程序中注册用户。
  • 通过扩展标准授权类来获取有关用户的其他数据。
  • 通过扩展授权类与社交网络的 API 一起工作。
  • 设置支持的服务列表,自定义小部件的外观,使用弹出窗口而不关闭您的应用程序。

扩展包含:

  • 包含实用函数的组件。
  • 一个小部件,以图标的形式显示服务列表,并允许在弹出窗口中进行授权。
  • 创建您自己的服务的基类。
  • 已准备好通过 Google、Twitter、Facebook 和其他提供者进行认证。

默认支持以下提供者:

  • OpenID:Google、Yandex(ru)
  • OAuth:Twitter、LinkedIn
  • OAuth 2.0:Google、Facebook、Live、GitHub、VKontake(ru)、Mail.ru(ru)、Moi Krug(ru)、Odnoklassniki(ru)

资源

要求

安装

  • 安装 loid 和 EOAuth 扩展
  • protected/extensions 下解压发布文件
  • 在您的 protected/config/main.php 中添加以下内容:
<?php
...
	'import'=>array(
		'ext.eoauth.*',
		'ext.eoauth.lib.*',
		'ext.lightopenid.*',
		'ext.eauth.*',
		'ext.eauth.services.*',
	),
...
	'components'=>array(
		'loid' => array(
			'class' => 'ext.lightopenid.loid',
		),
		'eauth' => array(
			'class' => 'ext.eauth.EAuth',
			'popup' => true, // Use the popup window instead of redirecting.
			'cache' => false, // Cache component name or false to disable cache. Defaults to 'cache'.
			'cacheExpire' => 0, // Cache lifetime. Defaults to 0 - means unlimited.
			'services' => array( // You can change the providers and their classes.
				'google' => array(
					'class' => 'GoogleOpenIDService',
					//'realm' => '*.example.org',
				),
				'yandex' => array(
					'class' => 'YandexOpenIDService',
					//'realm' => '*.example.org',
				),
				'twitter' => array(
					// register your app here: https://dev.twitter.com/apps/new
					'class' => 'TwitterOAuthService',
					'key' => '...',
					'secret' => '...',
				),
				'google_oauth' => array(
					// register your app here: https://code.google.com/apis/console/
					'class' => 'GoogleOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
					'title' => 'Google (OAuth)',
				),
				'yandex_oauth' => array(
					// register your app here: https://oauth.yandex.ru/client/my
					'class' => 'YandexOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
					'title' => 'Yandex (OAuth)',
				),
				'facebook' => array(
					// register your app here: https://developers.facebook.com/apps/
					'class' => 'FacebookOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'linkedin' => array(
					// register your app here: https://www.linkedin.com/secure/developer
					'class' => 'LinkedinOAuthService',
					'key' => '...',
					'secret' => '...',
				),
				'github' => array(
					// register your app here: https://github.com/settings/applications
					'class' => 'GitHubOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'live' => array(
					// register your app here: https://manage.dev.live.com/Applications/Index
					'class' => 'LiveOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'vkontakte' => array(
					// register your app here: https://vk.com/editapp?act=create&site=1
					'class' => 'VKontakteOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'mailru' => array(
					// register your app here: http://api.mail.ru/sites/my/add
					'class' => 'MailruOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'moikrug' => array(
					// register your app here: https://oauth.yandex.ru/client/my
					'class' => 'MoikrugOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'odnoklassniki' => array(
					// register your app here: http://dev.odnoklassniki.ru/wiki/pages/viewpage.action?pageId=13992188
					// ... or here: http://www.odnoklassniki.ru/dk?st.cmd=appsInfoMyDevList&st._aid=Apps_Info_MyDev
					'class' => 'OdnoklassnikiOAuthService',
					'client_id' => '...',
					'client_public' => '...',
					'client_secret' => '...',
					'title' => 'Odnokl.',
				),
			),
		),
		...
	),
...

使用方法

操作

<?php
...
	public function actionLogin() {
		$serviceName = Yii::app()->request->getQuery('service');
		if (isset($serviceName)) {
			/** @var $eauth EAuthServiceBase */
			$eauth = Yii::app()->eauth->getIdentity($serviceName);
			$eauth->redirectUrl = Yii::app()->user->returnUrl;
			$eauth->cancelUrl = $this->createAbsoluteUrl('site/login');

			try {
				if ($eauth->authenticate()) {
					//var_dump($eauth->getIsAuthenticated(), $eauth->getAttributes());
					$identity = new EAuthUserIdentity($eauth);

					// successful authentication
					if ($identity->authenticate()) {
						Yii::app()->user->login($identity);
						//var_dump($identity->id, $identity->name, Yii::app()->user->id);exit;

						// special redirect with closing popup window
						$eauth->redirect();
					}
					else {
						// close popup window and redirect to cancelUrl
						$eauth->cancel();
					}
				}

				// Something went wrong, redirect to login page
				$this->redirect(array('site/login'));
			}
			catch (EAuthException $e) {
				// save authentication error to session
				Yii::app()->user->setFlash('error', 'EAuthException: '.$e->getMessage());

				// close popup window and redirect to cancelUrl
				$eauth->redirect($eauth->getCancelUrl());
			}
		}

		// default authorization code through login/password ..
	}

视图

<?php
	if (Yii::app()->user->hasFlash('error')) {
		echo '<div class="error">'.Yii::app()->user->getFlash('error').'</div>';
	}
?>
...
<h2>Do you already have an account on one of these sites? Click the logo to log in with it here:</h2>
<?php
	$this->widget('ext.eauth.EAuthWidget', array('action' => 'site/login'));
?>

获取更多用户数据(可选)

要获取所有必要的数据到您的应用程序中,您可以覆盖任何提供者的基类。基类存储在protected/extensions/eauth/services/。扩展类的示例可以在protected/extensions/eauth/custom_services/中找到。

覆盖基类后,您需要修改您的配置文件以设置新的类名。您可能还需要覆盖EAuthUserIdentity类来存储额外数据。

翻译(可选)

  • 将文件/protected/extensions/eauth/messages/[lang]/eauth.php复制到/protected/messages/[lang]/eauth.php以将EAuth扩展翻译成其他语言。
  • 要添加新语言,可以使用空文件/protected/extensions/eauth/messages/blank/eauth.php

许可证

之前我为LiStick.ru开发了此扩展,并且我仍然支持此扩展。

该扩展在新BSD许可证下发布,因此您可以在GitHub上找到最新版本。