1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Middleware; |
4
|
|
|
|
5
|
|
|
use App\Model\User; |
6
|
|
|
use Awurth\Slim\Helper\Exception\UnauthorizedException; |
7
|
|
|
use Cartalyst\Sentinel\Sentinel; |
8
|
|
|
use Chadicus\Slim\OAuth2\Http\RequestBridge; |
9
|
|
|
use OAuth2\Server; |
10
|
|
|
use Slim\Http\Request; |
11
|
|
|
use Slim\Http\Response; |
12
|
|
|
|
13
|
|
|
class Authorization implements MiddlewareInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $scopes; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Sentinel |
22
|
|
|
*/ |
23
|
|
|
protected $sentinel; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Server; |
27
|
|
|
*/ |
28
|
|
|
protected $server; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param Server $server |
34
|
|
|
* @param Sentinel $sentinel |
35
|
|
|
* @param array $scopes |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Server $server, Sentinel $sentinel, array $scopes = []) |
38
|
|
|
{ |
39
|
|
|
$this->server = $server; |
40
|
|
|
$this->sentinel = $sentinel; |
41
|
|
|
$this->scopes = $this->formatScopes($scopes); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function __invoke(Request $request, Response $response, callable $next) |
48
|
|
|
{ |
49
|
|
|
$oauth2Request = RequestBridge::toOAuth2($request); |
50
|
|
|
foreach ($this->scopes as $scope) { |
51
|
|
|
if ($this->server->verifyResourceRequest($oauth2Request, null, $scope)) { |
52
|
|
|
$token = $this->server->getResourceController()->getToken(); |
|
|
|
|
53
|
|
|
$user = User::find($token['user_id']); |
54
|
|
|
|
55
|
|
|
$this->sentinel->stateless($user); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $next($request, $response); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw new UnauthorizedException($request, $response); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns a callable function to be used as a authorization middleware with a specified scope. |
66
|
|
|
* |
67
|
|
|
* @param array $scopes Scopes require for authorization. |
68
|
|
|
* |
69
|
|
|
* @return Authorization |
70
|
|
|
*/ |
71
|
|
|
public function withRequiredScope(array $scopes) |
72
|
|
|
{ |
73
|
|
|
$clone = clone $this; |
74
|
|
|
$clone->scopes = $clone->formatScopes($scopes); |
75
|
|
|
|
76
|
|
|
return $clone; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Helper method to ensure given scopes are formatted properly. |
81
|
|
|
* |
82
|
|
|
* @param array $scopes Scopes required for authorization. |
83
|
|
|
* |
84
|
|
|
* @return array The formatted scopes array. |
85
|
|
|
*/ |
86
|
|
|
protected function formatScopes(array $scopes) |
87
|
|
|
{ |
88
|
|
|
if (empty($scopes)) { |
89
|
|
|
return [null]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
array_walk($scopes, function (&$scope) { |
93
|
|
|
if (is_array($scope)) { |
94
|
|
|
$scope = implode(' ', $scope); |
95
|
|
|
} |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
return $scopes; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|