oauth2 custom error message

oauth2 with laravel

oauth2 is a service that is mostly used for api authentication.Let know a short note about oauth2.OAuth 2 is an authorization framework that enables applications to obtain limited access to user accounts on an HTTP service, such as Facebook, GitHub, and DigitalOcean.Like we want to build an api based application,that time we have a think that how can we authenticate our customer to our application with a very secure layer.For that oauth2 is a great solution.As a fan of laravel here i show how to we use oauth2 in our most recent laravel 5.2 application.For that first need to add oauth2 package in our project’s composer.json

Require oauth2 package in composer.json

"lucadegasperi/oauth2-server-laravel": "5.1.*"

Here is the packgist and github url for oauth2

After that we have to download it in our project directory.So run

composer update

and then it will download in vendor folder.

Then open app/config.php file and add those line in $providers array

LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider::class,
LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider::class,

and also keep this line in $aliases array

'Authorizer' => LucaDegasperi\OAuth2Server\Facades\Authorizer::class,

Now time do something in middleware,so open app/Http/Kernel.php and add the following line in $middleware array

\LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class,

and add those in $routemiddleware array


'oauth' => \LucaDegasperi\OAuth2Server\Middleware\OAuthMiddleware::class,
'oauth-user' => \LucaDegasperi\OAuth2Server\Middleware\OAuthUserOwnerMiddleware::class,
'oauth-client' => \LucaDegasperi\OAuth2Server\Middleware\OAuthClientOwnerMiddleware::class,
'check-authorization-params' => \LucaDegasperi\OAuth2Server\Middleware\CheckAuthCodeRequestMiddleware::class,
'csrf' => App\Http\Middleware\VerifyCsrfToken::class,

After doing those your app/Http/Kernal.php file will like this

Then change .env file by giving database credential then run from your terminal

php artisan vendor:publish

and

php artisan migrate

and

php artisan make:auth

after that do a user registration form browser as usual.

Then open app\config\oauth2.php and change $grant_type array like below

Please follow that here we use a class PasswordGrantVerifier so create a class PasswordGrantVerifier and put those line

Then open app\http\Route.php file and put below line

Then insert a new record in oauth_client table for that execute below line in mysql

INSERT INTO `oauth_clients` (`id`, `secret`, `name`, `created_at`, `updated_at`) VALUES
(‘f3d259ddd3ed8ff3843839b’, ‘4c7f6f8fa93d59c45502c0ae8c4a95b’, ‘Main website’, ‘2015–05–12 21:00:00’, ‘0000–00–00 00:00:00’);

Now all are finished and time to check oauth2 authentication.In chrome a great extension postman has to check rest request.Here i gave an example to use that.

oatuh2 authentication from postman

oatuh2 authentication from postman

If we see the image we found that there has json index is access_token and a number in here.That’s out expected access_token number and the authentication.

Here i shared another image how i use this access token number to get logged in user information

oauth2 with access token

oauth2 with access token

I did a quick start repository,anyone can take help from here,if fall any problem Laravel oauth2 quick start So that’s all the oauth2 authentication with your laravel project.

For oauth2 custom exception messge go here
Happy coding :p