ryanaby / lumen-auth-key by ryanaby

Authorize requests using auth key(s)
11
0
1
Package Data
Maintainer Username: ryanaby
Maintainer Contact: aby@ryanaby.com (Ryan Aby)
Package Create Date: 2020-06-17
Package Last Update: 2020-06-17
Language: PHP
License: MIT
Last Refreshed: 2024-04-26 03:25:13
Package Statistics
Total Downloads: 11
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

Lumen Auth Key

Manage api key access from .env file.

Installation

Run composer require ryanaby/lumen-auth-key

In your bootstrap/app.php file, add the Lumen Auth Key provider to Register Service Providers blocks

// $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);
$app->register(Ryanaby\LumenAuthKey\Providers\AuthKeyServiceProvider::class);

Add Keys

Add your key on .env file, you can add more than one key using pipe | as separator.

AUTH_KEY=y0ur5ecur3K3y|an0th3rSecur3K3y

Usage

Implementing Authorization

You can use middleware auth.apikey on your route.

$router->get('api/post/1', ['middleware' => ['auth.apikey'], function () {
    //
}]);

Pass Authentication

Request must have X-auth-key headers to have access your API endpoint.

X-auth-key: y0ur5ecur3K3y

Failed Authentication

The failed authentication will return 401 header

{
    "errors": [
        {
            "message": "Unauthorized Access"
        }
    ]
}

Lumen Auth Key package is open-sourced software licensed under the MIT license.