vatsim / sso
Requires
- php: >=5.4.0
- bonroyage/oauth: 1.*
This package is not auto-updated.
Last update: 2023-01-23 03:33:49 UTC
README
Laravel 4 兼容,使用 Laravel 5 请 选择版本 2
VatsimSSO 包集成了 VATSIM.net 单点登录,允许用户使用 VATSIM ID 登录。这对于官方 vACC 和 ARTCC 非常有用。
版本
1.0
安装
使用 Composer 安装 VatsimSSO 和依赖项。
$ composer require vatsim/sso 1.*
Laravel
设置
通过 Service Providers,在 Laravel 中使用 VatsimSSO 变得很容易。将服务提供者添加到您的 app/config/app.php
文件中
'providers' => array( // ... 'Vatsim\OAuth\OAuthServiceProvider', ),
然后是别名
'aliases' => array( // ... 'VatsimSSO' => 'Vatsim\OAuth\Facades\SSO', ),
配置文件
使用 artisan 发布配置文件。运行命令后,您将在 app/config/packages/vatsim/sso/config.php
中找到该文件。根据需要进行更改。
$ artisan config:publish vatsim/sso
Laravel 之外
让我们首先创建一个配置文件,以保持我们的代码整洁。
/* * DO NOT PUBLISH THE KEY, SECRET AND CERT TO CODE REPOSITORIES * FOR SECURITY. */ /* * The location of the VATSIM OAuth interface */ $base = 'https://'; /* * The consumer key for your organisation (provided by VATSIM) */ $key = 'MY_KEY'; /* * The secret key for your organisation (provided by VATSIM) * Do not give this to anyone else or display it to your users. It must be kept server-side */ $secret = 'my_secret'; /* * The signing method you are using to encrypt your request signature. * Different options must be enabled on your account at VATSIM. * Options: RSA / HMAC */ $method = 'HMAC'; /* * Your RSA **PRIVATE** key * If you are not using RSA, this value can be anything (or not set) */ $cert = ''; /* * The URL users will be redirected to after they log in, this should * be on the same server as the request */ $return = 'http://example.com/valiatelogin';
现在,让我们初始化 SSO 类。
// load the Composer autoload file, which automatically // loads all the classes required for use by VatsimSSO. require 'vendor/autoload.php'; require 'config.php'; use Vatsim\OAuth\SSO; $sso = new SSO($base, $key, $secret, $method, $cert);
使用方法
登录
第一步是向 VATSIM 发送请求,让用户登录。最简单的方法是使用 login
函数。该函数接受三个参数。
参数
参数 | 类型 | 描述 |
---|---|---|
$returnUrl |
string | array | 登录成功后用户应重定向到的 URL |
$success |
string | Closure | 包含在您能够让用户进行认证时(例如,当您的密钥/密钥正确时)需要执行的操作的回调函数。该函数将返回三个变量: $key 、$secret 和 $url 。 |
$error |
string | Closure | 默认:null – 包含在您的凭据(即密钥/密钥)不正确时需要执行的操作的回调函数。 |
对于 $success
和 $error
,您可以通过 [class]@[method]
格式传递字符串以调用另一个 Model 中的函数,否则传递匿名函数。
返回 URL
返回 URL 参数也将接受数组而不是字符串。在此数组中,您可以添加 suspended
和/或 inactive
的值,以允许挂起或非活动账户的成员登录。此数组中第一个有效的 URL 将用作返回 URL。
成功
成功参数返回三个变量: $key
、$secret
和 $url
。 key
和 secret
应该存储在会话中以便进行验证过程。 url
将用于将用户重定向到 VATSIM SSO 网站。
错误
可选参数。如果忽略此参数并且发生错误,函数将返回 false
。如果您传递一个函数,则将返回一个参数 $error
,它是一个与最后错误相关的数据数组。
示例
// Laravel return VatsimSSO::login( Config::get('vatsimsso:return'), function($key, $secret, $url) { Session::put('vatsimauth', compact('key', 'secret')); return Redirect::to($url); }, function($error) { throw new Exception('Could not authenticate: ' . $error['message']); } ); // Outside Laravel $sso->login( $return, function($key, $secret, $url) { $_SESSION['vatsimauth'] = compact('key', 'secret'); header('Location: ' . $url); die(); } );
如果您不希望使用 ->login()
函数,您可以使用 ->requestToken($returnUrl)
。这将返回一个包含 key
和 secret
的对象,或者在发生错误时返回 false
,此时您可以使用 ->error()
来获取最后发生的错误数组。然后使用 ->sendToVatsim()
来获取重定向的 URL。
验证登录
登录成功后,我们需要从 VATSIM 获取用户数据。为此,我们也为您编写了一个函数来简化这个过程。
参数
参数 | 类型 | 描述 |
---|---|---|
$key |
字符串 | 登录时存储在会话中的 key |
$secret |
字符串 | 登录时存储在会话中的 secret |
$verifier |
字符串 | 查询字符串中传递的 oauth_verifier |
$success |
string | Closure | 回调函数,包含在登录成功时需要执行的操作。 |
$error |
string | Closure | 默认值:null – 包含在身份验证失败时需要执行的操作的回调函数(可能是由于错误的 key/secret/verifier 导致) |
对于 $success
和 $error
,您可以通过 [class]@[method]
格式传递字符串以调用另一个 Model 中的函数,否则传递匿名函数。
成功
成功参数返回两个变量: $user
和 $request
。 user
变量将是一个对象,包含您组织机构可用的所有用户数据。 request
变量将提供有关请求的信息。
错误
可选参数。如果忽略此参数并且发生错误,函数将返回 false
。如果您传递一个函数,则将返回一个参数 $error
,它是一个与最后错误相关的数据数组。
示例
// Laravel $session = Session::get('vatsimauth'); return VatsimSSO::validate( $session['key'], $session['secret'], Input::get('oauth_verifier'), function($user, $request) { // At this point we can remove the session data. Session::forget('vatsimauth'); Auth::loginUsingId($user->id); return Redirect::home(); }, function($error) { throw new Exception('Could not authenticate: ' . $error['message']); } ); // Outside Laravel $session = $_SESSION['vatsimauth']; $sso->validate( $session['key'], $session['secret'], $_GET['oauth_verifier'], function($user, $request) { // At this point we can remove the session data. unset($_SESSION['vatsimauth']); // do something to log the user in on your site using the user id // $user->id // Redirect home header('Location: /'); die(); } );
如果您不希望使用 ->validate()
函数,您可以使用 ->checkLogin($key, $secret, $verifier)
。这将返回一个包含 user
和 request
对象的对象,或者在发生错误时返回 false
,此时您可以使用 ->error()
来获取最后发生的错误数组。
许可证
MIT
免费软件,太棒了!