1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\OAuth2\Server\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use CodexShaper\OAuth2\Server\Entities\User as UserEntity; |
6
|
|
|
use CodexShaper\OAuth2\Server\Http\Requests\ServerRequest; |
7
|
|
|
use CodexShaper\OAuth2\Server\Http\Responses\ServerResponse; |
8
|
|
|
use CodexShaper\OAuth2\Server\Manager; |
9
|
|
|
use CodexShaper\OAuth2\Server\Model; |
10
|
|
|
use CodexShaper\OAuth2\Server\Models\User; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
13
|
|
|
|
14
|
|
|
class AuthorizationController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The server manager. |
18
|
|
|
* |
19
|
|
|
* @var \CodexShaper\OAuth2\Server\Manager |
20
|
|
|
*/ |
21
|
|
|
protected $manager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The authorization server. |
25
|
|
|
* |
26
|
|
|
* @var \League\OAuth2\Server\AuthorizationServer |
27
|
|
|
*/ |
28
|
|
|
protected $server; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The psr7 server request. |
32
|
|
|
* |
33
|
|
|
* @var \CodexShaper\OAuth2\Server\Http\Requests\ServerRequest |
34
|
|
|
*/ |
35
|
|
|
protected $request; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The psr7 server response. |
39
|
|
|
* |
40
|
|
|
* @var \CodexShaper\OAuth2\Server\Http\Responses\ServerResponse |
41
|
|
|
*/ |
42
|
|
|
protected $response; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new authorization controller instance. |
46
|
|
|
* |
47
|
|
|
* @return void |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
$this->manager = new Manager(); |
52
|
|
|
$this->server = $this->manager->makeAuthorizationServer(); |
53
|
|
|
$this->request = ServerRequest::getPsrServerRequest(); |
54
|
|
|
$this->response = ServerResponse::getPsrServerResponse(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Make authorization. |
59
|
|
|
* |
60
|
|
|
* @param \CodexShaper\OAuth2\Server\Models\User $user |
61
|
|
|
* |
62
|
|
|
* @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface|void |
63
|
|
|
*/ |
64
|
|
|
public function authorize($user) |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
|
68
|
|
|
// Validate the HTTP request and return an AuthorizationRequest object. |
69
|
|
|
$authRequest = $this->server->validateAuthorizationRequest($this->request); |
|
|
|
|
70
|
|
|
|
71
|
|
|
// Get all validate scopes from psr request |
72
|
|
|
$scopes = $this->filterScopes($authRequest); |
|
|
|
|
73
|
|
|
|
74
|
|
|
// Get token for current user and request client id |
75
|
|
|
$token = Model::findToken('clientModel', $authRequest, $user); |
76
|
|
|
|
77
|
|
|
if (($token) || Model::instance('clientModel')->isSkipsAuthorization()) { |
78
|
|
|
return $this->approve($authRequest, $user); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $authRequest; |
82
|
|
|
} catch (OAuthServerException $exception) { |
83
|
|
|
|
84
|
|
|
// All instances of OAuthServerException can be formatted into a HTTP response |
85
|
|
|
return $exception->generateHttpResponse($this->response); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Approve the authorization. |
91
|
|
|
* |
92
|
|
|
* @param \League\OAuth2\Server\RequestTypes\AuthorizationRequest $authRequest |
93
|
|
|
* @param \CodexShaper\OAuth2\Server\Models\User $user |
94
|
|
|
* |
95
|
|
|
* @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface |
96
|
|
|
*/ |
97
|
|
|
public function approve($authRequest, $user) |
98
|
|
|
{ |
99
|
|
|
// Once the user has logged in set the user on the AuthorizationRequest |
100
|
|
|
$authRequest->setUser(new UserEntity($user->getKey())); // an instance of UserEntityInterface |
101
|
|
|
|
102
|
|
|
// Once the user has approved or denied the client update the status |
103
|
|
|
// (true = approved, false = denied) |
104
|
|
|
$authRequest->setAuthorizationApproved(true); |
105
|
|
|
|
106
|
|
|
// Return the HTTP redirect response |
107
|
|
|
return $this->server->completeAuthorizationRequest($authRequest, $this->response); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Deny the authorization request. |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function deny() |
116
|
|
|
{ |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Filter all scopes. |
121
|
|
|
* |
122
|
|
|
* @param \League\OAuth2\Server\RequestTypes\AuthorizationRequest $authRequest |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function filterScopes($authRequest) |
127
|
|
|
{ |
128
|
|
|
return array_filter($authRequest->getScopes(), function ($scope) { |
129
|
|
|
if (Manager::isValidateScope($scope->getIdentifier())) { |
130
|
|
|
return $scope->getIdentifier(); |
131
|
|
|
} |
132
|
|
|
}); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.