Issues (2)

Security Analysis    no request data  

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/Security/Provider/RedmineUserProvider.php (1 issue)

Labels
Severity

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
 * File part of the Redmine User Provider bundle
4
 *
5
 * @category  SymfonyBundle
6
 * @package   GMaissa.RedmineUserProviderBundle
7
 * @author    Guillaume Maïssa <[email protected]>
8
 * @copyright 2017 Guillaume Maïssa
9
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
10
 */
11
12
namespace GMaissa\RedmineUserProviderBundle\Security\Provider;
13
14
use GMaissa\RedmineUserProviderBundle\ApiClient\RedmineApiClientInterface;
15
use GMaissa\RedmineUserProviderBundle\Factory\UserFactoryInterface;
16
use GMaissa\RedmineUserProviderBundle\Model\RedmineUser;
17
use GMaissa\RedmineUserProviderBundle\Repository\UserRepositoryInterface;
18
use Psr\Log\LoggerInterface;
19
use Symfony\Component\Security\Core\Exception\AuthenticationException;
20
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
21
use Symfony\Component\Security\Core\User\UserInterface;
22
use Symfony\Component\Security\Core\User\UserProviderInterface;
23
24
/**
25
 * Redmine User provider class
26
 */
27
class RedmineUserProvider implements UserProviderInterface, CredentialsUserProviderInterface
28
{
29
    /**
30
     * @var UserFactoryInterface
31
     */
32
    protected $userFactory;
33
34
    /**
35
     * @var RedmineApiClientInterface
36
     */
37
    protected $apiClient;
38
39
    /**
40
     * @var UserRepositoryInterface
41
     */
42
    protected $userRepository = false;
43
44
    /**
45
     * @var array
46
     */
47
    protected $allowedDomains;
48
49
    /**
50
     * @var LoggerInterface
51
     */
52
    protected $logger;
53
54
    /**
55
     * Class constructor
56
     *
57
     * @param array           $allowedDomains
58
     * @param LoggerInterface $logger
59
     */
60 11
    public function __construct(array $allowedDomains, LoggerInterface $logger)
61
    {
62 11
        $this->allowedDomains = $allowedDomains;
63 11
        $this->logger         = $logger;
64 11
    }
65
66
    /**
67
     * Set User object factory
68
     *
69
     * @param UserFactoryInterface $userFactory
70
     */
71 11
    public function setUserFactory(UserFactoryInterface $userFactory)
72
    {
73 11
        $this->userFactory = $userFactory;
74 11
    }
75
76
    /**
77
     * Set the Redmine API client
78
     *
79
     * @param RedmineApiClientInterface $apiClient
80
     */
81 11
    public function setApiClient(RedmineApiClientInterface $apiClient)
82
    {
83 11
        $this->apiClient = $apiClient;
84 11
    }
85
86
    /**
87
     * Set the User repository
88
     *
89
     * @param UserRepositoryInterface $userRepository
90
     */
91 1
    public function setUserRepository(UserRepositoryInterface $userRepository)
92
    {
93 1
        $this->userRepository = $userRepository;
94 1
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 6
    public function loadUserByCredentials(string $username, string $password): UserInterface
100
    {
101
        try {
102 6
            $this->apiClient->connect($username, $password);
103 4
            $data = $this->apiClient->api('user')->getCurrentUser();
104
105 4
            if (!$data || !count($data)) {
106
                $this->logger->debug(sprintf('Invalid credentials %s / ****', $username));
107
                throw new AuthenticationException('Invalid credentials');
108
            }
109
110 4
            $this->checkUserDomain($data);
111
112 3
            $user = $this->getUser($data);
113 3
        } catch (AuthenticationException $e) {
114 1
            throw new AuthenticationException($e);
115 2
        } catch (\Exception $e) {
116 2
            $this->logger->debug(
117 2
                sprintf('Invalid credentials')
118
            );
119 2
            throw new AuthenticationException('Invalid credentials');
120
        }
121
122 3
        return $user;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 1
    public function loadUserByUsername($username)
129
    {
130 1
        throw new UsernameNotFoundException(
131 1
            sprintf('Username "%s" does not exist.', $username)
132
        );
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 1
    public function refreshUser(UserInterface $user)
139
    {
140 1
        $user = $this->userFactory->refreshUser($user);
141
142 1
        return $user;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 2
    public function supportsClass($class)
149
    {
150 2
        $obj = new \ReflectionClass($class);
151
152
        return (
153 2
            $obj->getName() == $this->userFactory->getUserClass() ||
0 ignored issues
show
Consider using $obj->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
154 2
            $obj->isSubclassOf($this->userFactory->getUserClass())
155
        );
156
    }
157
158
    /**
159
     * Control if the user's email address domain is allowed
160
     *
161
     * @param array $data
162
     */
163 4
    private function checkUserDomain(array $data)
164
    {
165 4
        if (count($this->allowedDomains)) {
166 4
            $userDomainAllowed = false;
167 4
            foreach ($this->allowedDomains as $allowedDomain) {
168 4
                if (strpos($data['user']['mail'], '@' . $allowedDomain)) {
169 3
                    $userDomainAllowed = true;
170
                }
171
            }
172 4
            if (!$userDomainAllowed) {
173 1
                $this->logger->debug(
174 1
                    sprintf('User email %s from unauthorized domain', $data['user']['mail'])
175
                );
176 1
                throw new AuthenticationException('User not from allowed domain');
177
            }
178
        }
179 3
    }
180
181
    /**
182
     * Retrieve the user or create it
183
     *
184
     * @param array $data
185
     *
186
     * @return RedmineUser
187
     */
188 3
    private function getUser(array $data): RedmineUser
189
    {
190 3
        $user = null;
191
        $userData = [
192 3
            'email'     => $data['user']['mail'],
193 3
            'username'  => $data['user']['login'],
194 3
            'password'  => $data['user']['api_key'],
195
            'enabled'   => true,
196 3
            'firstname' => $data['user']['firstname'],
197 3
            'lastname'  => $data['user']['lastname'],
198
        ];
199
200 3
        if ($this->userRepository) {
201 1
            $user = $this->userRepository->findOneBy(['username' => $data['user']['login']]);
202
        }
203
204 3
        if (is_null($user)) {
205 3
            $user = $this->userFactory->build($userData);
206
        }
207
208 3
        if ($this->userRepository) {
209 1
            $this->userRepository->save($user);
210
        }
211
212 3
        return $user;
213
    }
214
}
215