Để cho nhanh chóng, các bạn muốn biết Oauth2 để làm cái gì thì mời các bạn đọc bài Introduction to Oauth2 của anh Tùng D. Bài này mình sẽ giới thiệu cách sử dụng 1 package Oauth2 trong Laravel.
Giới thiệu package
Ở đây mình muốn giới thiệu package oauth2 server laravel mà mình đã dùng qua. Nó hỗ trợ hết các loại grant:
- Authorization code grant
- Client credentials grant
- Resource owner password credentials grant
- Refresh grant
- Ngoài ra còn có thể sử dụng grant tự định nghĩa của người dùng.
Cài đặt
Laravel 4
- Thêm dòng sau vào file
9'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider', 'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
"lucadegasperi/oauth2-server-laravel": "^3.0"
Chạy
0'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
Thêm vào
1 trong file'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
2 dòng sau'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
- Thêm
3'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
- Để tùy chỉnh package, chạy lệnh sau:
php artisan config:publish lucadegasperi/oauth2-server-laravel
sau đó vào
'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
4 để cấu hình theo ý muốn.- Thêm migration:
php artisan oauth2-server:migrations
- Tạo Controller:
php artisan oauth2-server:controller
Laravel 5
Tuơng tự với laravel 4, ta có:
- Thêm dòng sau vào file
9'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider', 'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
"lucadegasperi/oauth2-server-laravel": "5.0.*"
Chạy
0'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
Thêm vào
1 trong file'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
2 dòng sau'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider::class,
LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider::class,
- Thêm
3'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
'Authorizer' => LucaDegasperi\OAuth2Server\Facades\Authorizer::class,
- Để tùy chỉnh package, chạy lệnh sau:
\LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class,
- sau đó vào
4 để cấu hình theo ý muốn.'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
0- Để tùy chỉnh package, chạy lệnh sau:
'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
1sau đó vào
'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
4 để cấu hình theo ý muốn.- Thêm migration:
'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
2php artisan oauth2-server:migrations
php artisan oauth2-server:migrations
Tạo Controller:
Laravel 5
- Tuơng tự với laravel 4, ta có:
'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
3Thêm vào
'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
1 trong file 'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
8 dòng sauThêm vào
php artisan config:publish lucadegasperi/oauth2-server-laravel
0 trong file php artisan config:publish lucadegasperi/oauth2-server-laravel
1Sau đó thêm vào
php artisan config:publish lucadegasperi/oauth2-server-laravel
2sau đó vào
php artisan config:publish lucadegasperi/oauth2-server-laravel
3 để cấu hình theo ý muốn.Sử dụng
'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
4Để hiểu cách dùng, ta giả sử có 1 ứng dụng client muốn sử dụng 1 tài nguyên nào đó của 1 server chẳng hạn.
'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
5Server
'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
6Cấu hình lại oauth, giả sử ta dùng laravel 4, và dùng
php artisan config:publish lucadegasperi/oauth2-server-laravel
4 kết hợp với php artisan config:publish lucadegasperi/oauth2-server-laravel
5, chúng ta vào 'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade',
4 và cài đặt grant type như sau:Giải thích lại 1 chút, khi chúng ta dùng như trên, thì cách hoạt động của ứng dụng sẽ như thế này: client sẽ gửi yêu cầu tới server, sau đó server trả lại 1 mã, gọi là
php artisan config:publish lucadegasperi/oauth2-server-laravel
7, sau đó, client sẽ dùng mã này gửi lại tới server và server sẽ trả về php artisan config:publish lucadegasperi/oauth2-server-laravel
8. Và client sẽ dùng php artisan config:publish lucadegasperi/oauth2-server-laravel
8 để sử dụng tài nguyên server cho phép.'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
7Nhưng mà cái
php artisan config:publish lucadegasperi/oauth2-server-laravel
8 này cũng có hạn sử dụng, hết hạn thì hết dùng, nếu mà làm lại các bước trên cũng mệt, thế nên ta có php artisan config:publish lucadegasperi/oauth2-server-laravel
5 được gửi kèm php artisan config:publish lucadegasperi/oauth2-server-laravel
8 đầu tiên. Khi php artisan config:publish lucadegasperi/oauth2-server-laravel
8 này hết hạn thì sẽ dùng php artisan config:publish lucadegasperi/oauth2-server-laravel
5 đi kèm để được lấy 1 php artisan config:publish lucadegasperi/oauth2-server-laravel
8 mới và kèm theo là 1 php artisan config:publish lucadegasperi/oauth2-server-laravel
5 mới, và cái php artisan config:publish lucadegasperi/oauth2-server-laravel
5 cũng có thời hạn, nếu hết hạn cả 2 cái thì mời bạn làm lại từ đầu Trước hết là tạo dữ liệu cho client trên server này để mà còn sử dụng chứ. Khi bạn tạo migration thì sẽ có khá nhiều bảng được tạo ra như thế này:
Các bạn
php artisan oauth2-server:migrations
8 2 bảng php artisan oauth2-server:migrations
9 và php artisan oauth2-server:controller
0 là biết ngay cần phải thêm những dữ liệu nào, ở đây mình có viết sẵn 1 khung nhập dữ liệu client cho đỡ mỏi tay:Giả sử ta có 1 controller php artisan oauth2-server:controller
1 như thế này:
php artisan oauth2-server:controller
và có
php artisan oauth2-server:controller
2 như thế này:'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
8Gõ đường dẫn sau lên trình duyệt:
- Ta có:
- Tức là thế này, cái đường dẫn trên gọi route tới
3 dùng để yêu cầu server trả lại code cho client, với điều kiện là phải có sự đồng ý của người dùng. Form view của trang này là:php artisan oauth2-server:controller