keboola/api-bundle

Keboola API Bundle

维护者

详细信息

github.com/keboola/api-bundle

源代码

安装次数: 5,211

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 17

分支: 0

类型:symfony-bundle


README

Symfony 扩展包,为 Keboola API 应用程序提供常用功能。

安装

使用 Composer 安装此包

composer require keboola/api-bundle

配置

该扩展包期望在您的 Symfony 配置中定义 %app_name% 参数。

默认配置

keboola_api:
  app_name: '%app_name%'           # application name to use in user agent
  default_service_dns_type: public # default service DNS type to use in ServiceClient, can be 'public' or 'private'

功能

预配置的 ServiceClient

该扩展包提供预配置的 ServiceClient,可用于解析 Keboola API URL。默认情况下,它配置为使用公共主机名,但可以重新配置为使用内部主机名。

keboola_api:
  default_service_dns_type: internal

使用 ENV 变量

如果您需要使用 ENV 变量来配置 default_service_dns_type,请确保您提供一些默认值,否则验证将失败,错误信息为:The value "" is not allowed for path "keboola_api.default_service_dns_type".

parameters:
  env(API_DNS_TYPE): internal

keboola_api:
  default_service_dns_type: '%env(API_DNS_TYPE)%'

使用属性进行控制器认证

要使用属性认证,请配置防火墙使用 keboola.api_bundle.security.attribute_authenticator

security:
  firewalls:
    attribute:
        lazy: true
        stateless: true
        custom_authenticators:
          - keboola.api_bundle.security.attribute_authenticator

然后向您的控制器添加任何组合的认证属性

use Keboola\ApiBundle\Attribute\StorageApiTokenAuth;
use Keboola\ApiBundle\Security\StorageApiToken\SecurityApiToken;
use Symfony\Component\Security\Http\Attribute\CurrentUser;

#[StorageApiTokenAuth]
class Controller {
  public function __invoke(#[CurrentUser] StorageApiToken $token) 
  {
    // only requests with valid X-StorageApi-Token will be allowed
  }
}

#[StorageApiTokenAuth(features: ['feat-a', 'feat-b'])]
class Controller {
  public function __invoke(#[CurrentUser] StorageApiToken $token) 
  {
    // only requests with valid X-StorageApi-Token and project features 'feat-a' AND 'feat-b' is allowed
  }
}

#[StorageApiTokenAuth(features: ['feat-a'])]
#[StorageApiTokenAuth(features: ['feat-b'])]
class Controller {
  public function __invoke(#[CurrentUser] StorageApiToken $token) 
  {
    // only requests with valid X-StorageApi-Token and any of project features 'feat-a' OR 'feat-b' ise allowed
  }
}

#[ManageApiTokenAuth(scopes: ['something:manage'])]
#[StorageApiTokenAuth]
class Controller {
  public function __invoke(
    string $entityId,
    #[CurrentUser] TokenInterface $token,
  ) {
    // allows request with either valid X-KBC-ManageApiToken with 'something:manage' scope OR any valid X-StorageApi-Token
    // but with additional checks in controller
    $entity = $this->fetchEntity($entityId);
    if ($token instanceof StorageApiToken && $token->getProjectId() !== $entity->getProjectId()) {
      throw new AccessDeniedHttpException('...');
    }
  }
}

要使用单个认证属性,您需要安装适当的客户端包

  • 要使用 StorageApiTokenAuth,请安装 keboola/storage-api-client
  • 要使用 ManageApiTokenAuth,请安装 keboola/kbc-manage-api-php-client

注意

如果您忘记安装适当的客户端,您将获得类似以下的异常:Service "Keboola\ApiBundle\Attribute\ManageApiTokenAuth" not found: the container inside "Symfony\Component\DependencyInjection\Argument\ServiceLocator" is a smaller service locator

许可证

MIT 许可,请参阅 LICENSE 文件。