定制 wassa/apikey-authenticator-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wassa/apikey-authenticator-bundle

Composer 安装命令:

composer require wassa/apikey-authenticator-bundle

包简介

Wassa ApiKeyAuthenticatorBundle

关键字:

README 文档

README

The Symfony website has a great tutorial on how to create an API token authentication system. It's crystal clear and very easy to reproduce. From our point of view though, it has 2 main issues:

  1. If you have a lot of projects (which is our case at Wassa), it can be quite time consuming to reproduce it for every single project
  2. The tutorial is more targeted toward what we call "multi-users" environment, in which a single API is linked to a single user.

This bundle aims to address these 2 issues.

Requirements

  • Symfony 2.8

Installation

First run:

composer require wassa/apikey-authenticator-bundle

Then update AppKernel.php:

...
new Wassa\ApiKeyAuthenticatorBundle\WassaApiKeyAuthenticatorBundle(),
...

The API key must be stored in var/private/api.key. You can create the file yourself or use the built-in command to create a key:

mkdir -p var/private
php bin/console apikey-authenticator:create-key [apiKey] [-s size]

If you don't provide an API key, the command will use the default generator (see "Creating your own generator") to create a random key. The default size for the random key is 32 chars but can be configured (see "Configuration") or manually set it with the -s switch.

Remember to set correct permissions on the key file so that it is readable only by the webserver.

Configuration

The bundle works without any special configuration. Add and edit the following block to config.yml if necessary:

wassa_api_key_authenticator:
    role: 'ROLE_API'                                            # Role that will be assigned to authenticated requests
    name: 'x-api-key'                                           # Name of the "field" that must contain the API key
    location: 'all'                                             # Where to look for the API key in the request
    generator: 'wassa_api_key_authenticator.random_generator'   # Generator to use to generate the API key
    key_size: 32                                                # Size of the generated API key

The name parameter specify the name of the "field" in the request containing the API key.

The location parameter specifies which "field" to look for the API key in the request:

  • headers: look for an HTTP header
  • query: look for a query parameter
  • body: look for a POST data parameter
  • all: look for all above and returns the first found (in order above)

location can be a combination of different values, for example 'headers&query' to look in headers and query but not in the body.

Then edit security.yml:

    ...
    providers:
        api_key_provider:
            id: wassa_api_key_authenticator.user_provider
    ...
    firewalls:
        ...
        main:
            ...
            guard:
                authenticators:
                    - wassa_api_key_authenticator.authenticator
    ...
    access_control:
        ...
        - { path: ^/api, roles: ROLE_API }
        ...

Be sure that access_control is configured with the same role as in config.yml.

Creating your own API key generator

If you need your API key to be a bit more complex than a series of X chars, you can create your own generator.

In order to do that, just create a service that implements ApiKeyGeneratorInterface and write your own logic in the generate() method.

Then configure the bundle in config.yml:

wassa_api_key_authenticator:
    ...
    generator: 'app.my_generator'

Composer post-install script

You can automate the creation of the API key by including the built-in post-install script in your composer.json:

"scripts": {
    "post-install-cmd": [
        ...
        "Wassa\\ApiKeyAuthenticatorBundle\\Composer\\ScriptHandler::generateApiKey"
    ]
},
"extra": {
    ...
    "apikey-size": 128
}

apikey-size is optional, if you don't specify it, the key will be created with the configured key size.

Also, if an API key already exists, it will not be overriden.

Managing multiple API keys

For now our bundle handles only one API key, that's what it was made for in the first place so it's OK. If you want to handle multiple keys, then you can just follow these easy steps:

  • Create your own User class that implements our ApiKeyUserInterface and provide an implementation for getApiKey()
  • Create your own UserProvider class or use one that suits you (like FOSUserBundle).

This part of the tutorial can help you to do that.

wassa/apikey-authenticator-bundle 适用场景与选型建议

wassa/apikey-authenticator-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 244 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 wassa/apikey-authenticator-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wassa/apikey-authenticator-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 wassa/apikey-authenticator-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 244
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-25