Issues (6)

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/Grant/PasswordCredentialsGrant.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
namespace Phisch\OAuth\Server\Grant;
4
5
use Phisch\OAuth\Server\Entity\ClientEntityInterface;
6
use Phisch\OAuth\Server\Entity\ScopeEntityInterface;
7
use Phisch\OAuth\Server\Entity\UserEntityInterface;
8
use Phisch\OAuth\Server\Exception\AuthorizationServerException;
9
use Phisch\OAuth\Server\Repository\AccessTokenRepositoryInterface;
10
use Phisch\OAuth\Server\Repository\ClientRepositoryInterface;
11
use Phisch\OAuth\Server\Repository\RefreshTokenRepositoryInterface;
12
use Phisch\OAuth\Server\Repository\ScopeRepositoryInterface;
13
use Phisch\OAuth\Server\Repository\UserRepositoryInterface;
14
use Phisch\OAuth\Server\Response\ResponseBuilder;
15
use Phisch\OAuth\Server\Token\TokenType;
16
use Symfony\Component\HttpFoundation\Request;
17
18 View Code Duplication
class PasswordCredentialsGrant implements Grant
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
{
20
21
    /**
22
     * @var ClientRepositoryInterface
23
     */
24
    private $clientRepository;
25
26
    /**
27
     * @var ScopeRepositoryInterface
28
     */
29
    private $scopeRepository;
30
31
    /**
32
     * @var UserRepositoryInterface
33
     */
34
    private $userRepository;
35
36
    /**
37
     * @var AccessTokenRepositoryInterface
38
     */
39
    private $accessTokenRepository;
40
41
    /**
42
     * @var RefreshTokenRepositoryInterface
43
     */
44
    private $refreshTokenRepository;
45
46
    /**
47
     * @var TokenType
48
     */
49
    private $token;
50
51
    /**
52
     * @param ClientRepositoryInterface $clientRepository
53
     * @param ScopeRepositoryInterface $scopeRepository
54
     * @param UserRepositoryInterface $userRepository
55
     * @param AccessTokenRepositoryInterface $accessTokenRepository
56
     * @param RefreshTokenRepositoryInterface $refreshTokenRepository
57
     * @param TokenType $token
58
     */
59
    public function __construct(
60
        ClientRepositoryInterface $clientRepository,
61
        ScopeRepositoryInterface $scopeRepository,
62
        UserRepositoryInterface $userRepository,
63
        AccessTokenRepositoryInterface $accessTokenRepository,
64
        RefreshTokenRepositoryInterface $refreshTokenRepository,
65
        TokenType $token
66
    ) {
67
        $this->clientRepository = $clientRepository;
68
        $this->scopeRepository = $scopeRepository;
69
        $this->userRepository = $userRepository;
70
        $this->accessTokenRepository = $accessTokenRepository;
71
        $this->refreshTokenRepository = $refreshTokenRepository;
72
        $this->token = $token;
73
    }
74
75
    /**
76
     * @param Request $request
77
     * @return bool
78
     */
79
    public function supports(Request $request)
80
    {
81
        return $request->get('grant_type') === $this->getIdentifier();
82
    }
83
84
    /**
85
     * @param Request $request
86
     * @param ResponseBuilder $responseBuilder
87
     * @return mixed
88
     */
89
    public function handle(Request $request, ResponseBuilder $responseBuilder)
90
    {
91
        $client = $this->validateClient($request);
92
        $scopes = $this->validateScopes($request);
93
        $user = $this->validateUser($request);
94
95
        // check if scopes fit for client
96
97
        $expiryDateTime = (new \DateTime())->add(new \DateInterval('PT1H'));
98
        $accessToken = $this->accessTokenRepository->createToken($client, $user, $scopes, $expiryDateTime);
99
100
        $expiryDateTime = (new \DateTime())->add(new \DateInterval('P1M'));
101
        $refreshToken = $this->refreshTokenRepository->createToken($accessToken, $expiryDateTime);
102
103
        return $responseBuilder->success($this->token, $accessToken, $refreshToken, $scopes);
104
    }
105
106
    /**
107
     * @param Request $request
108
     * @return ClientEntityInterface
109
     * @throws AuthorizationServerException
110
     */
111
    private function validateClient(Request $request)
112
    {
113
        $clientId = $request->get('client_id');
114
        $clientSecret = $request->get('client_secret');
115
116
        $client = $this->clientRepository->getClient($clientId);
117
118
        if ($client instanceof ClientEntityInterface === false) {
119
            throw new AuthorizationServerException('The requested client is unknown.', null, null, 'invalid_client');
120
        }
121
122
        if ($client->getSecret() !== $clientSecret) {
123
            throw new AuthorizationServerException(
124
                'The given client_secret does not match the client.',
125
                null,
126
                null,
127
                'invalid_client'
128
            );
129
        }
130
131
        if (!in_array($this->getIdentifier(), $client->getGrantTypes())) {
132
            throw new AuthorizationServerException(
133
                'The authenticated client is not authorized to use this authorization grant type.',
134
                null,
135
                null,
136
                'unauthorized_client'
137
            );
138
        }
139
140
        // TODO: check if validating the uri is necessary here, should be irrelevant for password credentials grant
141
        // TODO: might be necessary if this will be used for multiple grants though
142
143
        return $client;
144
    }
145
146
    /**
147
     * @param Request $request
148
     * @return ScopeEntityInterface[]
149
     */
150
    private function validateScopes(Request $request)
151
    {
152
        $scopeParameter = $request->get('scope');
153
        $scopeIdentifiers = explode(' ', $scopeParameter);
154
155
        $scopes = $this->scopeRepository->getScopes($scopeIdentifiers);
156
157
        return $scopes;
158
    }
159
160
    private function validateUser(Request $request)
161
    {
162
        $username = $request->get('username');
163
        $password = $request->get('password');
164
165
        $user = $this->userRepository->getUser($username, $password);
166
167
        if ($user instanceof UserEntityInterface === false) {
168
            // TODO: check how to correctly handle invalid user credentials, the rfc doesn't really guide that case
169
            throw new AuthorizationServerException(
170
                'The given user credentials are invalid.',
171
                null,
172
                null,
173
                'invalid_user_credentials'
174
            );
175
        }
176
177
        return $user;
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    private function getIdentifier()
184
    {
185
        return 'password';
186
    }
187
}
188