鲁汶大学 /shibboleth-bundle
Symfony2 的 Shibboleth 认证提供者
This package is not auto-updated.
Last update: 2024-09-14 15:07:12 UTC
README
此扩展为您的 Symfony2 项目添加了 Shibboleth 认证提供者。
要求
- PHP 5.3.3 及以上版本。
- [Symfony 2.2+][https://symfony.com.cn]
安装
ShibbolethBundle 与 composer 兼容。
1. 在您的 composer.json 中添加 ShibbolethBundle
"require": { ... "kuleuven/shibboleth-bundle": "dev-master" ... }, "repositories": [ { "type": "vcs", "url": "git@github.com:rmoreas/ShibbolethBundle.git" } ],
现在运行以下命令,让 composer 下载扩展
php composer.phar update kuleuven/shibboleth-bundle
Composer 将将扩展安装到项目 vendor/kuleuven 目录中。
2. 启用扩展
在您的 kernel 中实例化扩展
// app/AppKernel.php <?php // ... public function registerBundles() { $bundles = array( // ... new KULeuven\ShibbolethBundle\ShibbolethBundle(), ); }
配置
1. 在 Apache 中启用懒加载 Shibboleth 认证
将以下行添加到项目 web 目录下的 .htaccess 文件中
# web/.htaccess AuthType shibboleth ShibRequireSession Off ShibUseHeaders On require shibboleth
2. 设置认证防火墙
# app/config/security.yml security: firewalls: secured_area: pattern: ^/secured shibboleth: ~ logout: path: /secured/logout target: / success_handler: security.logout.handler.shibboleth
3. Shibboleth 配置
可能的配置参数包括
# app/config/config.yml shibboleth: handler_path: /Shibboleth.sso secured_handler: true session_initiator_path: /Login username_attribute: uid use_headers: true
上面列出的配置值是默认值。要使用默认值,只需在您的配置中使用以下行即可
# app/config/config.yml shibboleth: ~
可用的 Shibboleth 属性
默认情况下,该扩展通过用户令牌公开了几个 Shibboleth 属性,ShibbolethUserToken。令牌为大多数属性提供了特定的访问器,以及通用的访问器 getAttribute
、getArrayAttribute
和 hasAttributeValue
。每个属性内部通过别名识别,该别名用作上述方法的参数。以下表格列出了通过用户令牌可用的 Shibboleth 属性(当提供时)
如果出于某种原因您想传递额外的属性(例如自定义属性)或覆盖现有的属性,您可以按照这种方式进行配置
# app/config/config.yml shibboleth: # ... attribute_definitions: foo: # the attribute alias header: shib-acme-foo # the attribute name bar: header: shib-acme-bar multivalue: true # attribute contains multiple values (default is false, i.e. attribute is scalar) identityProvider: header: REDIRECT_Shib-Identity-Provider # Change the existing attribute server: REDIRECT_Shib_Identity_Provider # Change the name of the variable with use_header option off
包含每个属性配置的键将是其别名。这意味着可以通过 shib-acme-foo
和 shib-acme-bar
属性的值来检索
$foo = $token->getAttribute('foo'); $bars = $token->getArrayAttribute('bar'); // returns an array containing the multiple values
用户提供者
此扩展不包含任何用户提供者,但您可以自己实现。
如果您在数据库中存储用户,则可以在用户第一次登录您的应用程序时动态创建用户。您的 UserProvider 需要实现 KULeuven\ShibbolethBundle\Security\ShibbolethUserProviderInterface
接口。
示例
此示例使用 Propel ORM 存储用户。
<?php namespace YourProjectNamespace\Security; use YourProjectNamespace\Model\User; use YourProjectNamespace\Model\UserQuery; use KULeuven\ShibbolethBundle\Security\ShibbolethUserProviderInterface; use KULeuven\ShibbolethBundle\Security\ShibbolethUserToken; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\Exception\UnsupportedUserException; class UserProvider implements ShibbolethUserProviderInterface { public function loadUserByUsername($username) { $user = UserQuery::create()->findOneByUsername($username); if($user){ return $user; } else{ throw new UsernameNotFoundException("User ".$username. " not found."); } } public function createUser(ShibbolethUserToken $token){ // Create user object using shibboleth attributes stored in the token. // $user = new User(); $user->setUid($token->getUsername()); $user->setSurname($token->getSurname()); $user->setGivenName($token->getGivenName()); $user->setMail($token->getMail()); // If you like, you can also add default roles to the user based on shibboleth attributes. E.g.: if ($token->isStudent()) $user->addRole('ROLE_STUDENT'); elseif ($token->isStaff()) $user->addRole('ROLE_STAFF'); else $user->addRole('ROLE_GUEST'); $user->save(); return $user; } public function refreshUser(UserInterface $user) { if (!$user instanceof User) { throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); } return $this->loadUserByUsername($user->getUsername()); } public function supportsClass($class) { return $class === 'YourProjectNamespace\Model\User'; } }