tomaszbrunarski/yii-eauth

我的分支(修复json编码的Facebook token)是Nodge/yii-eauth EAuth扩展,允许用户通过OpenID、OAuth 1.0和OAuth 2.0提供者进行身份验证。

1.1.1 2017-04-09 11:55 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:16:11 UTC


README

Yii EAuth扩展

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

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

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

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

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

该扩展允许您:

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

扩展包含

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

包含的服务

  • OpenID
    • Google
    • Yahoo
    • Steam
    • Wargaming
    • Yandex (ru)
  • OAuth1
    • Twitter
    • LinkedIn
  • OAuth2
    • Google
    • Facebook
    • Live
    • GitHub
    • Yandex (ru)
    • VKontake (ru)
    • Mail.ru (ru)
    • Odnoklassniki (ru)
    • Moi Krug(ru)
    • Dropbox
    • EVE Online

资源

要求

安装

  • 安装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',
				),
				'steam' => array(
					'class' => 'SteamOpenIDService',
					//'realm' => '*.example.org',
				),
				'yahoo' => array(
					'class' => 'YahooOpenIDService',
					//'realm' => '*.example.org',
				),
				'wargaming' => array(
					'class' => 'WargamingOpenIDService'
				),
				'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.',
				),
				'dropbox' => array(
					// register your app here: https://www.dropbox.com/developers/apps/create
					'class' => 'DropboxOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'eve' => array(
					// register your app here: https://developers.eveonline.com/applications
					'class' => 'EveOnlineOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
				),
				'slack' => array(
					// register your app here: https://api.slack.com/applications/new
					'class' => 'SlackOAuthService',
					'client_id' => '...',
					'client_secret' => '...',
					'title' => 'Slack',
				),

			),
		),
		...
	),
...

用法

演示项目

演示的源代码可在此处找到。

基本设置

操作

<?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 上找到最新版本。