maenbn/openamauth-laravel

Laravel的OpenAM认证驱动

dev-master 2017-10-18 08:52 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:06:00 UTC


README

这是一个为Laravel 5添加OpenAM驱动到您的认证系统的提供程序

安装

要安装此包,请运行以下Composer命令

composer require maenbn/openamauth

您还需要通过进入config/app.php并添加以下内容到providers键来注册服务提供程序

Maenbn\OpenAmAuthLaravel\Providers\OpenAmServiceProvider::class,

配置

您需要配置包以适用于您的OpenAM服务器。首先发布供应商资产

$ php artisan vendor:publish

这将创建一个位于您的应用程序中的config/openam.php文件,您可以修改它以反映您的OpenAM服务器。

最后,请确保在config/auth.php中将driver键的值更改为openam

Eloquent模型

还有一个选项可以使用Eloquent模型作为OpenAM认证的用户对象。如果您想要在OpenAM上进行认证但想在Laravel中控制授权(例如,使用Entrust包),这将非常有用。

理想情况下,新安装的Laravel中找到的默认App\User类非常适合此用例。将eloquentModel键修改为指向您想要使用的Eloquent类,并将eloquentUid键修改为在config/openam.php文件中将OpenAM uid存储到您的用户表列中,例如:

'eloquentModel' => App\User::class,

'eloquentUidName' => 'username',

最后,修改您的Eloquent模型以使用OpenAM的Authenticatable特质,并扩展Laravel的Model类而不是Authenticable类,如下所示

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
use Maenbn\OpenAmAuthLaravel\Authenticatable

class User extends Model
{
    use Notifiable, Authenticatable;
    ..........

中间件

如果您需要您的应用程序设置一个cookie来存储OpenAM令牌,您可以使用此包中可用的中间件。将其添加到app/Http/Kernel.php中的中间件组

protected $middlewareGroups = [
    ...............
    \Maenbn\OpenAmAuthLaravel\Middleware\SetOpenAmCookie::class,
];

您还必须确保将OpenAM cookie名称添加到中间件中的except数组中,位于app/Http/Middleware/EncryptCookies.php中,以便在认证尝试期间需要验证时,令牌值不会被加密。

您可以在app/Http/Middleware/EncryptCookies.php中硬编码它或执行以下操作,确保将Closure类导入到中间件中

namespace app\Http\Middleware;

use Closure;
use Illuminate\Cookie\Middleware\EncryptCookies as BaseEncrypter;

class EncryptCookies extends BaseEncrypter
{
    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    protected $except = [
    ];

    public function handle($request, Closure $next)
    {
        $this->except[] = config('openam.cookieName');
        return parent::handle($request, $next);
    }
}

##使用现在,您的认证驱动程序正在使用OpenAM,您将能够使用Laravel的Auth类进行用户认证。

###示例

//Authenticating using the OpenAM TokenID from a cookie
Auth::attempt();
	
//Authenticating using user input
$input = Input::only('username', 'password');
Auth::attempt($input);

//Retrieving the OpenAM attributes of a logged in user
$user = Auth::user();