pe/symfony-bundle-oauth2-client

league/oauth2-client 的 Symfony 集成

dev-master / 1.0.x-dev 2023-02-19 04:50 UTC

This package is auto-updated.

Last update: 2024-09-19 07:59:55 UTC


README

此扩展包集成了 league/oauth2-client

安装

通过运行以下命令使用 Composer 安装库

composer require pe/symfony-bundle-oauth2-client

然后在你的 kernel 中启用该扩展包

<?php
// app/AppKernel.php
class AppKernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new PE\Bundle\OAuth2ClientBundle\PEOAuth2ClientBundle(),
            // ...
        ];
    }
}

或者对于 Symfony 4.0

<?php
// SF 4.0 config/bundles.php

return [
    PE\Bundle\OAuth2ClientBundle\PEOAuth2ClientBundle::class => ['all' => true],
];

配置

添加到配置文件,以下为使用 Facebook 提供者的示例,所有提供者选项与构造函数参数数组的键匹配

pe_oauth2_client:
    driver: orm
    class:
        social_account: App\Entity\SocialAccount
    provider:
        facebook:
            class: \League\OAuth2\Client\Provider\Facebook
            options:
                clientId: 123
                clientSecret: 456
                graphApiVersion: v2.12

添加安全验证器

security:
    # other security config
    firewalls:
        some_firewall:
            # other firewall options
            guard:
                authenticators:
                    - pe_oauth2_client.security.authenticator

创建实体

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="oauth2_social_accounts")
 */
class SocialAccount extends \PE\Bundle\OAuth2ClientBundle\Model\SocialAccount
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="guid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @var string
     */
    protected $id;
}