1 | <?php |
||
35 | class TokenController extends Controller |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * @inheritdoc |
||
40 | */ |
||
41 | 8 | public function behaviors() |
|
48 | |||
49 | /** |
||
50 | * Send back an oauth token |
||
51 | * @return Response|array |
||
52 | * @since XXX |
||
53 | */ |
||
54 | 8 | public function actionIndex() |
|
55 | { |
||
56 | 8 | $oauthServer = Yii::createObject('OAuth2\Server'); |
|
57 | /* @var \Oauth2\Server $oauthServer */ |
||
58 | 8 | $grantType = Yii::$app->request->getBodyParam('grant_type'); |
|
59 | 8 | $grantIsValid = false; |
|
60 | switch ($grantType) { |
||
61 | // Client Credentials |
||
62 | 8 | case 'client_credentials': |
|
63 | 3 | if (Module::getInstance()->allowClientCredentials === true) { |
|
64 | 3 | $grantIsValid = true; |
|
65 | 2 | $oauthGrantType = Yii::createObject('OAuth2\GrantType\ClientCredentials'); |
|
66 | /* @var \OAuth2\GrantType\ClientCredentials $oauthGrantType */ |
||
67 | 2 | $oauthServer->addGrantType($oauthGrantType); |
|
68 | 2 | } |
|
69 | 3 | break; |
|
70 | // Resource Owner Password Credentials |
||
71 | 5 | case 'password': |
|
72 | 4 | if (Module::getInstance()->allowPassword === true) { |
|
73 | 4 | $grantIsValid = true; |
|
74 | 4 | $oauthGrantType = Yii::createObject('OAuth2\GrantType\UserCredentials'); |
|
75 | /* @var \OAuth2\GrantType\UserCredentials $oauthGrantType */ |
||
76 | 4 | $oauthServer->addGrantType($oauthGrantType); |
|
77 | 4 | } |
|
78 | 4 | break; |
|
79 | // Refresh Token |
||
80 | 2 | case 'refresh_token': |
|
81 | 1 | $grantIsValid = true; |
|
82 | 1 | $oauthGrantType = Yii::createObject('OAuth2\GrantType\RefreshToken'); |
|
83 | /* @var \OAuth2\GrantType\RefreshToken $oauthGrantType */ |
||
84 | 1 | $oauthServer->addGrantType($oauthGrantType); |
|
85 | 1 | break; |
|
86 | 1 | case 'authorization_code': |
|
87 | 1 | if (Module::getInstance()->allowAuthorizationCode === true) { |
|
88 | 1 | $grantIsValid = true; |
|
89 | 1 | $oauthGrantType = Yii::createObject('OAuth2\GrantType\AuthorizationCode'); |
|
90 | /* @var \OAuth2\GrantType\AuthorizationCode $oauthGrantType */ |
||
91 | 1 | $oauthServer->addGrantType($oauthGrantType); |
|
92 | 1 | } |
|
93 | 1 | break; |
|
94 | case 'urn:ietf:params:oauth:grant-type:jwt-bearer': |
||
95 | $grantIsValid = true; |
||
96 | $oauthGrantType = Yii::createObject('OAuth2\GrantType\RefreshToken'); |
||
97 | /* @var \OAuth2\GrantType\JwtBearer $oauthGrantType */ |
||
98 | $oauthServer->addGrantType($oauthGrantType); |
||
99 | break; |
||
100 | } |
||
101 | |||
102 | 8 | if ($grantIsValid === true) { |
|
103 | 7 | $response = $oauthServer->handleTokenRequest(OAuth2Request::createFromGlobals()); |
|
104 | 7 | $response = $this->convertResponse($response); |
|
|
|||
105 | 7 | } else { |
|
106 | $response = [ |
||
107 | 2 | 'error' => 'invalid_grant', |
|
108 | 'error_description' => $grantType.' doesn\'t exist or is invalid for the client.' |
||
109 | 2 | ]; |
|
110 | } |
||
111 | 8 | return $response; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * convert OAuth2 response to Yii2 response |
||
116 | * @param OAuth2Response $oauthResponse |
||
117 | * @return \yii\web\Response |
||
118 | * @since XXX |
||
119 | */ |
||
120 | 7 | protected function convertResponse(OAuth2Response $oauthResponse) |
|
153 | } |
||
154 |
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.