sequelone/laravel-sso

此包最新版本(v1.0.0)没有可用的许可证信息。

v1.0.0 2023-08-04 14:41 UTC

This package is auto-updated.

Last update: 2024-09-04 17:15:39 UTC


README

此库提供Laravel框架与SSO(单点登录)的集成 https://en.wikipedia.org/wiki/Single_sign-on

开始使用

安装

在shell中执行

composer require sequelone/laravel-sso

发布sso配置文件

php artisan vendor:publish --provider="SequelONE\LaravelSso\SSOServiceProvider"

代理配置

设置环境变量

SSO_SERVER=http://server.address
SSO_BROKER_ID=BROKER ID
SSO_BROKER_SECRET=BROKER SECRET

Kernel.php中的routes键中注册中间件 SequelONE\LaravelSso\Middleware\AttachBroker

config/auth.php中注册 sso

'sso' => [
    'driver' => 'sso',
    'model' => \App\User::class
]

服务器配置

设置环境变量

SSO_TYPE=server

创建App\Sso\Server.php

<?php


namespace App\Sso;


use SequelONE\LaravelSso\Server as SsoServer;
use Jasny\ValidationResult;
use App\Models\User;

class Server extends SsoServer
{

    private $brokers = [
        '1' => 'secret1',
        '2' => 'secret2'
    ];


    /**
     * Authenticate using user credentials
     *
     * @param string $username
     * @param string $password
     * @return \Jasny\ValidationResult
     */
    protected function authenticate($username, $password)
    {
        if (!\Auth::guard('web')->validate(['email' => $username, 'password' => $password])) {
            return ValidationResult::error(trans('auth.failed'));
        }

        return ValidationResult::success();
    }

    /**
     * Get the secret key and other info of a broker
     *
     * @param string $brokerId
     * @return array
     */
    protected function getBrokerInfo($brokerId)
    {
        return !array_key_exists($brokerId, $this->brokers) ? null : [
            'id' => $brokerId,
            'secret' => $this->brokers[$brokerId]
        ];
    }

    /**
     * Get the information about a user
     *
     * @param string $username
     * @return array|object
     */
    protected function getUserInfo($username)
    {
        $user = User::whereEmail($username)->first();
        return !$user ? null : [
            'user' => $user,
        ];
    }
}

此文件将描述服务器SSO如何识别和验证代理。更多信息请参阅:https://github.com/jasny/sso