Skip to content

Commit a31897d

Browse files
committed
README v1
1 parent b905dd3 commit a31897d

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Laravel Doctrine ApiKey
2+
3+
[![Build Status](https://github.com/API-Skeletons/laravel-doctrine-apikey/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/API-Skeletons/laravel-doctrine-apikey/actions/workflows/continuous-integration.yml?query=branch%3Amain)
4+
[![Code Coverage](https://codecov.io/gh/API-Skeletons/laravel-doctrine-apikey/branch/main/graphs/badge.svg)](https://codecov.io/gh/API-Skeletons/laravel-doctrine-apikey/branch/main)
5+
[![PHP Version](https://img.shields.io/badge/PHP-8.0-blue)](https://img.shields.io/badge/PHP-8.0-blue)
6+
[![Total Downloads](https://poser.pugx.org/api-skeletons/laravel-doctrine-apikey/downloads)](//packagist.org/packages/api-skeletons/laravel-doctrine-apikey)
7+
[![License](https://poser.pugx.org/api-skeletons/laravel-doctrine-apikey/license)](//packagist.org/packages/api-skeletons/laravel-doctrine-apikey)
8+
9+
This repository provides a driver for Doctrine which can be added to an existing entity manager.
10+
The driver provies a set of entities which enable ApiKey authorization through HTTP middleware.
11+
Scopes are supported! This was the missing piece of other repositories which catalyzed the creation of this library.
12+
13+
## Installation
14+
15+
Run the following to install this library using [Composer](https://getcomposer.org/):
16+
17+
```bash
18+
composer require api-skeletons/laravel-doctrine-apikey
19+
```
20+
21+
## Quick Start
22+
23+
Add Service Provider to app.php
24+
```php
25+
'providers' => [
26+
...
27+
ApiSkeletons\Laravel\Doctrine\ApiKey\ServiceProvider::class,
28+
],
29+
```
30+
31+
Initialize the ApiKey service for your entity manager in `App\Providers\AppServiceProvider`
32+
```php
33+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
34+
35+
public function boot()
36+
{
37+
app(ApiKeyService::class)->init(app('em'));
38+
}
39+
```
40+
41+
Add an API key through the console
42+
```shell
43+
$ php artisan apikey:generate yourapikeyname
44+
```
45+
46+
Add the middleware to a protected route
47+
```php
48+
Route::name('api.resource::fetch')
49+
->get('resource', 'ResourceController::fetch')
50+
->middleware('auth.apikey');
51+
```
52+
53+
Begin making requests to your ApiKey protected resource using you key as a Bearer token in the Authorization header
54+
```sh
55+
Authorization: Bearer {key}
56+
```
57+

src/ServiceProvider.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ApiSkeletons\Laravel\Doctrine\ApiKey;
44

5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Http\Middleware\AuthorizeApiKey;
56
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
67
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
78

@@ -14,6 +15,9 @@ class ServiceProvider extends LaravelServiceProvider
1415
*/
1516
public function register()
1617
{
18+
$this->app->singleton(ApiKeyService::class, function ($app) {
19+
return new ApiKeyService();
20+
});
1721
}
1822

1923
/**
@@ -23,9 +27,7 @@ public function register()
2327
*/
2428
public function boot()
2529
{
26-
$this->app->singleton(ApiKeyService::class, function ($app) {
27-
return new ApiKeyService();
28-
});
30+
$this->app['router']->middleware('auth.apiKey', AuthorizeApiKey::class);
2931

3032
if ($this->app->runningInConsole()) {
3133
$this->commands([

0 commit comments

Comments
 (0)