2017 © Pedro Peláez
 

library api-guard

A simple way of authenticating your APIs with API keys using Laravel. Forked from chrisbjr/api-guard And Working with L5.5.*

image

bizly/api-guard

A simple way of authenticating your APIs with API keys using Laravel. Forked from chrisbjr/api-guard And Working with L5.5.*

  • Friday, September 29, 2017
  • by torrancemiller
  • Repository
  • 3 Watchers
  • 1 Stars
  • 1,314 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 134 Forks
  • 0 Open issues
  • 34 Versions
  • 0 % Grown

The README.md

ApiGuard

Latest Stable Version Total Downloads, (*1)

Join the chat at https://gitter.im/chrisbjr/api-guard, (*2)

A simple way of authenticating your APIs with API keys using Laravel. This package uses the following libraries:, (*3)

The concept for managing API keys is also taken from Phil Sturgeon's codeigniter-restserver. I've been looking for an equivalent for Laravel but did not find any so this is an implementation for that., (*4)

PATCHED TO WORK WITH LARAVEL 5.3.*

Laravel 5 is finally supported!

**Laravel 5.1.x to 5.2.x: ~3.*, (*5)

**Laravel 5.1.x: ~2.*, (*6)

**Laravel 4.2.x: ~1.* (Recently updated version for Laravel 4. Please note that there are namespace changes here), (*7)

**Laravel 4.2.x: 0.* (The version that most of you are using), (*8)

Quick start

Laravel 5.1.x to 5.2.x

Run composer require chrisbjr/api-guard 3.1.*, (*9)

In your config/app.php add Chrisbjr\ApiGuard\Providers\ApiGuardServiceProvider to the end of the providers array, (*10)

'providers' => array(

    ...
    Chrisbjr\ApiGuard\Providers\ApiGuardServiceProvider::class,
),

Add the ApiGuardAuth facade to the end of the aliases array as well, (*11)

'aliases' => array(

    ...
    'ApiGuardAuth' => \Chrisbjr\ApiGuard\Facades\ApiGuardAuth::class,
),

Add the following middleware inside your app/Http/Kernel.php file:, (*12)

protected $routeMiddleware = [
    ...
    'apiguard' => \Chrisbjr\ApiGuard\Http\Middleware\ApiGuard::class,
];

Now publish the migration and configuration files for api-guard:, (*13)

$ php artisan vendor:publish --provider="Chrisbjr\ApiGuard\Providers\ApiGuardServiceProvider"

Then run the migration:, (*14)

$ php artisan migrate

It will setup two tables - api_keys and api_logs., (*15)

Laravel 5.0.x to 5.1.x (old users)

Note: Documentation for use with Laravel 5.0.x and 5.1.x differs from Laravel 5.2.x. Please refer to the README here., (*16)

Laravel 4.2.x

Note: Documentation for use with Laravel 4.2.x differs from Laravel 5.0.x. Please refer to the README here. If you are using version 0.* you can find the README here, (*17)

Generating your first API key

Once you're done with the required setup, you can now generate your first API key., (*18)

Run the following command to generate an API key:, (*19)

php artisan api-key:generate, (*20)

Generally, you will want to generate API keys for each user in your application. The api_keys table has a user_id field which you can populate for your users., (*21)

To generate an API key that is linked to a user, you can do the following:, (*22)

php artisan api-key:generate --user-id=1, (*23)

To generate an API key from within your application, you can use the following method in the ApiKey model:, (*24)

$apiKey = Chrisbjr\ApiGuard\Models\ApiKey::make()

Usage

Basic usage of ApiGuard is to create a controller and extend that class to use the ApiGuardController., (*25)

Note: The namespace of the ApiGuardController differs from previous versions., (*26)

<?php

use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;

class BooksController extends ApiGuardController
{

    public function all()
    {
        $books = Book::all();

        return $this->response->withCollection($books, new BookTransformer);
    }

    public function show($id)
    {
        try {

            $book = Book::findOrFail($id);

            return $this->response->withItem($book, new BookTransformer);

        } catch (ModelNotFoundException $e) {

            return $this->response->errorNotFound();

        }
    }

}

You should be able to use the api-response object by using $this->response. More examples can be found on the Github page: https://github.com/ellipsesynergie/api-response., (*27)

You can access the above controller by creating a basic route in your app/routes.php:, (*28)

Route::get('api/v1/books', 'BooksController@all');
Route::get('api/v1/books/{id}', 'BooksController@show');

You will need to use your API key and put it in the header to access it. By default, the header value is using the X-Authorization parameter. You can change this in the config file., (*29)

