这是一个可以创建新账户并利用第三方供应商连接来验证用户的库。
Requires
- php: >=5.3
- ext-curl: *
- ext-json: *
- abraham/twitteroauth: 0.9.2
- facebook/graph-sdk: ^5.4
- google/apiclient: ^2.0@dev
Requires (Dev)
- mockery/mockery: ^0.9 || ^1.0@dev
- phpunit/phpunit: ~4.8 || ~6.0
Suggests
- stripe/stripe-php: Allows using the Stripe Connection
This package is auto-updated.
Last update: 2024-09-16 06:53:11 UTC
README
这是一个可以创建新账户并利用第三方供应商连接来验证用户的库。
支持的供应商
- Amazon
- GitHub
- Slack
- Spotify
- Stripe
- Yahoo
- Zoom
结构
有三个主要功能。
- 第三方登录操作
- 身份验证过程
- 撤销对您的客户端应用程序的访问权限
第三方登录操作
使用以下代码将用户重定向到供应商的登录页面。以下以Google为例。
use TSK\SSO\ThirdParty\Google\GoogleConnectionFactory; $googleConnectionFactory = new GoogleConnectionFactory(); $googleConnection = $googleConnectionFactory->get( 'google_client_id', 'google_client_secret', 'http://www.your-amazing-app.com/sso/google/grant' ); header("Location: $googleConnection->getGrantUrl()");
身份验证过程
使用以下代码进行注册/登录。以下以Google为例。请注意,您必须实现 TSK\SSO\AppUser\AppUserRepository
以根据您的应用程序逻辑创建和验证用户。请参阅 examples
目录中的示例。
默认验证器使用
use TSK\SSO\Auth\DefaultAuthenticator; use TSK\SSO\Auth\Exception\AuthenticationFailedException; use TSK\SSO\ThirdParty\Exception\NoThirdPartyEmailFoundException; use TSK\SSO\ThirdParty\Exception\ThirdPartyConnectionFailedException; use TSK\SSO\ThirdParty\Google\GoogleConnectionFactory; use YouApp\TSKSSO\YourImplementationOfTheAppUserRepository; $googleConnectionFactory = new GoogleConnectionFactory(); $googleConnection = $googleConnectionFactory->get( 'google_client_id', 'google_client_secret', 'http://www.your-amazing-app.com/sso/google/grant' ); $authenticator = new DefaultAuthenticator( new YourImplementationOfTheAppUserRepository() ); try { $appUser = $authenticator->authenticate($googleConnection); } catch (AuthenticationFailedException $ex) { } catch (DataCannotBeStoredException $ex) { } catch (NoThirdPartyEmailFoundException $ex) { } catch (ThirdPartyConnectionFailedException $ex) { } catch (\Exception $ex) { } // log the detected application's user in $_SESSION['userId'] = $appUser->id();
请注意,使用 TSK\SSO\Auth\DefaultAuthenticator
将仅使用您的逻辑在用户存储中进行简单查找。如果您想支持多个供应商并避免为每个特定的电子邮件地址创建新用户,您必须使用此 TSK\SSO\Auth\PersistingAuthenticator
。
持久验证器使用
默认情况下,它使用文件系统作为用户映射的存储。
use TSK\SSO\Auth\PersistingAuthenticator; use YouApp\TSKSSO\YourImplementationOfTheAppUserRepository; $authenticator = new PersistingAuthenticator( new YourImplementationOfTheAppUserRepository() );
MySQL
有两个类可供您使用MySQL作为存储。
对于MySQL,我在sql文件夹下提供了一个模式文件。请使用该文件。
TSK\SSO\Storage\PdoThirdPartyStorageRepository
use TSK\SSO\Auth\PersistingAuthenticator; use TSK\SSO\Storage\PdoThirdPartyStorageRepository; use YouApp\TSKSSO\YourImplementationOfTheAppUserRepository; $authenticator = new PersistingAuthenticator( new YourImplementationOfTheAppUserRepository(), new PdoThirdPartyStorageRepository( // In Laravel, you can do this to get its PDO connection: \DB::connection()->getPdo(); new PDO('mysql:dbname=db;host=localhost', 'foo', 'bar'), 'Optional Table Name (default:thirdparty_connections)' ), );
TSK\SSO\Storage\MysqliThirdPartyStorageRepository
use TSK\SSO\Auth\PersistingAuthenticator; use TSK\SSO\Storage\PdoThirdPartyStorageRepository; use YouApp\TSKSSO\YourImplementationOfTheAppUserRepository; $authenticator = new PersistingAuthenticator( new YourImplementationOfTheAppUserRepository(), new MysqliThirdPartyStorageRepository(new mysqli('localhost', 'foo', 'bar', 'db')), );
MongoDB
TSK\SSO\Storage\PeclMongoDbThirdPartyStorageRepository
use TSK\SSO\Auth\PersistingAuthenticator; use TSK\SSO\Storage\PeclMongoDbThirdPartyStorageRepository; use YouApp\TSKSSO\YourImplementationOfTheAppUserRepository; $authenticator = new PersistingAuthenticator( new YourImplementationOfTheAppUserRepository(), new PeclMongoDbThirdPartyStorageRepository(new MongoDB\Driver\Manager('mongodb://:27017/yourdb'), 'yourdb'), );
当然,您可以通过实现此接口: TSK\SSO\Storage\ThirdPartyStorageRepository
使用自己的存储。
撤销供应商对您的客户端应用程序的访问权限
use TSK\SSO\ThirdParty\VendorConnectionRevoker; $vendorConnectionRevoker = new VendorConnectionRevoker( $googleConnection, // the vendor connection // [optional] `TSK\SSO\Storage\ThirdPartyStorageRepository` implementation. File system storage is used by default ); $vendorConnectionRevoker->revoke($vendorEmail, $vendorName); // returns a bool
在登录时连接多个账户。
-
用户可能在单个供应商上拥有多个账户。例如:具有不同电子邮件地址的多个Facebook/Google账户。
-
或者,用户可以在同一时间拥有Facebook和Google等供应商的账户。您可能希望让他们连接其他账户,以便他们更容易地使用多个供应商进行验证/访问。
您可以使用 TSK\SSO\Auth\AppUserAwarePersistingAuthenticator
验证他们选择的账户。
use TSK\SSO\AppUser\ExistingAppUser; use TSK\SSO\Auth\AppUserAwarePersistingAuthenticator; use TSK\SSO\Auth\PersistingAuthenticator; use YouApp\TSKSSO\YourImplementationOfTheAppUserRepository; $userId = $_SESSION['userid']; if (!is_null($userId)) { $authenticator = new AppUserAwarePersistingAuthenticator( new ExistingAppUser($userId, 'current-loggedin-user-email@tsk-webdevelopment.com') ); } else { $authenticator = new PersistingAuthenticator( new YourImplementationOfTheAppUserRepository() ); }
接下来是什么?
添加任何缺少的供应商支持和任何其他存储系统。
演示
创建自己的应用程序 [可选]
我已经创建了几个演示应用程序,并在Amazon、GitHub、Google、Twitter和Yahoo上注册了它们。如果需要测试,您也可以注册自己的应用程序。
- Amazon : https://sellercentral.amazon.com/hz/home
- GitHub : https://github.com/settings/developers
- Google : https://console.developers.google.com
- Twitter : https://developer.twitter.com/en/apps - 您至少需要具有“只读”访问权限,并在附加权限下勾选了“从用户请求电子邮件地址”。
- Spotify : https://developer.spotify.com/dashboard/applications
- Yahoo : https://developer.yahoo.com/apps - 您必须至少选择 '读取/写入公开和私有' 的 '配置文件(社交目录)' API 权限。
主机文件条目
然后按照以下步骤将 localhost.com
添加到主机文件中。 (Linux: /etc/hosts
,Windows: C:\Windows\System32\drivers\etc\hosts
)
127.0.0.1 localhost.com
开始演示
make demo
然后转到 https://.com