Issues (3)

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/Adapter/MongoDb.php (2 issues)

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
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Auth\Adapter;
22
23
use \Psr\Http\Message\ServerRequestInterface;
24
25
/**
26
 * MongoDB (new driver) authentication adapter.
27
 *
28
 * @copyright 2015-2018 LibreWorks contributors
29
 * @license   Apache-2.0
30
 */
31
class MongoDb extends AbstractAdapter
32
{
33
    /**
34
     * @var \MongoDB\Driver\Manager
35
     */
36
    protected $manager;
37
    /**
38
     * @var string
39
     */
40
    protected $collection;
41
    /**
42
     * @var \MongoDB\Driver\ReadPreference
43
     */
44
    protected $rp;
45
    /**
46
     * @var string The document field containing the username
47
     */
48
    protected $fieldUser;
49
    /**
50
     * @var string The document field containing the password
51
     */
52
    protected $fieldPass;
53
    /**
54
     * @var array Associative array to use to limit user accounts
55
     */
56
    protected $query = [];
57
    
58
    /**
59
     * Creates a new MongoDB authentication adapter.
60
     *
61
     * @param \MongoDB\Driver\Manager $manager The manager
62
     * @param string $collection The collection name (e.g. "mydb.emails")
63
     * @param string $fieldUser The document field containing the username
64
     * @param string $fieldPass The document field containing the hashed password
65
     * @param array $query Optional associative array to use to limit user accounts
66
     * @param \MongoDB\Driver\ReadPreference $rp Optional read preference
67
     */
68 5
    public function __construct(\MongoDB\Driver\Manager $manager, string $collection, string $fieldUser, string $fieldPass, array $query = [], \MongoDB\Driver\ReadPreference $rp = null)
69
    {
70 5
        $this->manager = $manager;
71 5
        $this->collection = $this->checkBlank($collection, "collection");
72 5
        $this->fieldUser = $this->checkBlank($fieldUser, "username");
73 5
        $this->fieldPass = $this->checkBlank($fieldPass, "password");
74 5
        $this->query = $query;
75 5
        $this->rp = $rp;
76 5
    }
77
    
78
    /**
79
     * Authenticates the current principal using the provided credentials.
80
     *
81
     * This method expects two request body values to be available. These are
82
     * `username` and `password`, as provided by the authenticating user.
83
     *
84
     * The principal details will include `ip` (remote IP address), `ua` (remote
85
     * User Agent), and `ip` (MongoDB document `_id` field for user record).
86
     *
87
     * @param ServerRequestInterface $request The Server Request message containing credentials
88
     * @return \Caridea\Auth\Principal An authenticated principal
89
     * @throws \Caridea\Auth\Exception\MissingCredentials If the username or password is empty
90
     * @throws \Caridea\Auth\Exception\UsernameNotFound if the provided username wasn't found
91
     * @throws \Caridea\Auth\Exception\UsernameAmbiguous if the provided username matches multiple accounts
92
     * @throws \Caridea\Auth\Exception\InvalidPassword if the provided password is invalid
93
     * @throws \Caridea\Auth\Exception\ConnectionFailed if a MongoDB error is encountered
94
     */
95 5
    public function login(ServerRequestInterface $request): \Caridea\Auth\Principal
96
    {
97 5
        $post = (array) $request->getParsedBody();
98 5
        $username = $this->ensure($post, 'username');
99
        try {
100 5
            $results = $this->getResults($username, $request);
101 4
            $doc = $this->fetchResult($results, $username);
102 2
            $this->verify($this->ensure($post, 'password'), $doc->{$this->fieldPass});
103 1
            return \Caridea\Auth\Principal::get(
104 1
                $username,
105 1
                $this->details($request, ['id' => (string) $doc->_id])
106
            );
107 4
        } catch (\MongoDB\Driver\Exception\Exception $e) {
0 ignored issues
show
The class MongoDB\Driver\Exception\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
108 1
            throw new \Caridea\Auth\Exception\ConnectionFailed($e);
109
        }
110
    }
111
    
112
    /**
113
     * Queries the MongoDB collection.
114
     *
115
     * @param string $username The username to use for parameter binding
116
     * @param ServerRequestInterface $request The Server Request message (to use for additional parameter binding)
117
     * @return \MongoDB\Driver\Cursor The results cursor
118
     */
119 5
    protected function getResults(string $username, ServerRequestInterface $request): \MongoDB\Driver\Cursor
0 ignored issues
show
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121 5
        $q = new \MongoDB\Driver\Query(
122 5
            array_merge($this->query, [($this->fieldUser) => $username]),
123 5
            ['projection' => [($this->fieldUser) => true, ($this->fieldPass) => true]]
124
        );
125 5
        return $this->manager->executeQuery($this->collection, $q, $this->rp);
126
    }
127
128
    /**
129
     * Fetches a single result from the Mongo Cursor.
130
     *
131
     * @param \MongoCursor $results The results
132
     * @param string $username The attempted username (for Exception purposes)
133
     * @return \stdClass A single MongoDB document
134
     * @throws \Caridea\Auth\Exception\UsernameAmbiguous If there is more than 1 result
135
     * @throws \Caridea\Auth\Exception\UsernameNotFound If there are 0 results
136
     */
137 4
    protected function fetchResult(\MongoDB\Driver\Cursor $results, string $username): \stdClass
138
    {
139 4
        $values = $results->toArray();
140 4
        if (count($values) > 1) {
141 1
            throw new \Caridea\Auth\Exception\UsernameAmbiguous($username);
142 3
        } elseif (count($values) == 0) {
143 1
            throw new \Caridea\Auth\Exception\UsernameNotFound($username);
144
        }
145 2
        return current($values);
146
    }
147
}
148