jonathonwalz/shibboleth-bundle

此包已被 废弃,不再维护。作者建议使用 kuleuven/shibboleth-bundle 包。

Symfony2 的 Shibboleth 认证提供者

安装次数: 1,093

依赖者: 1

建议者: 0

安全性: 0

星标: 1

关注者: 1

分支: 21

类型:symfony-bundle

v1.1.1 2015-06-18 19:46 UTC

This package is auto-updated.

Last update: 2019-04-06 19:19:56 UTC


README

此包为您的 Symfony2 项目添加了 Shibboleth 认证提供者。

要求

  • [PHP][@php] 5.3.3 及以上。
  • [Symfony 2.1][@symfony]

安装

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: Shib-Person-uid
		use_headers: true

上述列出的配置值是默认值。要使用默认值,只需在配置中使用以下行即可:

	# app/config/config.yml
	shibboleth: ~

可用的 Shibboleth 属性

默认情况下,此包通过用户令牌公开了几个 Shibboleth 属性,ShibbolethUserToken。令牌提供了对大多数属性的特定访问器,以及通用的访问器 getAttributegetArrayAttributehasAttributeValue。每个属性都通过别名内部标识,该别名作为上述方法的参数。以下表格列出了通过用户令牌可用的 Shibboleth 属性(当提供时):

属性 别名
Shib-Person-uid uid
Shib-Person-commonName cn
Shib-Person-surname sn
Shib-Person-givenName givenName
Shib-Person-mail mail
Shib-Person-ou ou
Shib-Person-telephoneNumber telephoneNumber
Shib-Person-facsimileTelephoneNumber facsimileTelephoneNumber
Shib-Person-mobile mobile
Shib-Person-postalAddress postalAddress
Shib-EP-UnscopedAffiliation affiliation
Shib-EP-Scopedaffiliation scopedAffiliation
Shib-EP-OrgunitDN orgUnitDN
Shib-EP-OrgDN orgDN
Shib-logoutURL logoutURL
Shib-Identity-Provider identityProvider
Shib-Origin-Site originSite
Shib-Authentication-Instant authenticationInstant
Shib-KUL-员工类型 员工类型
Shib-KUL-学生类型 学生类型
Shib-KUL-primou编号 primou编号
Shib-KUL-ou编号 ou编号
Shib-KUL-毕业证书 毕业证书
Shib-KUL-opl opl
Shib-KUL-校区 校区

如果由于某些原因您想传递额外的属性(例如自定义属性),您可以这样配置

# 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)

包含每个属性配置的键将是它的别名。这意味着可以通过以下方式检索 shib-acme-fooshib-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';
		}
	}