avvertix / caslite
一个用于处理基于CAS认证的Laravel包。
Requires
- php: >=5.5.9
- illuminate/support: ~5.1
- jasig/phpcas: ^1.3
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-08 22:51:15 UTC
README
Caslite为Laravel 5.1应用提供了中央认证服务(CAS)认证。
灵感来源于 Laravel Socialite
安装
要开始使用Caslite,请将其添加到您的 composer.json 文件中作为依赖项
composer require avvertix/caslite
配置
安装Caslite库后,在您的 config/app.php 配置文件中注册 Avvertix\Caslite\CasliteServiceProvider
'providers' => [ // Other service providers... Avvertix\Caslite\CasliteServiceProvider::class, ],
同时,将Caslite门面添加到您的应用配置文件中的别名数组中
'Caslite' => Avvertix\Caslite\Facades\Caslite::class,
您还需要添加配置以访问您的应用程序使用的CAS服务器。这些配置参数应放在您的 config/services.php 配置文件中,并使用键 cas。以下代码块显示了在配置参数中应添加的内容
'cas' => [ /* |-------------------------------------------------------------------------- | phpCAS Debug |-------------------------------------------------------------------------- | | @var boolean true to enable debug, log file will be written in storage/logs/cas.log | */ 'cas_debug' => env('CAS_DEBUG', false), /* |-------------------------------------------------------------------------- | phpCAS Hostname |-------------------------------------------------------------------------- | | Example: 'login.uksw.edu.pl' | @var string */ 'cas_hostname' => env('CAS_HOSTNAME', ''), /* |-------------------------------------------------------------------------- | CAS Port |-------------------------------------------------------------------------- | | Usually 443 is default | @var integer */ 'cas_port' => env('CAS_PORT', 443), /* |-------------------------------------------------------------------------- | CAS URI |-------------------------------------------------------------------------- | | Usually '/cas' is default | @var string */ 'cas_uri' => env('CAS_URI', '/cas'), /* |-------------------------------------------------------------------------- | CAS login URI |-------------------------------------------------------------------------- | | Empty is fine | @var string */ 'cas_login_uri' => env('CAS_LOGIN_URI', ''), /* |-------------------------------------------------------------------------- | CAS logout URI |-------------------------------------------------------------------------- | | Example: 'https://login.uksw.edu.pl/cas/logout?service=' | Empty is fine | @var string */ 'cas_logout_uri' => env('CAS_LOGOUT_URI', ''), /* |-------------------------------------------------------------------------- | CAS Validation |-------------------------------------------------------------------------- | | CAS server SSL validation: 'self' for self-signed certificate, 'ca' for | certificate from a CA, empty for no SSL validation | @var string */ 'cas_validation' => env('CAS_VALIDATION', ''), /* |-------------------------------------------------------------------------- | CAS Certificate |-------------------------------------------------------------------------- | | Path to the CAS certificate file | @var string */ 'cas_cert' => env('CAS_CERT', ''), /* |-------------------------------------------------------------------------- | Use SAML to retrieve user attributes |-------------------------------------------------------------------------- | | CAS can be configured to return more than just the username to a given | service. It could for example use an LDAP backend to return the first name, | last name, and email of the user. This can be activated on the client side | by setting 'cas_saml' to true | @var boolean */ 'cas_saml' => env('CAS_SAML', false), /* |-------------------------------------------------------------------------- | SAML group name attribute |-------------------------------------------------------------------------- | | If you are using SAML with LDAP backend you can simply check if logged | user is member of specific group. Type below LDAP's group attribute | name | @var string */ 'cas_saml_attr_groups' => env('CAS_SAML_ATTR_GROUPS', 'Groups'), /* |-------------------------------------------------------------------------- | CAS session name |-------------------------------------------------------------------------- | | Define your CAS session name | @var string */ 'cas_session_name' => env('CAS_SESSION_NAME', 'CAS_SESSION'), ],
如您所注意到的,大多数参数都引用了环境变量,因此您可以根据每个环境自定义它们。通常,您只需在 .env 文件中包含以下变量(值仅为示例)
CAS_HOSTNAME=cas-server-host.com
CAS_URI=cas
使用
接下来,您已准备好对用户进行认证!您需要两个路由:一个用于将用户重定向到CAS提供者,另一个用于接收提供者认证后的回调。我们将使用Caslite门面来访问Caslite
<?php namespace App\Http\Controllers; use Caslite; use Illuminate\Routing\Controller; class AuthController extends Controller { // The Laravel AuthController might contain other methods and traits, please preserve them while editing /** * Redirect the user to the CAS authentication page. * * @return Response */ public function redirectToProvider() { return Caslite::authenticate(); } /** * Obtain the user information from CAS. * * @return Response */ public function handleProviderCallback() { $user = Caslite::user(); // $user->getEmail; // here you can store the returned information in a local User model on your database (or storage). // This is particularly usefull in case of profile construction with roles and other details // e.g. Auth::login($local_user); } }
authenticate 方法负责将用户发送到CAS认证提供者,而 user 方法将读取传入的请求并从提供者获取用户信息。
当然,您需要定义路由到您的控制器方法
Route::get('auth/cas', 'Auth\AuthController@redirectToProvider'); Route::get('auth/cas/callback', 'Auth\AuthController@handleProviderCallback');
检索用户详细信息
一旦您有了用户实例,您就可以获取更多有关用户的信息
$user = Caslite::user(); $user->getEmail();
登出
当用户从您的应用程序登出时,您必须调用 Caslite::logout() 来执行CAS登出。
贡献
请参阅 contributing 和 conduct 了解详细信息。
许可证
Caslite在 MIT许可证 下开源。