|
1 | 1 | # PHP OAuth 2.0 Server for Laravel
|
2 | 2 |
|
3 |
| -[](https://packagist.org/packages/lucadegasperi/oauth2-server-laravel) [](https://travis-ci.org/lucadegasperi/oauth2-server-laravel) [](https://coveralls.io/r/lucadegasperi/oauth2-server-laravel) |
| 3 | +[](https://packagist.org/packages/lucadegasperi/oauth2-server-laravel) [](https://scrutinizer-ci.com/g/lucadegasperi/oauth2-server-laravel/build-status/rewrite) [](https://scrutinizer-ci.com/g/lucadegasperi/oauth2-server-laravel/?branch=rewrite) [](https://scrutinizer-ci.com/g/lucadegasperi/oauth2-server-laravel/?branch=rewrite) |
4 | 4 |
|
5 |
| -A wrapper package for the standards compliant OAuth 2.0 authorization server and resource server written in PHP by the [League of Extraordinary Packages](http://www.thephpleague.com). |
| 5 | +[OAuth 2.0](http://tools.ietf.org/wg/oauth/draft-ietf-oauth-v2/) authorization server and resource server for the Laravel framework. Standard compliant thanks to the amazing work by [The League of Extraordinary Packages](http://www.thephpleague.com) OAuth 2.0 authorization server and resource server. |
6 | 6 |
|
7 | 7 | The package assumes you have a good-enough knowledge of the principles behind the [OAuth 2.0 Specification](http://tools.ietf.org/html/rfc6749).
|
8 | 8 |
|
9 | 9 | ## Package Installation
|
10 | 10 |
|
11 |
| -### With Laravel Package Installer |
12 |
| - |
13 |
| -The easiest way to install this package is via [Laravel Package Installer](https://github.com/rtablada/package-installer), this will set all the service providers and aliases for you. Run this artisan command to install the package: |
14 |
| - |
15 |
| -``` |
16 |
| -php artisan package:install lucadegasperi/oauth2-server-laravel |
17 |
| -``` |
18 |
| - |
19 |
| -### Manual Install |
20 |
| - |
21 |
| -alternatively, you can manually install the package via composer. add the following line to your composer.json file: |
| 11 | +You can install the package via composer. add the following line to your composer.json file: |
22 | 12 |
|
23 | 13 | ```javascript
|
24 |
| -"lucadegasperi/oauth2-server-laravel": "1.0.x" |
| 14 | +"lucadegasperi/oauth2-server-laravel": "3.0.x" |
25 | 15 | ```
|
26 | 16 |
|
27 | 17 | Add this line of code to the ```providers``` array located in your ```app/config/app.php``` file:
|
28 | 18 | ```php
|
| 19 | +'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider', |
29 | 20 | 'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
|
30 | 21 | ```
|
31 | 22 |
|
32 | 23 | And this lines to the ```aliases``` array:
|
33 | 24 | ```php
|
34 |
| -'AuthorizationServer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizationServerFacade', |
35 |
| -'ResourceServer' => 'LucaDegasperi\OAuth2Server\Facades\ResourceServerFacade', |
36 |
| -``` |
37 |
| - |
38 |
| -### Configuration |
39 |
| - |
40 |
| -In order to use the OAuth2 server publish its configuration first |
41 |
| - |
42 |
| -``` |
43 |
| -php artisan config:publish lucadegasperi/oauth2-server-laravel |
44 |
| -``` |
45 |
| - |
46 |
| -Afterwards edit the file ```app/config/packages/lucadegasperi/oauth2-server-laravel/oauth2.php``` to suit your needs. |
47 |
| - |
48 |
| -### Migrations |
49 |
| - |
50 |
| -This package comes with all the migrations you need to run a full featured oauth2 server. Run: |
51 |
| - |
52 |
| -``` |
53 |
| -php artisan migrate --package="lucadegasperi/oauth2-server-laravel" |
54 |
| -``` |
55 |
| - |
56 |
| -## Issuing an access token |
57 |
| - |
58 |
| -You can use different grant types to issue an access token depending on which suits your use cases. A detailed description of the different grant types can be found [here](https://github.com/php-loep/oauth2-server/wiki/Which-OAuth-2.0-grant-should-I-use%3F) |
59 |
| - |
60 |
| -A client should make a ```POST``` request with the appropriate parameters (depending on the grant type used) to an access token endpoint like the one defined here. This package will take care of all the fuss for you. |
61 |
| - |
62 |
| -```php |
63 |
| -Route::post('oauth/access_token', function() |
64 |
| -{ |
65 |
| - return AuthorizationServer::performAccessTokenFlow(); |
66 |
| -}); |
67 |
| -``` |
68 |
| - |
69 |
| -### Authorization Code flow |
70 |
| - |
71 |
| -The most common grant type is the ```authorization_code```. It's also the longest to setup, but don't worry, it will be easy. |
72 |
| - |
73 |
| -First the client must obtain the authorization (not an access token) from the resource owner to access the resources on its behalf. The client application should redirect the user to the authorization page with the correct query string parameters, for example: |
74 |
| - |
75 |
| -``` |
76 |
| -https://www.example.com/oauth/authorize? |
77 |
| -client_id=the_client_id& |
78 |
| -redirect_uri=client_redirect_uri& |
79 |
| -response_type=code& |
80 |
| -scope=scope1,scope2& |
81 |
| -state=1234567890 |
82 |
| -``` |
83 |
| - |
84 |
| -```php |
85 |
| -Route::get('/oauth/authorize', array('before' => 'check-authorization-params|auth', function() |
86 |
| -{ |
87 |
| - // get the data from the check-authorization-params filter |
88 |
| - $params = Session::get('authorize-params'); |
89 |
| - |
90 |
| - // get the user id |
91 |
| - $params['user_id'] = Auth::user()->id; |
92 |
| - |
93 |
| - // display the authorization form |
94 |
| - return View::make('authorization-form', array('params' => $params)); |
95 |
| -})); |
96 |
| -``` |
97 |
| - |
98 |
| -```php |
99 |
| -Route::post('/oauth/authorize', array('before' => 'check-authorization-params|auth|csrf', function() |
100 |
| -{ |
101 |
| - // get the data from the check-authorization-params filter |
102 |
| - $params = Session::get('authorize-params'); |
103 |
| - |
104 |
| - // get the user id |
105 |
| - $params['user_id'] = Auth::user()->id; |
106 |
| - |
107 |
| - // check if the user approved or denied the authorization request |
108 |
| - if (Input::get('approve') !== null) { |
109 |
| - |
110 |
| - $code = AuthorizationServer::newAuthorizeRequest('user', $params['user_id'], $params); |
111 |
| - |
112 |
| - Session::forget('authorize-params'); |
113 |
| - |
114 |
| - return Redirect::to(AuthorizationServer::makeRedirectWithCode($code, $params)); |
115 |
| - } |
116 |
| - |
117 |
| - if (Input::get('deny') !== null) { |
118 |
| - |
119 |
| - Session::forget('authorize-params'); |
120 |
| - |
121 |
| - return Redirect::to(AuthorizationServer::makeRedirectWithError($params)); |
122 |
| - } |
123 |
| -})); |
124 |
| -``` |
125 |
| - |
126 |
| -If the authorization process is successful the client will be redirected to its ```redirect_uri``` parameter with an authorization code in the query string like in the example below |
127 |
| - |
128 |
| -``` |
129 |
| -https://www.yourclient.com/redirect?code=XYZ123 |
130 |
| -``` |
131 |
| - |
132 |
| -The client can now use this code to make an access token request in the background. |
133 |
| - |
134 |
| -``` |
135 |
| -POST https://www.example.com/oauth/access_token? |
136 |
| -grant_type=authorization_code& |
137 |
| -client_id=the_client_id& |
138 |
| -client_secret=the_client_secret& |
139 |
| -redirect_uri=client_redirect_uri& |
140 |
| -code=the_authorization_code& |
141 |
| -scope=scope1,scope2& |
142 |
| -state=123456789 |
143 |
| -``` |
144 |
| - |
145 |
| -### Password flow |
146 |
| - |
147 |
| -This grant type is the easiest to use and is ideal for highly trusted clients. To enable this grant type add the code below to the ```grant_types``` array located at ```app/config/packages/lucadegasperi/oauth2-server-laravel/oauth2.php``` |
148 |
| - |
149 |
| -```php |
150 |
| -'password' => array( |
151 |
| - 'class' => 'League\OAuth2\Server\Grant\Password', |
152 |
| - 'access_token_ttl' => 604800, |
153 |
| - 'callback' => function($username, $password){ |
154 |
| - |
155 |
| - $credentials = array( |
156 |
| - 'email' => $username, |
157 |
| - 'password' => $password, |
158 |
| - ); |
159 |
| - |
160 |
| - $valid = Auth::validate($credentials); |
161 |
| - |
162 |
| - if (!$valid) { |
163 |
| - return false; |
164 |
| - } |
165 |
| - |
166 |
| - return Auth::getProvider()->retrieveByCredentials($credentials)->id; |
167 |
| - } |
168 |
| -), |
| 25 | +'Authorizer' => 'LucaDegasperi\OAuth2Server\Facades\AuthorizerFacade', |
169 | 26 | ```
|
170 |
| -An example request for an access token using this grant type might look like this. |
171 | 27 |
|
172 |
| -``` |
173 |
| -POST https://www.example.com/oauth/access_token? |
174 |
| -grant_type=password& |
175 |
| -client_id=the_client_id& |
176 |
| -client_secret=the_client_secret& |
177 |
| -username=the_username& |
178 |
| -password=the_password& |
179 |
| -scope=scope1,scope2& |
180 |
| -state=123456789 |
181 |
| -``` |
182 |
| - |
183 |
| -### Client Credentials flow |
184 |
| - |
185 |
| -Sometimes the client and the resource owner are the same thing. This grant types allows for client to access your API on their own behalf. To enable this grant type add the code below to the ```grant_types``` array located at ```app/config/packages/lucadegasperi/oauth2-server-laravel/oauth2.php``` |
186 |
| - |
187 |
| -```php |
188 |
| -'client_credentials' => array( |
189 |
| - 'class' => 'League\OAuth2\Server\Grant\ClientCredentials', |
190 |
| - 'access_token_ttl' => 3600, |
191 |
| -), |
192 |
| -``` |
193 |
| - |
194 |
| -An example request for an access token using this grant type might look like this. |
195 |
| - |
196 |
| -``` |
197 |
| -POST https://www.example.com/oauth/access_token? |
198 |
| -grant_type=client_credentials& |
199 |
| -client_id=the_client_id& |
200 |
| -client_secret=the_client_secret& |
201 |
| -scope=scope1,scope2& |
202 |
| -state=123456789 |
203 |
| -``` |
204 |
| - |
205 |
| -### Refresh token flow |
206 |
| - |
207 |
| -Access tokens do expire but by using the refresh token flow you can exchange a refresh token for an access token. |
208 |
| -When this grant type is enabled, every access token request will also issue a refresh token you can use to get a new access token when the current one expires. Configure this grant type in the ```grant_types``` array located at ```app/config/packages/lucadegasperi/oauth2-server-laravel/oauth2.php``` like this: |
209 |
| - |
210 |
| -```php |
211 |
| -'refresh_token' => array( |
212 |
| - 'class' => 'League\OAuth2\Server\Grant\RefreshToken', |
213 |
| - 'access_token_ttl' => 3600, |
214 |
| - 'refresh_token_ttl' => 604800, |
215 |
| - 'rotate_refresh_tokens' => false, |
216 |
| -), |
217 |
| -``` |
218 |
| - |
219 |
| -An example request for an access token using this grant type might look like this. |
220 |
| - |
221 |
| -``` |
222 |
| -POST https://www.example.com/oauth/access_token? |
223 |
| -grant_type=refresh_token& |
224 |
| -refresh_token=the_refresh_token& |
225 |
| -client_id=the_client_id& |
226 |
| -client_secret=the_client_secret& |
227 |
| -state=123456789 |
228 |
| -``` |
229 |
| - |
230 |
| - |
231 |
| -## Securing the API endpoints |
232 |
| - |
233 |
| -You can protect your laravel routes with oauth by applying the ```oauth``` before filter to them like in the example shown below |
234 |
| - |
235 |
| -```php |
236 |
| -Route::get('secure-route', array('before' => 'oauth', function(){ |
237 |
| - return "oauth secured route"; |
238 |
| -})); |
239 |
| -``` |
240 |
| - |
241 |
| -Additionaly you can provide the allowed scopes to the ```oauth``` before filter by passing them in the filter name. |
242 |
| - |
243 |
| -```php |
244 |
| -Route::get('secure-route', array('before' => 'oauth:scope1,scope2', function(){ |
245 |
| - return "oauth secured route"; |
246 |
| -})); |
247 |
| -``` |
248 |
| - |
249 |
| -An interesting addition is the possibility to limit an endpoint to a specific owner type when using the client credentials grant type. It can be achieved by adding the ```oauth-owner``` before filter to your route. |
250 |
| - |
251 |
| -```php |
252 |
| -Route::get('secure-route', array('before' => 'oauth:scope1,scope2|oauth-owner:client', function(){ |
253 |
| - return "oauth secured route for clients only"; |
254 |
| -})); |
255 |
| -``` |
256 |
| - |
257 |
| -## Accessing the API |
258 |
| - |
259 |
| -To access any api route, after issuing a token you must pass it as a parameter to the api call: |
260 |
| - |
261 |
| -```http://www.example.com/secure-route?access_token= 'valid_token'``` |
262 |
| - |
263 |
| -## Getting the token owner ID and type |
264 |
| - |
265 |
| -When accessing your API with an access token, you might want to know who's the owner of that token. The server makes it trivial. |
266 |
| - |
267 |
| -```php |
268 |
| -$ownerId = ResourceServer::getOwnerId(); |
269 |
| -``` |
270 |
| - |
271 |
| -When using the ```client_credentials``` grant type you might have users mixed with clients, to distinguish them use the following method |
272 |
| - |
273 |
| -```php |
274 |
| -$ownerType = ResourceServer::getOwnerType(); |
275 |
| -``` |
| 28 | +## Documentation |
276 | 29 |
|
277 |
| -The aim of this package is to make working with oauth2 server stuff in Laravel a breeze. You can still access all the undelying power of the league/oauth2-server package via the ```ResourceServer``` and ```AuthorizationServer``` facades. |
| 30 | +This package features an [extensive wiki](https://github.com/lucadegasperi/oauth2-server-laravel/wiki) to help you getting started implementing an OAuth 2.0 Server in your Laravel app. |
278 | 31 |
|
279 | 32 | ## Support
|
280 | 33 |
|
|
0 commit comments