Skip to content

Commit a0d7a25

Browse files
committed
Merge pull request #502 from lucadegasperi/middleware
Added New Middleware
2 parents c638589 + 7eb866b commit a0d7a25

File tree

4 files changed

+140
-19
lines changed

4 files changed

+140
-19
lines changed

src/Middleware/OAuthOwnerMiddleware.php renamed to src/Middleware/OAuthClientOwnerMiddleware.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
use LucaDegasperi\OAuth2Server\Authorizer;
1717

1818
/**
19-
* This is the oauth owner middleware class.
19+
* This is the oauth client middleware class.
2020
*
21-
* @author Luca Degasperi <packages@lucadegasperi.com>
21+
* @author Vincent Klaiber <hello@vinkla.com>
2222
*/
23-
class OAuthOwnerMiddleware
23+
class OAuthClientOwnerMiddleware
2424
{
2525
/**
2626
* The Authorizer instance.
@@ -30,7 +30,7 @@ class OAuthOwnerMiddleware
3030
protected $authorizer;
3131

3232
/**
33-
* Create a new oauth owner middleware instance.
33+
* Create a new oauth client middleware instance.
3434
*
3535
* @param \LucaDegasperi\OAuth2Server\Authorizer $authorizer
3636
*/
@@ -44,21 +44,14 @@ public function __construct(Authorizer $authorizer)
4444
*
4545
* @param \Illuminate\Http\Request $request
4646
* @param \Closure $next
47-
* @param string|null $ownerTypesString
4847
*
4948
* @throws \League\OAuth2\Server\Exception\AccessDeniedException
5049
*
5150
* @return mixed
5251
*/
53-
public function handle($request, Closure $next, $ownerTypesString = null)
52+
public function handle($request, Closure $next)
5453
{
55-
$ownerTypes = [];
56-
57-
if (!is_null($ownerTypesString)) {
58-
$ownerTypes = explode('+', $ownerTypesString);
59-
}
60-
61-
if (!in_array($this->authorizer->getResourceOwnerType(), $ownerTypes)) {
54+
if ($this->authorizer->getResourceOwnerType() !== 'client') {
6255
throw new AccessDeniedException();
6356
}
6457

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of OAuth 2.0 Laravel.
5+
*
6+
* (c) Luca Degasperi <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace LucaDegasperi\OAuth2Server\Middleware;
13+
14+
use Closure;
15+
use League\OAuth2\Server\Exception\AccessDeniedException;
16+
use LucaDegasperi\OAuth2Server\Authorizer;
17+
18+
/**
19+
* This is the oauth user middleware class.
20+
*
21+
* @author Vincent Klaiber <[email protected]>
22+
*/
23+
class OAuthUserOwnerMiddleware
24+
{
25+
/**
26+
* The Authorizer instance.
27+
*
28+
* @var \LucaDegasperi\OAuth2Server\Authorizer
29+
*/
30+
protected $authorizer;
31+
32+
/**
33+
* Create a new oauth user middleware instance.
34+
*
35+
* @param \LucaDegasperi\OAuth2Server\Authorizer $authorizer
36+
*/
37+
public function __construct(Authorizer $authorizer)
38+
{
39+
$this->authorizer = $authorizer;
40+
}
41+
42+
/**
43+
* Handle an incoming request.
44+
*
45+
* @param \Illuminate\Http\Request $request
46+
* @param \Closure $next
47+
*
48+
* @throws \League\OAuth2\Server\Exception\AccessDeniedException
49+
*
50+
* @return mixed
51+
*/
52+
public function handle($request, Closure $next)
53+
{
54+
if ($this->authorizer->getResourceOwnerType() !== 'user') {
55+
throw new AccessDeniedException();
56+
}
57+
58+
return $next($request);
59+
}
60+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of OAuth 2.0 Laravel.
5+
*
6+
* (c) Luca Degasperi <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace unit\LucaDegasperi\OAuth2Server\Middleware;
13+
14+
use Illuminate\Http\Request;
15+
use League\OAuth2\Server\Exception\AccessDeniedException;
16+
use LucaDegasperi\OAuth2Server\Authorizer;
17+
use PhpSpec\ObjectBehavior;
18+
19+
/**
20+
* This is the oauth client middleware spec class.
21+
*
22+
* @author Vincent Klaiber <[email protected]>
23+
*/
24+
class OAuthClientOwnerMiddlewareSpec extends ObjectBehavior
25+
{
26+
private $next = null;
27+
28+
public function __construct()
29+
{
30+
$this->next = (function () {
31+
throw new MiddlewareException('Called execution of $next');
32+
});
33+
}
34+
35+
public function let(Authorizer $authorizer)
36+
{
37+
$this->beConstructedWith($authorizer);
38+
}
39+
40+
public function it_is_initializable()
41+
{
42+
$this->shouldHaveType('LucaDegasperi\OAuth2Server\Middleware\OAuthClientOwnerMiddleware');
43+
}
44+
45+
public function it_passes_if_resource_owners_are_allowed(Request $request, Authorizer $authorizer)
46+
{
47+
$authorizer->getResourceOwnerType()->willReturn('client')->shouldBeCalled();
48+
49+
$this->shouldThrow(new MiddlewareException('Called execution of $next'))
50+
->during('handle', [$request, $this->next]);
51+
}
52+
53+
public function it_blocks_if_resource_owners_are_not_allowed(Request $request, Authorizer $authorizer)
54+
{
55+
$authorizer->getResourceOwnerType()->willReturn('user')->shouldBeCalled();
56+
57+
$this->shouldThrow(new AccessDeniedException())
58+
->during('handle', [$request, $this->next]);
59+
60+
$this->shouldNotThrow(new MiddlewareException('Called execution of $next'))
61+
->during('handle', [$request, $this->next]);
62+
}
63+
}

tests/unit/LucaDegasperi/OAuth2Server/Middleware/OAuthOwnerMiddlewareSpec.php renamed to tests/unit/LucaDegasperi/OAuth2Server/Middleware/OAuthUserOwnerMiddlewareSpec.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
use LucaDegasperi\OAuth2Server\Authorizer;
1717
use PhpSpec\ObjectBehavior;
1818

19-
class OAuthOwnerMiddlewareSpec extends ObjectBehavior
19+
/**
20+
* This is the oauth user middleware spec class.
21+
*
22+
* @author Vincent Klaiber <[email protected]>
23+
*/
24+
class OAuthUserOwnerMiddlewareSpec extends ObjectBehavior
2025
{
2126
private $next = null;
2227

@@ -34,25 +39,25 @@ public function let(Authorizer $authorizer)
3439

3540
public function it_is_initializable()
3641
{
37-
$this->shouldHaveType('LucaDegasperi\OAuth2Server\Middleware\OAuthOwnerMiddleware');
42+
$this->shouldHaveType('LucaDegasperi\OAuth2Server\Middleware\OAuthUserOwnerMiddleware');
3843
}
3944

4045
public function it_passes_if_resource_owners_are_allowed(Request $request, Authorizer $authorizer)
4146
{
4247
$authorizer->getResourceOwnerType()->willReturn('user')->shouldBeCalled();
4348

4449
$this->shouldThrow(new MiddlewareException('Called execution of $next'))
45-
->during('handle', [$request, $this->next, 'user']);
50+
->during('handle', [$request, $this->next]);
4651
}
4752

4853
public function it_blocks_if_resource_owners_are_not_allowed(Request $request, Authorizer $authorizer)
4954
{
50-
$authorizer->getResourceOwnerType()->willReturn('user')->shouldBeCalled();
55+
$authorizer->getResourceOwnerType()->willReturn('client')->shouldBeCalled();
5156

5257
$this->shouldThrow(new AccessDeniedException())
53-
->during('handle', [$request, $this->next, 'client']);
58+
->during('handle', [$request, $this->next]);
5459

5560
$this->shouldNotThrow(new MiddlewareException('Called execution of $next'))
56-
->during('handle', [$request, $this->next, 'client']);
61+
->during('handle', [$request, $this->next]);
5762
}
5863
}

0 commit comments

Comments
 (0)