silverbullet / api-token-laravel
Token-based API service authentication & authorization for Laravel
Requires
- php: ^7.0
README
Setup
Installation
$ composer require silverbullet/api-token-laravel
Configuration
Add service provider to the providers
array in config/app.php
file:
'providers' => [ ... Silverbullet\ApiTokenLaravel\Providers\ApiTokenServiceProvider::class ],
Publish the migration file:
php artisan vendor:publish --provider="Silverbullet\ApiTokenLaravel\Providers\ApiTokenServiceProvider"
Run the migration:
php artisan migrate
Usage
API Token Commands
Generate new API Token
$ php artisan api-token:generate {name} {code?}
List all API Token
$ php artisan api-token:list
Delete API Token by id
$ php artisan api-token:delete {id}
Middleware
Use middleware with the key apitoken.auth:{code}
on your Larave route. Example:
Route::get('partner-products', function() { // })->middleware('apitoken.auth:{service1}');
You can also pass multiple parameters for the {code}
(e.g. apitoken.auth{service1,service2}
). This feature enables you to authorize the service that is going to use your API.
Authorize Request
To pass the middleware, you must include Authorization
header as a part of your request.
Authorization: Basic {api_token_goes_here}
Response
-
401
: UnauthenticatedThis error code means you do not have the valid token.
-
403
: UnauthorizedThis error code means you do have a valid token but not the permission to access the API you are trying to access.
License
The MIT License (MIT). Please see License File for more information.