knobjeсtоr/steam-authentication-bundle

Symfony Bundle 用于集成 Steam 认证

安装次数: 9,471

依赖项: 0

建议者: 0

安全: 0

星级: 11

关注者: 2

分支: 4

开放问题: 0

类型:symfony-bundle

1.4.0 2024-02-01 21:18 UTC

This package is auto-updated.

Last update: 2024-08-30 22:38:27 UTC


README

GitHub license GitHub issues GitHub issues GitHub issues Packagist Downloads

SteamAuthenticationBundle - 为 Symfony 提供Steam认证

SteamAuthenticationBundle 提供了一种简单的方法来集成 Steam 的 OpenID 登录功能到您的应用程序中。

目录

安装

步骤 1 - 安装包

composer require knojector/steam-authentication-bundle

步骤 2 - 配置

knojector_steam_authentication:
    login_success_redirect: 'app.protected_route'
    login_failure_redirect: 'app.error_route'

如你所见,配置中只有两个选项,它们都非常直观。选项 login_success_redirect 包含用户登录成功后应重定向到的路由名称。选项 login_failure_redirect 包含用户登录失败后应重定向到的路由。

此外,您还需要调整您的 security.yml。以下是一个基本配置的示例。需要考虑的两个重要事项是

  • 防火墙名称必须是 steam
  • 只要 Steam CommunityId 是查询属性,您就可以使用任何用户提供者
security:
    providers:
        users:
            entity:
                class: 'App\Entity\User'
                property: 'username'
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        steam:
            pattern: ^/
            lazy: true
            provider: users

最后一步是在您的 routes.yaml 中启用包的控制器

steam_authentication_callback:
  path: /steam/login_check
  controller: Knojector\SteamAuthenticationBundle\Controller\SteamController::callback

使用方法

步骤 1 - 创建您自己的注册订阅者

从技术上讲,此包中没有提供注册功能。该包从 Steam 接收一个 ID,并尝试通过配置的用户提供者加载一个用户。如果不存在用户,则可以假设该用户是第一次登录。而不是抛出异常,该包会分发动件供你订阅。以下是一个简单的订阅者示例

<?php

namespace App\Subscriber;

use App\Entity\User;
use Knojector\SteamAuthenticationBundle\Event\AuthenticateUserEvent;
use Knojector\SteamAuthenticationBundle\Event\FirstLoginEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class FirstLoginSubscriber implements EventSubscriberInterface
{
    public function __construct(private EventDispatcherInterface $eventDispatcher)
    {}

    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents()
    {
        return [
            FirstLoginEvent::NAME => 'onFirstLogin'
        ];
    }

    public function onFirstLogin(FirstLoginEvent $event)
    {
        $communityId = $event->getCommunityId();
        
        $user = new User();
        $user->setUsername($communityId);
        
        // e.g. call the Steam API to fetch more profile information
        // e.g. create user entity and persist it
        
        // dispatch the authenticate event in order to sign in the new created user.
        $this->eventDispatcher->dispatch(new AuthenticateUserEvent($user), AuthenticateUserEvent::NAME);
    }
}

步骤 2 - 放置登录按钮

要将“通过 Steam 登录”按钮放置在页面上,您可以在模板中包含以下代码片段

{% include '@KnojectorSteamAuthentication/button.html.twig' %}

有虫子或有想法?

随时提出问题或提交拉取请求 😉

要求

该包需要

  • PHP 8.0.0+
  • Symfony 5.3/6