Issues (199)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/controllers/TokenController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * DefaultController.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version 1.2.0
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 Philippe Gaultier <[email protected]>
31
 * @copyright 2010-2017 Philippe Gaultier
32
 * @license http://www.sweelix.net/license license
33
 * @version 1.2.0
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 1
                'Access-Control-Request-Method' => ['POST', 'OPTIONS'],
53
                'Access-Control-Allow-Credentials' => true,
54
                'Access-Control-Max-Age' => 3600,
55
            ], Module::getInstance()->cors);
56
57
            $behaviors['corsFilter'] = [
58
                'class' => Cors::class,
59 1
                'cors' => $corsFilter,
60
            ];
61
        }
62 8
        return $behaviors;
63 1
    }
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\JwtBearer');
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);
0 ignored issues
show
$response of type object<OAuth2\ResponseInterface> is not a sub-type of object<OAuth2\Response>. It seems like you assume a concrete implementation of the interface OAuth2\ResponseInterface to be always present.

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.

Loading history...
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 1.1.0
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