承接 kasitaw/api-key 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

kasitaw/api-key

Composer 安装命令:

composer require kasitaw/api-key

包简介

User defined api key(using custom laravel guard) to enable client communicate with server for external integration in general

README 文档

README

This package makes it easy to authenticate users using user defined api key authentication guard with Laravel 6.0+

Installation

API Key can be installed via composer:

composer require "kasitaw/api-key"

The package will automatically register itself.

You can publish the migration with:

php artisan vendor:publish --provider="Kasitaw\ApiKey\ApiKeyServiceProvider" --tag=migrations

After the migration has been published, run the migrations with following command:

php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="Kasitaw\ApiKey\ApiKeyServiceProvider" --tag=config

This is the contents of the published config file:

<?php

return [
    /**
     * Model use to configure Api Key
     */
    'model' => [
        'api_key' => Kasitaw\ApiKey\ApiKey::class, // Make sure use Kasitaw\ApiKey\Traits\HasApiKey.php trait if you use your own modal
    ],

    /**
     * Table name that reflected to the above model.
     */
    'table_name' => [
        'api_keys' => 'api_keys', // Table name to the above model
    ],

    /**
     * Column name being used to store generated api key
     */
    'columns' => [
        'key' => 'key',
    ],

    /**
     * Field name that being used to fetch the "apiKey". Either passed through query params or as a body.
     */
    'request_key' => [
        'api_key' => 'api_key',
    ],

    /**
     * Generated key length.
     */
    'key_length' => 80,
];

Usages

Before started, configure config/auth.php guard as following:

'guards' => [
    'web' => [
        //
    ],

    'api' => [
        //
    ],

    /*
     * Adding new `api_key` key into guards section 
     */
    'api_key' => [
        'driver' => 'api_key',
    ]
],

Use HasApiKey.php trait inside App\User.php model or any model that implement \Illuminate\Contracts\Auth\Authenticatable interface:

<?php

namespace App;

use Kasitaw\ApiKey\Traits\HasApiKey;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiKey;
}

Call endpoint with middleware as following:

// Using `auth:api` as regular user authentication
Route::get('/users', function() {
    // 
})->middleware('auth:api');

// Using `auth:api_key` to authenticate user for external api
Route::get('/external/intergation/users', function() {
    dd(request()->user());
    // or using Auth::guard('api_key')->user()
    // or using auth('api_key')->user()
})->middleware('auth:api_key');

Finally, lets authenticate. 3 ways to pass in the generated key

  1. Using query params. i.e /users?api_key=xxx
  2. Using http body. i.e api_key = xxx
  3. Using Authorization header. i.e Authorization Bearer xxx

Notes: The request header should be provide Accept header. i.e Accept: application/json

Available Methods to manage the key

Generate new api key that ties up to the authenticate user

$user->generateNewKey(); // By default will activate the key, pass `false` params to make it inactive

Activate all existing keys

$user->activateAllKeys();

Activate the key using key

$user->activateKeyByKey('J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC');

// or 

$user->activateKeyByKey(
    'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC',
    '5c9fuEbAny4737an7hXC9VdNmDzd1yE0qn6Am9R8nNzJ0HWROn1daMJ19Lp36XLJlI5QIAkv6xYUkt6U'
);

Activate the key using uuid

$user->activateKeyByUuid('e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6');

// or

$user->activateKeyByUuid(
    'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6',
    '597a67f8-9c19-4c2b-98ff-8020c0f7e360'
);

Revoke all existing keys

$user->revokeAllKeys();

Revoke the key using key

$user->revokeKeyByKey('J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC');

// or 

$user->revokeKeyByKey(
    'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC',
    '5c9fuEbAny4737an7hXC9VdNmDzd1yE0qn6Am9R8nNzJ0HWROn1daMJ19Lp36XLJlI5QIAkv6xYUkt6U'
);

Revoked the key using uuid

$user->revokeKeyByUuid('e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6');

// or

$user->revokeKeyByUuid(
    'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6',
    '597a67f8-9c19-4c2b-98ff-8020c0f7e360'
);

Delete the key using key

$user->removeKeyByKey('J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC');

// or 

$user->removeKeyByKey(
    'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC',
    '5c9fuEbAny4737an7hXC9VdNmDzd1yE0qn6Am9R8nNzJ0HWROn1daMJ19Lp36XLJlI5QIAkv6xYUkt6U'
);

Delete the key using uuid

$user->removeKeyByUuid('e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6');

// or

$user->removeKeyByUuid(
    'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6',
    '597a67f8-9c19-4c2b-98ff-8020c0f7e360'
);

Get all keys

$keys = $user->api_keys;

foreach($keys as $key) {
    // 
}

Delete all keys

$user->removeAllKeys();

Get all active keys

$keys = $user->api_keys()->active()->get();

foreach($keys as $key) {
    // 
}

Get all in-active keys

$keys = $user->api_keys()->inActive()->get();

foreach($keys as $key) {
    // 
}

Check whether key is active

$key = $user->api_keys->first();

dd($key->isActive());

Or directly check the key is active

$uuid = 'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6';
$user->isKeyActive($uuid); // true/false, return null if key not found

// or
$key = 'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC';
$user->isKeyActive($key);

Test

Run test with following command

vendor/bin/phpunit --testdox --verbose

License

This package is open-sourced software licensed under the MIT license

kasitaw/api-key 适用场景与选型建议

kasitaw/api-key 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 97 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 kasitaw/api-key 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-23