nodge/yii-eauth

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

1.2.0 2017-04-09 19:49 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:41:34 UTC


README

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

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

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

在自己的服务器上实现授权有多个优点

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

该扩展允许您

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

扩展包括

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

包含的服务

  • OpenID
    • Yahoo
    • Steam
    • Wargaming
  • 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.
				'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 上找到最新版本。