oauth2-client-bundle symfony 集成类型通用

oauth2-client-bundle symfony integration type generic

提问人:cointreau17 提问时间:11/17/2023 最后编辑:cointreau17 更新时间:11/17/2023 访问量:32

问:

我尝试在 symfony 6.3 中配置 oauth2-client-bundle 2.16 以连接到 laravel passport 中制作的通用 oauth2 服务器,但没有成功。我现在的问题是,我需要METHOD_POST

http:// localhost: 8080/oauth/authorize?scope = public_profile%20email & state = 292c1d03896bac3ef8c6109dbcbae4a & responsibility_type = code & approval_prompt = auto & redirect_uri = http%3a% A8081%2FCONNECT%2FPASSPORTSERVER%2FCHECK & CLIENT_ID = 1

因为根据文档,此路由必须有一个标头,client_secret转换为 base64。

授权:基本my_client_secret_base64

护照服务器之所以有效,是因为失眠后我可以访问 url:

enter image description here

连接开始的控制器:

    /**
     * @Route("/connect/passport", name="connect_passport_start")
     */
    public function connectAction(ClientRegistry $clientRegistry)
    {
        // on Symfony 3.3 or lower, $clientRegistry = $this->get('knpu.oauth2.registry');

        // will redirect to passport server
        return $clientRegistry
            ->getClient('passport_oauth') // key used in config/packages/knpu_oauth2_client.yaml
            ->redirect([
                'public_profile', 'email' // the scopes you want to access
            ]);
    }

文件knpu_oauth2_client.yaml:

knpu_oauth2_client:
    clients:
        # will create service: "knpu.oauth2.client.passport_oauth"
        # an instance of: KnpU\OAuth2ClientBundle\Client\OAuth2Client
        
        # configure your clients as described here: https://github.com/knpuniversity/oauth2-client-bundle#configuration
        passport_oauth:
            # this will be one of the supported types
            type: generic
            provider_class: App\Provider\PassportProvider
            
            # optional: a class that extends OAuth2Client
            # client_class: Some\Custom\Client

            # optional: if your provider has custom constructor options
            # provider_options: {}

            # now, all the normal options!
            
            client_id: '%env(OAUTH_PASSPORT_ID)%'
            client_secret: '%env(OAUTH_PASSPORT_SECRET)%'
            # the route that you're redirected to after
            # see the controller example below
            redirect_route: connect_passport_check
            redirect_params: {}
            # whether to check OAuth2 "state": defaults to true
            # use_state: true

mi App\Provider\PassportProvider.php

<?php

namespace App\Provider;

use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Provider\GenericProvider;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;

class PassportoProvider extends AbstractProvider
{
    use BearerAuthorizationTrait;
    
    protected function getAccessTokenMethod(): string
    {
        return self::METHOD_POST;
    }
    
    /**
     * @var string Key used in a token response to identify the resource owner.
     */
    const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'user_id';

    /**
     * Get authorization url to begin OAuth flow
     *
     * @return string
     */
    public function getBaseAuthorizationUrl(): string
    {
        return 'http://localhost:8080/oauth/authorize';
    }

    /**
     * Get access token url to retrieve token
     *
     * @return string
     */
    public function getBaseAccessTokenUrl(array $params): string
    {
        return 'http://localhost:8080/oauth/token';
    }

    /**
     * Get provider url to fetch user details
     *
     * @param  AccessToken $token
     *
     * @return string
     */
    public function getResourceOwnerDetailsUrl(AccessToken $token): string
    {
        return 'https://localhost:8080/account';
    }

    /**
     * Get the default scopes used by this provider.
     *
     * This should not be a complete list of all scopes, but the minimum
     * required for the provider user interface!
     *
     * @return array
     */
    public function getDefaultScopes(): array
    {
        return [];
    }

    /**
     * Returns the string that should be used to separate scopes when building
     * the URL for requesting an access token.
     *
     * @return string Scope separator, defaults to ','
     */
    protected function getScopeSeparator(): string
    {
        return ' ';
    }

    /**
     * Check a provider response for errors.
     *
     * @throws IdentityProviderException
     * @param  ResponseInterface $response
     * @param  string $data Parsed response data
     * @return void
     */
    protected function checkResponse(ResponseInterface $response, $data): void
    {
        $statusCode = $response->getStatusCode();
        if ($statusCode >= 400) {
            throw new IdentityProviderException(
                isset($data['message']) ? $data['message'] : $response->getReasonPhrase(),
                $statusCode,
                $response
            );
        }
    }

    /**
     * Generate a user object from a successful user details request.
     *
     * @param object $response
     * @param AccessToken $token
     * @return PassportResourceOwner
     */
    protected function createResourceOwner(array $response, AccessToken $token): PassportResourceOwner
    {
        return new PassportResourceOwner($response);
    }
}

帮助,谢谢

护照服务器之所以有效,是因为失眠后我可以访问 url:

enter image description here

但是,如果我请求mi控制器的这个端点:

http://localhost:8081/connect/passport

它在内部是通过 GET 方法完成的,并且 de 导航器中的结果是:

enter image description here

我想我需要通过 POST 方法发出请求,但我不知道该怎么做。谢谢

symfony oauth-2.0 thephpleague

评论


答: 暂无答案