Laravel 5 API server supports signature authentication
Github project address: github.com/HavenShen/l…
Api Authorized Signature Middleware for Laravel 5
about
The larsign
package authorized signature server.
Features
- Handles larsign requests
Installation
Require the havenshen/larsign
package in your composer.json
and update your dependencies:
$ composer require havenshen/larsign
Copy the code
Add the HavenShen\Larsign\LarsignServiceProvider
to your config/app.php
providers array:
HavenShen\Larsign\LarsignServiceProvider::class,
Copy the code
Add the HavenShen\Larsign\LarsignFacade
to your config/app.php
aliases array:
'Larsign' => HavenShen\Larsign\LarsignFacade::class,
Copy the code
Global usage
To allow Larsign for all your routes, add the HandleLarsign
middleware in the $middleware
property of app/Http/Kernel.php
class:
protected $middleware = [
// ...
\HavenShen\Larsign\HandleLarsign::class,
];
Copy the code
Group middleware
If you want to allow Larsign on a specific middleware group or route, add the HandleLarsign
middleware to your group:
protected $middlewareGroups = [
'web'= > [// ...].'api'= > [// ...
\HavenShen\Larsign\HandleLarsign::class,
],
];
Copy the code
Application route middleware
If you want to allow Larsign on a specific application middleware or route, add the HandleLarsign
middleware to your application route:
protected $routeMiddleware = [
// ...
'auth.larsign' => \HavenShen\Larsign\HandleLarsign::class,
];
Copy the code
Configuration
The defaults are set in config/larsign.php
. Copy this file to your own config directory to modify the values. You can publish the config using this command:
$ php artisan vendor:publish --provider="HavenShen\Larsign\LarsignServiceProvider"
Copy the code
return [
/* |-------------------------------------------------------------------------- | Larsign | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | * /
'headerName' => env('LARSIGN_HEADER_NAME'.'Larsign'),
'accessKey' => env('LARSIGN_ACCESS_KEY'.' '),
'secretKey' => env('LARSIGN_SECRET_KEY'.' ')];Copy the code
Add api route in routes/api.php
Copy this.
Route::middleware(['auth.larsign'])->group(function (a) {
Route::get('/larsign'.function (a) {
return [
'message'= >'done.'
]);
});
Copy the code
or
Route::get('/larsign'.function (a) {
return [
'message'= >'done.'
];
})->middleware('auth.larsign');
Copy the code
Client
Generate Larsign
signatures
- Assume the following management credentials:
AccessKey = "test"
SecretKey = "123456"
Copy the code
- Call interface address:
url = "https://larsign.dev/api/v1/test?page=1"
Copy the code
- The original string to be signed:
note: the time-stamping followed by a newline [currenttime + voucher valid seconds]
signingStr = "/api/v1/test? page=1\n1510986405"Copy the code
- Base64 url safe encode:
signingStrBase64UrlSafeEncode = "L2FwaS92MS90ZXN0P3BhZ2U9MQoxNTEwOTg2NDY1"
Copy the code
hmac_sha1
carriesSecretKey
encryption then base64 url safe encode:
sign = "MLKnFIdI-0TOQ4mHn5TyCcmWACU="
Copy the code
- The final administrative credentials are:
note: stitching
headerName
SpaceAccessKey
:sign
:signingStrBase64UrlSafeEncode
larsignToken = "Larsign test:MLKnFIdI-0TOQ4mHn5TyCcmWACU=:L2FwaS92MS90ZXN0P3BhZ2U9MQoxNTEwOTg2NDY1"
Copy the code
- Add http header:
note: header key in
config/larsign.php -> headerName
Larsign:Larsign test:MLKnFIdI-0TOQ4mHn5TyCcmWACU=:L2FwaS92MS90ZXN0P3BhZ2U9MQoxNTEwOTg2NDY1
Copy the code
Client signature authorization failed
Http Response: 403
Copy the code
Testing
$ phpunit
Copy the code
License
The MIT License (MIT). Please see License File for more information.