承接 henrik/token-auth 相关项目开发

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

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

henrik/token-auth

Composer 安装命令:

composer require henrik/token-auth

包简介

This package created for token based authorization

README 文档

README

How to install

composer require henrik/token-auth

Configurations For Laravel Framework.

  • Create a helper file app\Helpers\TokenManagerHelper.php.
<?php
namespace App\Helpers;
use HashAuth\TokenManager;

/**
 * Class TokenManagerHelper * @package App\Helpers
 */
 class TokenManagerHelper {
	  /**
	 * @return TokenManager
	 * @throws \Exception
	 */
	 public static function getManagerInstance()
	 {
		 return new TokenManager(
		  config('hash_auth.token_private_key'),
		  config('hash_auth.token_private_iv'),
		  config('hash_auth.signature_private_key')
		 );
	 }
 }
  • Then create hash_auth.config file into your laravel configs folder.
<?php

return [
  'token_private_key' => env('TOKEN_PRIVATE_KEY', ''),
  'token_private_iv' => env('TOKEN_PRIVATE_IV', ''),
  'signature_private_key' => env('SIGNATURE_PRIVATE_KEY', '')
];
  • Then add into your .env file this lines
SIGNATURE_PRIVATE_KEY="secret_line1"
TOKEN_PRIVATE_IV="secret_line2"
TOKEN_PRIVATE_KEY="secret_line3"
  • Type in your console

    php artisan make:middleware HashAuthFilterMiddleware

  • Then paste this code into created file

<?php

	namespace App\Http\Middleware;
	use App\Helpers\TokenManagerHelper;
	use Carbon\Carbon;
	use Closure;
	use HashAuth\Exceptions\HashAuthException;
	use Illuminate\Http\Request;
	use Illuminate\Http\Response;


    class HashAuthFilterMiddleware{
     /**
     * @param $request
     * @param Closure $next
     * @return mixed
     * @throws \Exception
     */
     public function handle(Request $request, Closure $next)  {
	     try {
			 $unparsed_token = $request->header("Authorization");
			 if (empty($unparsed_token)) {
			       $unparsed_token = $request->input('token');
		     } else {
			       $unparsed_token = str_replace('Bearer ', '', $unparsed_token);
		     }
		     $tokenManager = TokenManagerHelper::getManagerInstance();
		     $parsed_token = $tokenManager->parseToke($unparsed_token, [
			      'exp' => Carbon::now()->timestamp,
			      'sessId' => 0,
			      'browserId' => $request->header('User-Agent')
		      ]);
		      // $parsed_token  it's a  data which  is  saved into token
	      } catch (HashAuthException $e) {
		      return response(
		      [
			      'message' => 'You dont has access for this action'
		      ],
		      Response::HTTP_FORBIDDEN
		      );
	     }
	     return $next($request);
	 }
   }

open Kernel.php file and add middleware into $routeMiddleware like this

protected $routeMiddleware = [
	'auth' => \App\Http\Middleware\Authenticate::class,
	// ...
	'hash.auth' => \App\Http\Middleware\HashAuthFilterMiddleware::class,
	// ...
];
  • Add this functions into your User.php model
	 /**
	 * @param $request
	 * @return mixed
	 * @throws \Exception
	 */
	 public function createNewAccessToken($request, $user)
	 {
		  $tokenManager = TokenManagerHelper::getManagerInstance();
		  $claims = $this->getClaims($request);
		  $token = $tokenManager->makeToken($user, $claims);
		  return $token;
	 }
	 private function getClaims(Request $request)
	 {
		 $claims = [
			 'exp' => Carbon::now()->timestamp + (2 * 60 * 60),
			 'browserId' => $request->header('User-Agent'),
		 ];
		 return $claims;
	 }

The example login action (must return token string in json format):

 public function Login(User $user, Request $resuest){
	$token = $user->createNewAccessToken($request, $user);
	// ...
}

The registered middleware you can use for your routes. For example:

	Route::group(['middleware' => ['hash.auth']], function () {
		// your routes here
	}
	Route::get('your-route', 'Controller@Action')->middleware('hash.auth');

The request lifecycle in graph dagram.

graph LR
R[Request With Token] --> HAM
HAM --> R
HAM((HashAuthFilterMiddleware)) -->A
A-->HAM

A[Token Manager] -- Token --> TP((Token Parser))
TP -- data --> A
TB((Token Builder)) -- token --> A
A -- data --> TB
TB --> T{Token}
TP --> T
CL[Claim] --> T
DL[DataLine] -->T


Loading

henrik/token-auth 适用场景与选型建议

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

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

围绕 henrik/token-auth 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-05