Passed
Pull Request — master (#24)
by Damian
01:51
created

AnonymousUserAuthenticator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 9 2
A supportedServices() 0 3 1
A checkPassword() 0 3 1
A authenticateMember() 0 18 3
1
<?php declare(strict_types=1);
2
3
namespace Firesphere\GraphQLJWT\Authentication;
4
5
use BadMethodCallException;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Injector\Injectable;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\ORM\ValidationException;
11
use SilverStripe\ORM\ValidationResult;
12
use SilverStripe\Security\Authenticator;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
15
16
class AnonymousUserAuthenticator extends MemberAuthenticator
17
{
18
    use Configurable;
19
    use Injectable;
20
21
    /**
22
     * Anonymous username
23
     *
24
     * @var string
25
     */
26
    private static $anonymous_username = 'anonymous';
0 ignored issues
show
introduced by
The private property $anonymous_username is not used, and could be removed.
Loading history...
27
28
    public function supportedServices(): int
29
    {
30
        return Authenticator::LOGIN | Authenticator::LOGOUT;
31
    }
32
33
    public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null): ?Member
34
    {
35
        // Only applies to request for anonymous user specifically
36
        $email = $data['Email'] ?? null;
37
        if ($email !== static::config()->get('anonymous_username')) {
38
            return null;
39
        }
40
41
        return parent::authenticate($data, $request, $result);
42
    }
43
44
    /**
45
     * Attempt to find and authenticate member if possible from the given data
46
     *
47
     * @skipUpgrade
48
     * @param array $data Form submitted data
49
     * @param ValidationResult $result
50
     * @param Member $member This third parameter is used in the CMSAuthenticator(s)
51
     * @return Member Found member, regardless of successful login
52
     * @throws ValidationException
53
     */
54
    protected function authenticateMember($data, ValidationResult &$result = null, Member $member = null): Member
55
    {
56
        // Get user, or create if not exists
57
        $username = static::config()->get('anonymous_username');
58
        $member = Injector::inst()->get(Member::class . '.anonymous', true, ['username' => $username]);
59
60
        // Validate this member is still allowed to login
61
        $result = $result ?: ValidationResult::create();
62
        $member->validateCanLogin($result);
63
64
        // Emit failure to member and form (if available)
65
        if ($result->isValid()) {
66
            $member->registerSuccessfulLogin();
67
        } else {
68
            $member->registerFailedLogin();
69
        }
70
71
        return $member;
72
    }
73
74
    public function checkPassword(Member $member, $password, ValidationResult &$result = null)
75
    {
76
        throw new BadMethodCallException("checkPassword not supported for anonymous users");
77
    }
78
}
79