roensby/symfony-drupal-jsonapi

一个支持Drupal实体的内置OAuth2认证的Symfony JSONAPI客户端。

安装次数: 15,496

依赖项: 0

建议者: 0

安全性: 0

星标: 0

分支: 0

类型:symfony-bundle

1.8.0 2020-11-02 13:39 UTC

README

一个使用OAuth2进行身份验证的内置支持Drupal实体的Symfony JSONAPI客户端。

安装说明

  1. 运行composer require roensby/symfony-drupal-jsonapi

  2. 配置库

     symfony_drupal_jsonapi:
       jsonapi:
         # The Drupal jsonapi endpoint.
         endpoint:             'http://example/jsonapi'
         # The Drupal CSRF endpoint.
         csrf_endpoint:        'http://example/session/token'
       oauth2:
         # The Drupal simple_oauth module endpoint.
         endpoint:             'http://example/oauth/token'
         # A uuid string given by a Drupal consumer entity.
         client_id:            'CLIENT ID'
         # A secret string shared between your Symfony application and Drupal.
         client_secret:        'CLIENT SECRET'
         # Use the interactive login if users log in.
         interactive_login:
           # Where to redirect when authentication fails.
           login_route:        app_login
           # Where to redirect when authentication succeeds.
           login_redirect:     frontpage
         # Use preset user for non-interactive applications.
         preset_user:
           # A Drupal username.
           username:           'USERNAME'
           # The corresponding password.
           password:           'SOME PASSWORD'
    
  3. 使用LoginAuthenticatorPresetAuthenticator类与OAuthRefreshAuthenticator类的组合。

使用说明

通过tid加载'标签'束的术语分类

<?php

namespace App\Controller;

use Roensby\SymfonyDrupalJsonApi\Entity\Core\Tag;
use Roensby\SymfonyDrupalJsonApi\Entity\Core\TagInterface;
use Roensby\SymfonyDrupalJsonApi\Factory\EntityFactory;
use Roensby\SymfonyDrupalJsonApi\JsonApi\Filter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class MyController extends AbstractController {

    /**
     * Renders a tag.
     */
    public function getTag(string $tid): Response
    {
        /** @var EntityFactory $entityFactory */        
        $entityFactory = $this->get(EntityFactory::class);
        /** @var TagInterface $tag */
        $tag = $entityFactory->query(Tag::class)
            ->addFilter(new Filter('drupal_internal__tid', '=', $tid))
            ->load();
        return $this->render('my_template', [
            'description' => $tag->getDescription(),
            'name' => $tag->getName(),
        ]);
    }
}

安全性

设置GuardAuthenticator类。

有关更多信息,请参阅使用Guard的自定义认证系统

security:
    encoders:
        Roensby\SymfonyDrupalJsonApi\Security\User\OAuthUser:
            algorithm: auto

    # https://symfony.com.cn/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        app_user_provider:
            id: Roensby\SymfonyDrupalJsonApi\Security\User\UserProvider
    firewalls:
        main:
          guard:
            authenticators:
              - Roensby\SymfonyDrupalJsonApi\Security\GuardAuthenticator\LoginAuthenticator
              - Roensby\SymfonyDrupalJsonApi\Security\GuardAuthenticator\OAuthRefreshAuthenticator
            entry_point: Roensby\SymfonyDrupalJsonApi\Security\LoginAuthenticator

使用OAuth进行请求认证。
有关更多信息,请参阅Simple OAuth模块