silverbullet/api-token-laravel

Token-based API service authentication & authorization for Laravel

1.1 2021-02-06 23:45 UTC

This package is auto-updated.

Last update: 2024-04-10 05:29:34 UTC


README

Latest Version on Packagist Software License Total Downloads

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: Unauthenticated

    This error code means you do not have the valid token.

  • 403: Unauthorized

    This 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.