Completed
Push — master ( e5a757...47ec18 )
by Robbie
14s
created

BasicRestfulAuthenticator::authenticate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 13
nc 4
nop 0
1
<?php
2
3
namespace SilverStripe\RestfulServer;
4
5
use SilverStripe\Security\Authenticator;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Security\Security;
9
10
/**
11
 * A simple authenticator for the Restful server.
12
 *
13
 * This allows users to be authenticated against that RestfulServer using their
14
 * login details, however they will be passed 'in the open' and will require the
15
 * application accessing the RestfulServer to store logins in plain text (or in
16
 * decrytable form)
17
 */
18
class BasicRestfulAuthenticator
19
{
20
    /**
21
     * The authenticate function
22
     *
23
     * Takes the basic auth details and attempts to log a user in from the DB
24
     *
25
     * @return Member|false The Member object, or false if no member
0 ignored issues
show
Bug introduced by
The type SilverStripe\RestfulServer\Member was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
     */
27
    public static function authenticate()
28
    {
29
        //if there is no username or password, fail
30
        if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
31
            return null;
32
        }
33
34
        // With a valid user and password, check the password is correct
35
        $data = [
36
            'Email' => $_SERVER['PHP_AUTH_USER'],
37
            'Password' => $_SERVER['PHP_AUTH_PW'],
38
        ];
39
        $request = Controller::curr()->getRequest();
40
        $authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::LOGIN);
41
        $member = null;
42
        foreach ($authenticators as $authenticator) {
43
            $member = $authenticator->authenticate($data, $request);
44
            if ($member) {
45
                break;
46
            }
47
        }
48
        return $member;
49
    }
50
}
51