Try calling this route using curl, (*30)

curl --header "X-Authorization: 2ed9d72e5596800bf805ca1c735e446df72019ef" http://localhost:8000/api/v1/books

You should get the following response:, (*31)

{
    "data": {
        "id": 1,
        "title": "The Great Adventures of Chris",
        "created_at": {
            "date": "2014-03-25 18:54:18",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "updated_at": {
            "date": "2014-03-25 18:54:18",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "deleted_at": null
    }
}

API Options

There are various options that can be specified for each method in your controller. These options can be specified inside the $apiMethods variable. Examples can be found below., (*32)

Turning off API key authentication for a specific method

By default, all the methods in the ApiGuardController will be authenticated. To turn this off for a specific method, use the keyAuthentication option., (*33)

<?php

use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;

class BooksController extends ApiGuardController
{

    protected $apiMethods = [
        'show' => [
            'keyAuthentication' => false
        ],
    ];

    ...

}

This above example will turn off key authentication for the show method., (*34)

Specifying access levels for API methods

If you take a look at the api_keys table in your database, you will notice that there is a level field., (*35)

This will allow you to specify a level for your API key and if the method has a higher level than the API key, access will be restricted. Here is an example on how to set the level on a method:, (*36)

<?php

use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;

class BooksController extends ApiGuardController
{

    protected $apiMethods = [
        'show' => [
            'level' => 10
        ],
    ];

    ...

}

Now if your API key has a level of 9 or lower, then access to the show method will be restricted., (*37)

Limiting API key access rate

You can limit the rate at which an API key can have access to a particular method by using the limits.key option., (*38)

<?php

use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;

class BooksController extends ApiGuardController
{

    protected $apiMethods = [
        'show' => [
            'limits' => [
                'key' => [
                    'increment' => '1 hour',
                    'limit' => 100
                ]
            ]
        ],
    ];

    ...

}

The above example will limit the access to the show method of an API key to 100 requests for every hour., (*39)

Note: The increment option can be any value that is accepted by the strtotime() method., (*40)

Limiting access to a method

There is also an option to limit the request rate for a given method no matter what API key is used. For this, we use the limits.method option., (*41)

<?php

use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;

class BooksController extends ApiGuardController
{

    protected $apiMethods = [
        'show' => [
            'limits' => [
                'method' => [
                    'increment' => '1 day',
                    'limit' => 1000
                ]
            ]
        ],
    ];

    ...

}

The above example will limit the request rate to the show method to 1000 requests per day., (*42)

Note: The increment option can be any value that is accepted by the strtotime() method., (*43)

Logging at method level

You can set logging at method level by using the logged option., (*44)

<?php

use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;

class BooksController extends ApiGuardController
{

    protected $apiMethods = [
        'show' => [
            'logged' => true
        ]
    ];

    ...

}

By default for all methods in api-guard, the option logged is set to true. Set it to false to exclude that method for logging., (*45)

The Versions

17/02 2016

3.0.x-dev

3.0.9999999.9999999-dev https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

13/02 2016

v3.0.3

3.0.3.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

04/02 2016

v3.0.1

3.0.1.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

01/02 2016

v3.0.0

3.0.0.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

17/12 2015

1.1.x-dev

1.1.9999999.9999999-dev https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

06/08 2015

v2.3.0

2.3.0.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

25/06 2015

v2.2.3

2.2.3.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

16/06 2015

v2.2.2

2.2.2.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

27/05 2015

v1.1.3

1.1.3.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

27/05 2015

v1.1.2

1.1.2.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

03/05 2015

v1.1.1

1.1.1.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

03/05 2015

v2.2.1

2.2.1.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

02/05 2015

v1.1

1.1.0.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

23/04 2015

v2.2.0

2.2.0.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

17/04 2015

v2.1.1

2.1.1.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

08/04 2015

v2.1.0

2.1.0.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

07/04 2015

v2.0.1

2.0.1.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

27/03 2015

v2.0.0

2.0.0.0 https://github.com/chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

27/03 2015

v1.0

1.0.0.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

10/01 2015

v0.7

0.7.0.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

18/11 2014

v0.6

0.6.0.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

18/10 2014

v0.5

0.5.0.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

30/09 2014

v0.4

0.4.0.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

11/07 2014

v0.3

0.3.0.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

27/06 2014

v0.2

0.2.0.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api json rest api keys api authentication

15/06 2014

v0.1

0.1.0.0 https://github.com/chrisbjr/ApiGuard

A simple way of authenticating your APIs with API keys using Laravel

  Sources   Download

The Requires

 

laravel api rest controller api keys