phantomwatson/cakephp-simple-saml 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

phantomwatson/cakephp-simple-saml

Composer 安装命令:

composer require phantomwatson/cakephp-simple-saml

包简介

SimpleSaml plugin for CakePHP

README 文档

README

Installation

Until this issue is resolved, SimpleSAML is incompatible with CakePHP's Bake package, so cakephp/bake must be removed before installing.

composer remove cakephp/bake

Then:

composer require phantomwatson/cakephp-simple-saml

Add an authorization policy

Update Application.php

  • Have the Application class implement AuthorizationServiceProviderInterface
  • Add these lines to Application::bootstrap():
    $this->addPlugin('Authentication');
    $this->addPlugin('SimpleSaml');
  • Add getAuthorizationService() and getAuthenticationService() methods, using the name of your policy class:
    /**
     * Returns the authorization service
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request Server request
     * @return \Authorization\AuthorizationServiceInterface
     */
    public function getAuthorizationService(ServerRequestInterface $request): AuthorizationServiceInterface
    {
        $mapResolver = new MapResolver();
        $mapResolver->map(ServerRequest::class, YourPolicyClass::class);
    
        return new AuthorizationService($mapResolver);
    }
    
    /**
     * Returns a service provider instance.
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request Request
     * @return \Authentication\AuthenticationServiceInterface
     */
    public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
    {
        $service = new AuthenticationService();
        $loginUrl = '/login';
    
        // Define where users should be redirected to when they are not authenticated
        $service->setConfig([
            'unauthenticatedRedirect' => $loginUrl,
            'queryParam' => 'redirect',
        ]);
    
        $service->loadAuthenticator('Authentication.Session');
        $service->loadAuthenticator('SimpleSaml.SimpleSaml');
    
        // Load identifiers
        $service->loadIdentifier('Authentication.Token', [
            // The field in the database to check against
            'tokenField' => '',
    
            // The field in the passed data from the authenticator
            'dataField' => '',
    
            /* The OrmResolver will search the Users table for a record with a tokenField with the same value as
             * dataField */
            'resolver' => [
                'className' => OrmResolver::class,
                'userModel' => 'Users',
                'finder' => 'all',
            ]
        ]);
    
        return $service;
    }
    • Set the value of tokenField to the name of the database field to identify users with (e.g. 'sso-uuid')
    • Set the value of dataField to the name of the field in the data received from the authenticator (e.g. 'id')

Update AppController.php

In AppController::initialize(), load the SimpleSamlComponent from the plugin:

$this->loadComponent('SimpleSaml.SimpleSaml', [
    //'authSource' => 'default-sp'
]);

Uncomment and change the value of authSource if needed.

Update User model

Have User entity class implement IdentityInterface and add the getIdentifier() and getOriginalData() methods to it. (example)

Get SimpleSAML's /www directory ready for being accessed

  1. Set up a VirtualHost alias (or its equivalent in non-Apache servers) or a symlink for /vendor/simplesamlphp/simplesamlphp/www, named something like /simplesaml
  2. Navigate to /vendor/simplesamlphp/simplesamlphp in the command line and run these two commands to download front-end dependencies and set up CSS and JS (this assumes that NodeJS is installed on the server).
    npm install
    npm run build
    

Configuration

  1. Copy the SimpleSAML /config-templates directory to /config/simplesaml at the root of the project
  2. Set the SIMPLESAMLPHP_CONFIG_DIR environment variable to the path to this new directory so SimpleSAML can access these config files.
    • If you're doing this via PHP, you would use putenv('SIMPLESAMLPHP_CONFIG_DIR=' . CONFIG . 'simplesaml');
    • Do not include a trailing slash in the path string
    • This can be placed in /config/bootstrap.php
  3. Set baseurlpath value to the full URL path to access SimpleSAML's www directory, e.g. 'baseurlpath' => 'https://example.com/simplesaml-alias-name/'
  4. If SimpleSAML's metadata files need to be edited
    1. Copy the library's /metadata-templates directory to /config/simplesaml/metadata from the project's root
    2. Update the metadatadir value in /config/simplesaml/config.php
    'metadatadir' => CONFIG . 'simplesaml' . DS . 'metadata'',

Run checks

Open the SimpleSAML web-accessible directory in a browser to confirm that it’s installed and configured correctly.

Using the component

All controllers should now have access to the SimpleSaml component, which provides these methods:

  • $this->SimpleSaml->isAuthenticated(); - Returns true if the user is logged in via SimpleSaml
  • $this->SimpleSaml->login($params); - Starts the authentication process ($params is documented at \SimpleSAML\Auth\Simple::login())
  • $this->SimpleSaml->logout(); - Logs the user out
  • $this->SimpleSaml->getUserAttributes(); - Returns the authenticated user's attributes from the SimpleSaml session, or an empty array if no user is authenticated

phantomwatson/cakephp-simple-saml 适用场景与选型建议

phantomwatson/cakephp-simple-saml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 215 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 phantomwatson/cakephp-simple-saml 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-15