定制 consilience/laravel-api-token-generator 二次开发

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

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

consilience/laravel-api-token-generator

Composer 安装命令:

composer require consilience/laravel-api-token-generator

包简介

Artisan command to generate API tokens for laravel applications

README 文档

README

Laravel supports API token authentication out of the box. When developing, and for internal (machine-to-machine) APIs, it is useful to be able to generate tokens for a user. This package provides a simple Artisan command to generate a token.

The token will be hashed using the sha256 algorithm.

This package does not provide a database migration for the api_token column. That is left to your application.

Installation

Installing with composer:

composer require consilience/laravel-api-token-generator

Lumen

For Laravel, the service provider and configuration file are registered automatically. With Lumen, additional entried are needed in bootstrap/app.php.

The service provider is registered:

$app->register(Consilience\Laravel\ApiTokenGenerator\Providers\ServiceProvider::class);

If the configuration file is published, add:

$app->configure('apitokens');

then copy apitokens.php:

cp vender/consilience/laravel-api-token-generator/apitokens.php config/apitokens.php

Publishing assets:

php artisan vendor:publish --provider="Consilience\ApiTokenGenerator\ApiTokenGeneratorServiceProvider"

Configuration

You can change the model that will hold the API tokens. By default this will be App\User, but yu may want App\Models\User for example.

'model' => App\Models\User::class,

The name_field is an alternative column to id that can be used to uniquely identify a model instance:

'name_field' => 'name'

The token column will be api_token by default, but can be changed:

'token_field' => 'my_api_token_column',

Note: this package does not add the API token column to your users table. That is a step for other packages or your application deployment.

Usage

Generate a new token or replace the existing token for a user:

php artisan apitoken:generate --id=123 --generate
php artisan apitoken:generate --id=5fd40c23-fcda-4bdc-a07c-f2bfeb56bb03 --generate

The id is normally an integer, but some this should also work if the id is a string such as UUID.

A generated token will only be displayed once. It is encrypted for saving against the model, so cannot be recovered if not recorded immediately.

Where users are uniquely identified by another column, then that column can be used to identify the model instance to update with a new token:

php artisan apitoken:generate --name=bloggs@example.com --generate

Rather than generating a random token, you can set your own explicit tokanes. Use the --token= option to do this instead of the --generate option.

php artisan apitoken:generate --id=123 --token=d8a928b2043db77e340b523547bf16cb4aa483f0645fe0a290ed1f20aab76257

If using for automated deployment, you may want to use the --no-ansi option to remove control characters.

The --check option will tell you whether an instance has a token set or not:

$ php artisan apitoken:generate --id=11 --check
App\Models\User::11 has no API token set
No explicit token supplied (--token=) and no token to be generated (--generate)

consilience/laravel-api-token-generator 适用场景与选型建议

consilience/laravel-api-token-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 590 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 consilience/laravel-api-token-generator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-04