All Projects → OmarElGabry → Lumen Api Oauth

OmarElGabry / Lumen Api Oauth

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

Projects that are alternatives of or similar to Lumen Api Oauth

Restful Api With Laravel Definitive Guide
Repository with the base code for the course "RESTful API with Laravel - Definitive-Guide"
Stars: ✭ 156 (+113.7%)
Mutual labels:  restful-api, restful, oauth2
Api Restful Con Laravel Guia Definitiva
Repositorio para el código base del curso "API RESTful con Laravel - Guía Definitiva"
Stars: ✭ 95 (+30.14%)
Mutual labels:  restful-api, restful, oauth2
Ngx Restangular
Restangular for Angular 2 and higher versions
Stars: ✭ 787 (+978.08%)
Mutual labels:  restful-api, restful
Gen
Converts a database into gorm structs and RESTful api
Stars: ✭ 825 (+1030.14%)
Mutual labels:  restful-api, restful
X Restful Api Generator Koa
一个基于 Koa 的 RESTful API 服务脚手架。 A RESTful API generator for Koa
Stars: ✭ 18 (-75.34%)
Mutual labels:  restful-api, restful
Laravel Restful Api Starter
Build a RESTful API with Laravel and MongoDB
Stars: ✭ 66 (-9.59%)
Mutual labels:  restful-api, restful
Delphimvcframework
DMVCFramework (for short) is a popular and powerful framework for web solution in Delphi. Supports RESTful and JSON-RPC APIs development.
Stars: ✭ 761 (+942.47%)
Mutual labels:  restful-api, restful
Farwest
Framework for building RESTful HATEOAS-driven applications.
Stars: ✭ 18 (-75.34%)
Mutual labels:  restful-api, restful
Perfect Ssm
🍇更完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis)
Stars: ✭ 606 (+730.14%)
Mutual labels:  restful-api, restful
Thinkphp5 Restfulapi
restful-api风格接口 APP接口 APP接口权限 oauth2.0 接口版本管理 接口鉴权
Stars: ✭ 949 (+1200%)
Mutual labels:  restful-api, restful
Web Framework For Java
A seed project with spring boot for AngularJS, AngularJs Material, Thymeleaf, RESTful API, MySQL and admin panel based on AdminLTE.
Stars: ✭ 29 (-60.27%)
Mutual labels:  restful-api, oauth2
Restful Demo
A RESTful web service demo - building your own lightweight REST library in Swift
Stars: ✭ 37 (-49.32%)
Mutual labels:  restful-api, restful
Koa2 Api Scaffold
一个基于Koa2的轻量级RESTful API Server脚手架。
Stars: ✭ 694 (+850.68%)
Mutual labels:  restful-api, restful
Rest Api Design Guide
NBB's REST-ish API Design Guide
Stars: ✭ 643 (+780.82%)
Mutual labels:  restful-api, restful
Apidoc
RESTful API 文档生成工具,支持 Go、Java、Swift、JavaScript、Rust、PHP、Python、Typescript、Kotlin 和 Ruby 等大部分语言。
Stars: ✭ 785 (+975.34%)
Mutual labels:  restful-api, restful
Easyweb Jwt
基于 SpringBoot、jwt和JwtPermission实现的前后端分离开发框架,接口遵循RESTful风格。
Stars: ✭ 614 (+741.1%)
Mutual labels:  restful, oauth2
Nodepress
😎 RESTful API service for Blog/CMS, powered by @nestjs
Stars: ✭ 829 (+1035.62%)
Mutual labels:  restful-api, restful
Calm
It is always Calm before a Tornado!
Stars: ✭ 50 (-31.51%)
Mutual labels:  restful-api, restful
Restful Api Design References
RESTful API 设计参考文献列表,可帮助你更加彻底的了解REST风格的接口设计。
Stars: ✭ 4,830 (+6516.44%)
Mutual labels:  restful-api, restful
Lumen Passport
Making Laravel Passport work with Lumen
Stars: ✭ 585 (+701.37%)
Mutual labels:  lumen, oauth2

Lumen API OAuth

Lumen API OAuth

Build Status Scrutinizer Code Quality Code Climate Dependency Status

Latest Stable Version License

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.

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.

📣 A full tutorial on building a RESTful API with Lumen and OAuth2 can be found on Medium.

Index

Installation

Steps:

  1. Run Composer

    	composer install
    
  2. Laravel Homestead

    If you are using Laravel Homestead, then follow the Installation Guide.

  3. WAMP, LAMP, MAMP, XAMP Server

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

  4. Configure the.env file

    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.

  5. Finally, Run Migrations and Seed the database with fake data.

    	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.

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.

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.

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.

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

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

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?

Assign Middleware to controller

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

Order

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

Override isAuthorized() method

	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.

Routing

These are some of the routes defined in app/routes.php. You can test the API using Postman

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.

Contribute

Contribute by creating new issues, sending pull requests on Github or you can send an email at: [email protected]

Dependencies

License

Built under MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].