定制 singh/simplesaml 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

singh/simplesaml

Composer 安装命令:

composer require singh/simplesaml

包简介

This package provides a wrapper to onelogin/php-saml library.

README 文档

README

This package offers Saml2 integration as a Service Provider. It uses OneLogin APIs to connect with IPD and retreive parsed data. The code was tested on Laravel Homestead Virtual machine with PHP 7.1 and Laravel 5.5.28.

Installation

You can install this project using composer command

composer require singh/simplesaml

Laravel Configuration

You need to update the below code to execute this package

  1. First make sure you run the php artisan vendor:publish command. This command will copy the saml2_settings.php file to config folder.

  2. Next, you want to update settings inside this folder or add environment variables to .env file for idp_host, sp_entityid, ipd_entityid, and idp_x509. Here are the sample settings:

     #SAML2 Settings
     SAML2_IDP_HOST=https://developer.oktapreview.com
     SAML2_SP_ENTITYID=myapp
     SAML2_IDP_URI="/saml2/idp/ssoservice.php"
     SAML2_IDP_ENTITYID=http://www.okta.com/exkd9nlyw4oshZ4U80h8
     SAML2_IDP_x509="..."
    
  3. Update config\app.php with the following:

     'aliases' => [
         ....
         'Saml2' => Singh\SimpleSaml\Facades\Saml2Auth::class,
     ];
     'providers' => [
         ....
         Singh\SimpleSaml\Providers\SimpleSamlServiceProvider::class,
     ];
    
  4. Inside the Kernel.php, you would want to setup few things for saml to work as follows: Update middlewaregroup block:

         protected $middlewareGroups = [
             .....
             'saml2group' => [
                 \App\Http\Middleware\EncryptCookies::class,
                 \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
                 \Illuminate\Session\Middleware\StartSession::class,
                 \Illuminate\View\Middleware\ShareErrorsFromSession::class,
                 \Illuminate\Routing\Middleware\SubstituteBindings::class,
             ],
         ];
     Also, add the following line to the routeMiddleware block:
         protected $routeMiddleware = [
             ....
             'saml2' => \Singh\SimpleSaml\Middleware\Saml2Middleware::class,
         ];
    
  5. Update EventServiceProvider.php with the following:

     protected $listen = [
             ....
             'Singh\SimpleSaml\Events\Saml2LoginEvent' => [
                 'App\Listeners\UserLoggedIn'],  
         ];
    
  6. Finally, create Listener class inside /Listeners folder as follows:

     <?php
    

namespace App\Listeners;

use App\User;

use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Singh\SimpleSaml\Events\Saml2LoginEvent;

class UserLoggedIn {

/**
 * Create the event listener.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Handle the event.
 *
 * @param  Saml2LoginEvent  $event
 * @return void
 */
public function handle(Saml2LoginEvent $event)
{
    if (!$event->getSaml2Auth()->isAuthenticated()) {
        Log::info('The user is not authenticated');
        return redirect(config('saml2_settings.logoutRoute'));
    }

    $samlUser = $event->getSaml2User();
   
    $attributes = $samlUser->getAttributes();
    
    //check if email already exists and fetch user
    $user = \App\User::where('email', $attributes['email'][0])->first();
    
    //if email doesn't exist, create new user
	if ($user === null)
	{		
		$user = new \App\User;
        $user->email = $attributes['email'][0];
        $user->firstname = $attributes['firstname'][0];
        $user->lastname = $attributes['lastname'][0];
		$user->save();
	}

    if (count($attributes) >= 4) {
        //Add values to PHP and Laravel Session
        session()->put('email', $attributes['email'][0]);
        session()->put('firstname', $attributes['firstname'][0]);
        session()->put('lastname', $attributes['lastname'][0]);
        
        //The below block is useful if your application host both laravel and non-larvel code in one domain.
        session_start();
        $_SESSION['email'] = $user->email;
        $_SESSION['shortname'] = $user->shortname;
        $_SESSION['firstname'] = $user->firstname;
        $_SESSION['lastname'] = $user->lastname;
    }
    
    session()->save();

    Auth::login($user, true);
}

}

### Credits: This project was based on aacotrnoeo/laravel-saml2 package.

singh/simplesaml 适用场景与选型建议

singh/simplesaml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 153 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 12 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「SSO」 「laravel」 「SAML2」 「single sign-on」 「okta」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 singh/simplesaml 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 singh/simplesaml 我们能提供哪些服务?
定制开发 / 二次开发

基于 singh/simplesaml 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 153
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-14