2017 © Pedro Peláez
 

project lumen-api-oauth

A RESTful API based on Lumen micro-framework with OAuth2.

image

omarelgabry/lumen-api-oauth

A RESTful API based on Lumen micro-framework with OAuth2.

  • Wednesday, March 22, 2017
  • by OmarElGabry
  • Repository
  • 10 Watchers
  • 65 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 33 Forks
  • 3 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Lumen API OAuth , (*1)

Lumen API OAuth

Build Status Scrutinizer Code Quality Code Climate Dependency Status, (*2)

Latest Stable Version License, (*3)

A RESTful API based on Lumen micro-framework with OAuth2. Lumen API OAuth is a simple application, indented for small projects, helps to understand creating RESTful APIs with Lumen and OAuth2, know how to authenticate and authorize, and more., (*4)

The RESTful API for Posts and Comments, where Users can view, create, update, and delete. It provides authorization mechanism to authorize against access tokens using OAuth2, ownership, and non-admin Vs admin users., (*5)

:mega: A full tutorial on building a RESTful API with Lumen and OAuth2 can be found on Medium., (*6)

Index

Installation

Steps:, (*7)

  1. Run Composer, (*8)

        composer install
    
  2. Laravel Homestead, (*9)

    If you are using Laravel Homestead, then follow the Installation Guide., (*10)

  3. WAMP, LAMP, MAMP, XAMP Server, (*11)

    If you are using any of WAMP, LAMP, MAMP, XAMP Servers, then don't forget to create a database, probably a MySQL database., (*12)

  4. Configure the.env file, (*13)

    Rename .env.example file to .env, set your application key to a random string with 32 characters long, edit database name, database username, and database password if needed., (*14)

  5. Finally, Run Migrations and Seed the database with fake data., (*15)

        php artisan migrate --seed
    

Terminology

There are some terminologies that will be used on the meaning of the terms used by OAuth 2.0. If you need a refresher, then check this out., (*16)

Authorization

Authorization comes in two layers. The first layer authorize against the access token, and the second one is for checking against ownership, and non-admin Vs admin users., (*17)

By default, user can delete or update a post or a comment only if he is the owner. Admins are authorized to view, create, update or delete anything., (*18)

Access Tokens

The application implements Resource owner credentials grant, which essentially requires the client to submit 5 fields: username, password, client_id, client_secret, and grant_type., (*19)

The authorization server will then issue access tokens to the client after successfully authenticating the client credentials and presenting authorization grant(user credentials)., (*20)

In app/Http/routes.php, A route has been defined for requesting an access token., (*21)

Ownership, & non-Admin Vs Admin Users

Now, after validating the access token, we can extend the authorization layers and check if the current user is owner of the requested resource(i.e. post or comment), or is admin. So, How does it work?, (*22)

Assign Middleware to controller, (*23)

    public function __construct(){

        $this->middleware('oauth', ['except' => ['index', 'show']]);
        $this->middleware('authorize:' . __CLASS__, ['except' => ['index', 'show', 'store']]);
    }

Order, (*24)

Please note that the middlewares has to be applied in a certain order. The oauth has to be added before the authorize Middleware., (*25)

Override isAuthorized() method, (*26)

    public function isAuthorized(Request $request){

        $resource = "posts";
        $post     = Post::find($this->getArgs($request)["post_id"]);

        return $this->authorizeUser($request, $resource, $post);
    }

In app/Providers/AuthServiceProvider.php, Abilities are defined using Gate facade., (*27)

Routing

These are some of the routes defined in app/routes.php. You can test the API using Postman, (*28)

HTTP Method Path Action Fields
GET /users index
POST /oauth/access_token username, password, client_id, client_secret, and grant_type.
The username field is the email in Users table.
The password field is secret.
The client_id & client_secret fields are id0 & secret0, or id1 & secret1, ...etc respectively.
The grant_type field is password.
POST /posts store access_token, title, content
PUT /posts/{post_id} update access_token, title, content
DELETE /posts/{post_id} destroy access_token

Support

I've written this script in my free time during my studies. This is for free, unpaid. If you find it useful, please support the project by spreading the word., (*29)

Contribute

Contribute by creating new issues, sending pull requests on Github or you can send an email at: omar.elgabry.93@gmail.com, (*30)

Dependencies

License

Built under MIT license., (*31)

The Versions

22/03 2017

dev-master

9999999-dev https://github.com/OmarElGabry/lumen-api-oauth

A RESTful API based on Lumen micro-framework with OAuth2.

  Sources   Download

MIT

The Requires

 

The Development Requires

api authentication authorization lumen oauth rest

07/04 2016

v1.0

1.0.0.0 https://github.com/OmarElGabry/lumen-api-oauth

A RESTful API based on Lumen micro-framework with OAuth2.

  Sources   Download

MIT

The Requires

 

The Development Requires

api authentication authorization lumen oauth rest