|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DefaultController.php |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6+ |
|
6
|
|
|
* |
|
7
|
|
|
* @author pgaultier |
|
8
|
|
|
* @copyright 2010-2016 Philippe Gaultier |
|
9
|
|
|
* @license http://www.sweelix.net/license license |
|
10
|
|
|
* @version 1.0.3 |
|
11
|
|
|
* @link http://www.sweelix.net |
|
12
|
|
|
* @package sweelix\oauth2\server\controllers |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace sweelix\oauth2\server\controllers; |
|
16
|
|
|
|
|
17
|
|
|
use OAuth2\Request as OAuth2Request; |
|
18
|
|
|
use OAuth2\Response as OAuth2Response; |
|
19
|
|
|
use sweelix\oauth2\server\Module; |
|
20
|
|
|
use yii\filters\Cors; |
|
21
|
|
|
use yii\helpers\ArrayHelper; |
|
22
|
|
|
use yii\rest\Controller; |
|
23
|
|
|
use yii\web\MethodNotAllowedHttpException; |
|
24
|
|
|
use yii\web\Response; |
|
25
|
|
|
use Yii; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Oauth2 main controller |
|
29
|
|
|
* |
|
30
|
|
|
* @author pgaultier |
|
31
|
|
|
* @copyright 2010-2016 Philippe Gaultier |
|
32
|
|
|
* @license http://www.sweelix.net/license license |
|
33
|
|
|
* @version 1.0.3 |
|
34
|
|
|
* @link http://www.sweelix.net |
|
35
|
|
|
* @package sweelix\oauth2\server\controllers |
|
36
|
|
|
* @since 1.0.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
class TokenController extends Controller |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
8 |
|
public function behaviors() |
|
45
|
|
|
{ |
|
46
|
8 |
|
$behaviors = parent::behaviors(); |
|
47
|
8 |
|
unset($behaviors['authenticator']); |
|
48
|
8 |
|
unset($behaviors['rateLimiter']); |
|
49
|
|
|
|
|
50
|
8 |
|
if (Module::getInstance()->cors !== false) { |
|
51
|
|
|
$corsFilter = ArrayHelper::merge([ |
|
52
|
|
|
'Access-Control-Request-Method' => ['POST', 'OPTIONS'], |
|
53
|
|
|
'Access-Control-Allow-Credentials' => true, |
|
54
|
|
|
'Access-Control-Max-Age' => 3600, |
|
55
|
|
|
], Module::getInstance()->cors); |
|
56
|
|
|
|
|
57
|
1 |
|
$behaviors['corsFilter'] = [ |
|
58
|
|
|
'class' => Cors::className(), |
|
59
|
|
|
'cors' => $corsFilter, |
|
60
|
|
|
]; |
|
61
|
1 |
|
} |
|
62
|
8 |
|
return $behaviors; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Send back an oauth token |
|
67
|
|
|
* @return Response|array |
|
68
|
|
|
* @since 1.0.0 |
|
69
|
|
|
*/ |
|
70
|
8 |
|
public function actionIndex() |
|
71
|
|
|
{ |
|
72
|
8 |
|
$oauthServer = Yii::createObject('OAuth2\Server'); |
|
73
|
|
|
/* @var \Oauth2\Server $oauthServer */ |
|
74
|
8 |
|
$grantType = Yii::$app->request->getBodyParam('grant_type'); |
|
75
|
8 |
|
$grantIsValid = false; |
|
76
|
|
|
switch ($grantType) { |
|
77
|
|
|
// Client Credentials |
|
78
|
8 |
|
case 'client_credentials': |
|
79
|
3 |
|
if (Module::getInstance()->allowClientCredentials === true) { |
|
80
|
2 |
|
$grantIsValid = true; |
|
81
|
2 |
|
$oauthGrantType = Yii::createObject('OAuth2\GrantType\ClientCredentials'); |
|
82
|
|
|
/* @var \OAuth2\GrantType\ClientCredentials $oauthGrantType */ |
|
83
|
2 |
|
$oauthServer->addGrantType($oauthGrantType); |
|
84
|
2 |
|
} |
|
85
|
3 |
|
break; |
|
86
|
|
|
// Resource Owner Password Credentials |
|
87
|
5 |
|
case 'password': |
|
88
|
4 |
|
if (Module::getInstance()->allowPassword === true) { |
|
89
|
4 |
|
$grantIsValid = true; |
|
90
|
4 |
|
$oauthGrantType = Yii::createObject('OAuth2\GrantType\UserCredentials'); |
|
91
|
|
|
/* @var \OAuth2\GrantType\UserCredentials $oauthGrantType */ |
|
92
|
4 |
|
$oauthServer->addGrantType($oauthGrantType); |
|
93
|
4 |
|
} |
|
94
|
4 |
|
break; |
|
95
|
|
|
// Refresh Token |
|
96
|
2 |
|
case 'refresh_token': |
|
97
|
1 |
|
$grantIsValid = true; |
|
98
|
1 |
|
$oauthGrantType = Yii::createObject('OAuth2\GrantType\RefreshToken'); |
|
99
|
|
|
/* @var \OAuth2\GrantType\RefreshToken $oauthGrantType */ |
|
100
|
1 |
|
$oauthServer->addGrantType($oauthGrantType); |
|
101
|
1 |
|
break; |
|
102
|
1 |
|
case 'authorization_code': |
|
103
|
1 |
|
if (Module::getInstance()->allowAuthorizationCode === true) { |
|
104
|
1 |
|
$grantIsValid = true; |
|
105
|
1 |
|
$oauthGrantType = Yii::createObject('OAuth2\GrantType\AuthorizationCode'); |
|
106
|
|
|
/* @var \OAuth2\GrantType\AuthorizationCode $oauthGrantType */ |
|
107
|
1 |
|
$oauthServer->addGrantType($oauthGrantType); |
|
108
|
1 |
|
} |
|
109
|
1 |
|
break; |
|
110
|
|
|
case 'urn:ietf:params:oauth:grant-type:jwt-bearer': |
|
111
|
|
|
$grantIsValid = true; |
|
112
|
|
|
$oauthGrantType = Yii::createObject('OAuth2\GrantType\RefreshToken'); |
|
113
|
|
|
/* @var \OAuth2\GrantType\JwtBearer $oauthGrantType */ |
|
114
|
|
|
$oauthServer->addGrantType($oauthGrantType); |
|
115
|
|
|
break; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
if ($grantIsValid === true) { |
|
119
|
7 |
|
$response = $oauthServer->handleTokenRequest(OAuth2Request::createFromGlobals()); |
|
120
|
7 |
|
$response = $this->convertResponse($response); |
|
|
|
|
|
|
121
|
7 |
|
} else { |
|
122
|
|
|
$response = [ |
|
123
|
2 |
|
'error' => 'invalid_grant', |
|
124
|
|
|
'error_description' => $grantType.' doesn\'t exist or is invalid for the client.' |
|
125
|
2 |
|
]; |
|
126
|
|
|
} |
|
127
|
8 |
|
return $response; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Basic options response for cors |
|
132
|
|
|
* @return null|\yii\web\Response |
|
133
|
|
|
* @since XXX |
|
134
|
|
|
* @throws MethodNotAllowedHttpException |
|
135
|
|
|
*/ |
|
136
|
|
|
public function actionOptions() |
|
137
|
|
|
{ |
|
138
|
|
|
if (Module::getInstance()->cors === false) { |
|
139
|
|
|
throw new MethodNotAllowedHttpException(); |
|
140
|
|
|
} |
|
141
|
|
|
return null; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* convert OAuth2 response to Yii2 response |
|
146
|
|
|
* @param OAuth2Response $oauthResponse |
|
147
|
|
|
* @return \yii\web\Response |
|
148
|
|
|
* @since 1.0.0 |
|
149
|
|
|
*/ |
|
150
|
7 |
|
protected function convertResponse(OAuth2Response $oauthResponse) |
|
151
|
|
|
{ |
|
152
|
|
|
//TODO: check if we should use acceptable contentType |
|
153
|
|
|
/* |
|
154
|
|
|
$acceptableContentTypes = Yii::$app->request->getAcceptableContentTypes(); |
|
155
|
|
|
foreach ($acceptableContentTypes as $acceptableContentType => $q) { |
|
156
|
|
|
$rawContentType = $acceptableContentType; |
|
157
|
|
|
if (($pos = strpos($rawContentType, ';')) !== false) { |
|
158
|
|
|
// e.g. application/json; charset=UTF-8 |
|
159
|
|
|
$contentType = substr($rawContentType, 0, $pos); |
|
160
|
|
|
} else { |
|
161
|
|
|
$contentType = $rawContentType; |
|
162
|
|
|
} |
|
163
|
|
|
break; |
|
164
|
|
|
} |
|
165
|
|
|
*/ |
|
166
|
7 |
|
$contentType = 'application/json'; |
|
167
|
7 |
|
$response = Yii::$app->response; |
|
168
|
7 |
|
$response->statusCode = $oauthResponse->getStatusCode(); |
|
169
|
7 |
|
$response->statusText = $oauthResponse->getStatusText(); |
|
170
|
7 |
|
if ($contentType === 'application/json') { |
|
171
|
7 |
|
$response->content = $oauthResponse->getResponseBody(); |
|
172
|
7 |
|
} else { |
|
173
|
|
|
$response->content = $oauthResponse->getResponseBody('xml'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
7 |
|
$headers = $oauthResponse->getHttpHeaders(); |
|
177
|
7 |
|
foreach($headers as $name => $value) |
|
178
|
|
|
{ |
|
179
|
7 |
|
$response->headers->set($name, $value); |
|
180
|
7 |
|
} |
|
181
|
7 |
|
return $response; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.