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
- Add an authorization policy class under
/src/Policy. (example policy)
Update Application.php
- Have the
Applicationclass implementAuthorizationServiceProviderInterface - Add these lines to
Application::bootstrap():$this->addPlugin('Authentication'); $this->addPlugin('SimpleSaml');
- Add
getAuthorizationService()andgetAuthenticationService()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
tokenFieldto the name of the database field to identify users with (e.g.'sso-uuid') - Set the value of
dataFieldto the name of the field in the data received from the authenticator (e.g.'id')
- Set the value of
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
- Set up a VirtualHost alias (or its equivalent in non-Apache servers) or a symlink for
/vendor/simplesamlphp/simplesamlphp/www, named something like/simplesaml - Navigate to
/vendor/simplesamlphp/simplesamlphpin 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
- Copy the SimpleSAML
/config-templatesdirectory to/config/simplesamlat the root of the project - Set the
SIMPLESAMLPHP_CONFIG_DIRenvironment 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
- If you're doing this via PHP, you would use
- Set
baseurlpathvalue to the full URL path to access SimpleSAML's www directory, e.g.'baseurlpath' => 'https://example.com/simplesaml-alias-name/' - If SimpleSAML's metadata files need to be edited
- Copy the library's
/metadata-templatesdirectory to/config/simplesaml/metadatafrom the project's root - Update the metadatadir value in
/config/simplesaml/config.php
'metadatadir' => CONFIG . 'simplesaml' . DS . 'metadata'',
- Copy the library's
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 ($paramsis 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 215
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-15