rafalniewinski/silex-steamauth

Silex 框架的 Steam Community 授权提供程序,使用 SecurityServiceProvider

v2.0.0 2016-06-07 21:34 UTC

This package is auto-updated.

Last update: 2024-09-15 06:56:00 UTC


README

此版本适用于 Silex ~2.0
(Silex ~1.3 的版本可在另一个分支中找到)

Silex-SteamAuth

Silex PHP 框架的 Steam Community 授权提供程序,使用 SecurityServiceProvider。

安装

如果您想使用 Composer 安装

composer require rafalniewinski/silex-steamauth "~2.0"

或者,您可以克隆此存储库

git clone https://github.com/RafalNiewinski/Silex-SteamAuth.git

##使用方法 此库使用标准的 SecurityServiceProvider 模块。请确保阅读该模块的操作说明

http://silex.sensiolabs.org/doc/providers/security.html

使用 Steam 作为授权提供程序类似于“使用表单保护路径”段落,除了将表单替换为 OpenID 和 Steam Community 机制。

###注册 使用此方法注册 SteamAuthServiceProvider 和核心 SecurityServiceProvider

$app->register(new SteamAuth\SteamAuthServiceProvider(), array(
    'steam_auth.host' => 'https://domain.tld' // your service domain to configure OpenID
));

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'admin' => array(
        'pattern' => '^/admin/',
        'steam_auth' => array('check_path' => '/admin/login_check'), // Only this line is different - see below
        'users' => array(
            'admin' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
        ),
    ),
));

如果您仔细阅读,您会注意到 SecurityServiceProvider 组件的注册与原始 Silex 教程中使用的表单保护相同,只是有一行不同 ‘form’ 被替换为 ‘steam_auth’

‘check_path’ 选项与表单授权中的作用相同(必须在受保护防火墙区域中定义)

重要:登录路径永久设置为 /login,并且必须可以匿名访问或位于防火墙之外

###交互 如果您想让用户登录,请将其重定向到 /login

如果您想获取已登录用户的 Steam ID,请执行

$app['security.token_storage']->getToken()->getUser()->getSteamID();

请注意,如果用户未登录,则 getToken()getUser() 可以返回 null

##定义用户提供者 Silex-SteamAuth 与标准 UserProviderInterface 兼容,但您必须使用用户 SteamID 作为用户名。

尽管兼容,但建议为了方便使用专门的 SteamAuthUserProviderInterface,其中包含 loadUserBySteamId() 方法。

以下是一个 SteamAuth 编辑的简单用户提供者示例,其中使用 Doctrine DBAL 存储用户

use SteamAuth\SteamAuthUserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Doctrine\DBAL\Connection;

class UserProvider implements SteamAuthUserProviderInterface
{
    private $conn;

    public function __construct(Connection $conn)
    {
        $this->conn = $conn;
    }

    public function loadUserBySteamId($steamid)
    {
        $stmt = $this->conn->executeQuery('SELECT * FROM users WHERE steamid = ?', array($steamid));

        if (!$user = $stmt->fetch())
        {
            //User never previously logged in from this steam accout - probably you should create new account now
            throw new UsernameNotFoundException(sprintf('steamid "%s" does not exist.', $steamid));
        }

        return new SteamAuthUser($user['steamid'], explode(',', $user['Roles']));
    }

    public function loadUserByUsername($steamid)
    {
        //Retain original method operating for compatibility
        return $this->loadUserBySteamId($steamid);
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof SteamAuthUser)
        {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
        }

        return $this->loadUserBySteamId($user->getSteamID());
    }

    public function supportsClass($class)
    {
        return $class === 'SteamAuth\SteamAuthUser';
    }

}

当然,您可以扩展 SteamAuthUser 类并自由扩展它