Skip to content

Commit e96c092

Browse files
committed
Merge remote-tracking branch 'origin/rewrite'
Signed-off-by: Luca Degasperi <[email protected]> Conflicts: .gitignore .scrutinizer.yml .travis.yml README.md composer.json phpunit.xml.dist src/LucaDegasperi/OAuth2Server/Filters/CheckAuthorizationParamsFilter.php src/LucaDegasperi/OAuth2Server/Filters/OAuthFilter.php src/LucaDegasperi/OAuth2Server/Filters/OAuthOwnerFilter.php src/LucaDegasperi/OAuth2Server/OAuth2ServerServiceProvider.php src/LucaDegasperi/OAuth2Server/Proxies/AuthorizationServerProxy.php src/LucaDegasperi/OAuth2Server/Repositories/FluentClient.php src/LucaDegasperi/OAuth2Server/Repositories/FluentScope.php src/LucaDegasperi/OAuth2Server/Repositories/FluentSession.php tests/AuthorizationServerProxyTest.php tests/OAuthFilterTest.php
2 parents 84a87c5 + f39dd7d commit e96c092

File tree

98 files changed

+7047
-2370
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+7047
-2370
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/vendor
22
composer.phar
3-
composer.lock
43
.DS_Store
5-
.idea
6-
oauth2-server-laravel.*
4+
/.idea/

.scrutinizer.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ filter:
55
excluded_paths:
66
- 'vendor/*'
77
- 'tests/*'
8+
- 'spec/*'
89
- 'app/*'
910
- 'bin/*'
1011
- 'library/*'

.travis.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
language: php
22

3-
php:
4-
- 5.3
3+
php:
54
- 5.4
65
- 5.5
76
- 5.6
8-
- 5.6
97
- hhvm
108

119
matrix:
@@ -16,10 +14,13 @@ before_script:
1614
- curl -s http://getcomposer.org/installer | php
1715
- php composer.phar install --dev --no-interaction
1816
- wget https://scrutinizer-ci.com/ocular.phar
17+
- wget https://phar.phpunit.de/phpcov.phar
1918
script:
2019
- mkdir -p tmp
21-
- phpunit --configuration phpunit.xml.dist
20+
- vendor/bin/phpspec run -c phpspec.yml.dist
21+
- vendor/bin/phpunit --configuration phpunit.xml.dist
2222
after_script:
23+
- php phpcov.phar merge --clover tmp/coverage.clover tmp
2324
- php ocular.phar code-coverage:upload --format=php-clover tmp/coverage.clover
2425

2526
cache:

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MIT License
2+
3+
Copyright (C) 2014 Luca Degasperi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 8 additions & 255 deletions
Original file line numberDiff line numberDiff line change
@@ -1,280 +1,33 @@
11
# PHP OAuth 2.0 Server for Laravel
22

3-
[![Latest Stable Version](https://poser.pugx.org/lucadegasperi/oauth2-server-laravel/v/stable.png)](https://packagist.org/packages/lucadegasperi/oauth2-server-laravel) [![Build Status](https://travis-ci.org/lucadegasperi/oauth2-server-laravel.png?branch=master)](https://travis-ci.org/lucadegasperi/oauth2-server-laravel) [![Coverage Status](https://coveralls.io/repos/lucadegasperi/oauth2-server-laravel/badge.png)](https://coveralls.io/r/lucadegasperi/oauth2-server-laravel)
3+
[![Latest Stable Version](https://poser.pugx.org/lucadegasperi/oauth2-server-laravel/v/stable.png)](https://packagist.org/packages/lucadegasperi/oauth2-server-laravel) [![Build Status](https://scrutinizer-ci.com/g/lucadegasperi/oauth2-server-laravel/badges/build.png?b=rewrite)](https://scrutinizer-ci.com/g/lucadegasperi/oauth2-server-laravel/build-status/rewrite) [![Code Coverage](https://scrutinizer-ci.com/g/lucadegasperi/oauth2-server-laravel/badges/coverage.png?b=rewrite)](https://scrutinizer-ci.com/g/lucadegasperi/oauth2-server-laravel/?branch=rewrite) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/lucadegasperi/oauth2-server-laravel/badges/quality-score.png?b=rewrite)](https://scrutinizer-ci.com/g/lucadegasperi/oauth2-server-laravel/?branch=rewrite)
44

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.
66

77
The package assumes you have a good-enough knowledge of the principles behind the [OAuth 2.0 Specification](http://tools.ietf.org/html/rfc6749).
88

99
## Package Installation
1010

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:
2212

2313
```javascript
24-
"lucadegasperi/oauth2-server-laravel": "1.0.x"
14+
"lucadegasperi/oauth2-server-laravel": "3.0.x"
2515
```
2616

2717
Add this line of code to the ```providers``` array located in your ```app/config/app.php``` file:
2818
```php
19+
'LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider',
2920
'LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider',
3021
```
3122

3223
And this lines to the ```aliases``` array:
3324
```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',
16926
```
170-
An example request for an access token using this grant type might look like this.
17127

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
27629

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.
27831

27932
## Support
28033

behat.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
default:
2+
paths:
3+
features: tests/functional

0 commit comments

Comments
 (0)