dormilich/http-oauth-bundle

dormilich/http-oauth 的 Symfony 5 扩展包,用于为需要OAuth 2.0授权的请求添加授权。不需要授权(或缺少授权凭证)的请求将无需授权运行。

安装: 2

依赖项: 0

建议: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放性问题: 0

类型:symfony-bundle

dev-main 2021-08-16 10:37 UTC

This package is auto-updated.

Last update: 2024-09-16 17:17:44 UTC


README

dormilich/http-oauth 的 Symfony 5 扩展包,它是 dormilich/http-client 的扩展,旨在为需要授权的请求添加OAuth 2.0授权。不需要授权(或缺少授权凭证)的请求将无需授权运行。

安装

此扩展包需要 Symfony 5 以及PSR-16、PSR-17和PSR-18的实现。

然而,在Symfony项目中,使用 symfony/cache 作为PSR-16和 symfony/http-client 作为PSR-18实现是有意义的。

您可以通过composer安装此扩展包

composer require dormilich/http-oauth-bundle

要设置 dormilich/http-client,还需要安装 dormilich/http-client-bundle

配置

配置是设置OAuth凭证的标准方式。如果没有添加至少一种凭证类型,则授权将被忽略。

所有请求使用单一凭证集

当您只有一组凭证时,设置是最简单的。这将用于使用HTTP客户端进行的每个请求(即使不需要授权)。

# note that the actual credentials should be loaded from the environment
dormilich_http_oauth:
  credentials:
    default:
      client: '%env(CLIENT_ID)%'
      secret: '%env(CLIENT_SECRET)%'
      server: https://example.com/oauth/token

特定资源域的凭证

这是为了将一组凭证与一组特定域相关联(使用Google API进行演示)。其他请求不会添加Authorization头。

# this will only add authorisation to requests to googleapis.com (and its subdomains)
dormilich_http_oauth:
  credentials:
    domains:
      - client: '%env(CLIENT_ID)%'
        secret: '%env(CLIENT_SECRET)%'
        server: https://example.com/oauth/token
        hosts: googleapis.com

自定义凭证策略

如果上述配置策略都不符合您的需求,则可以创建自己的凭证提供程序,通过创建一个实现 CredentialsProviderInterface 的类。为了使扩展使用此提供程序,重新定义 CredentialsProviderInterface 服务为您自己的类。

# services.yaml
services:
  Dormilich\HttpOauth\Credentials\CredentialsProviderInterface:
    class: App\CustomCredentialsProvider

或者,如果您需要使用多个提供程序,只需对这些提供程序进行标记即可。这也与配置的提供程序结合使用。

# services.yaml
services:
  App\CustomCredentialsProvider:
    tags: dormilich_http_oauth.credentials