Completed
Push — master ( dd6027...6d621d )
by Tobias
06:47
created

JWTAuthenticationHandler::logOut()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Firesphere\GraphQLJWT;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Security\AuthenticationHandler;
8
use SilverStripe\Security\IdentityStore;
9
use SilverStripe\Security\Member;
10
use SilverStripe\Security\Security;
11
12
/**
13
 * Class JWTAuthenticationHandler
14
 *
15
 *
16
 * @package Firesphere\GraphQLJWT
17
 */
18
class JWTAuthenticationHandler implements AuthenticationHandler
19
{
20
21
    /**
22
     * @var JWTAuthenticator
23
     */
24
    protected $authenticator;
25
26
    /**
27
     * @return mixed
28
     */
29
    public function getAuthenticator()
30
    {
31
        return $this->authenticator;
32
    }
33
34
    /**
35
     * @param mixed $authenticator
36
     */
37
    public function setAuthenticator($authenticator)
38
    {
39
        $this->authenticator = $authenticator;
40
    }
41
42
    /**
43
     * @param HTTPRequest $request
44
     * @return null|Member
45
     */
46
    public function authenticateRequest(HTTPRequest $request)
47
    {
48
        $authHeader = $request->getHeader('Authorization');
49
        $member = null;
50 View Code Duplication
        if ($authHeader && preg_match('/Bearer\s+(.*)$/i', $authHeader, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
            $member = $this->authenticator->authenticate(['token' => $matches[1]], $request);
52
        }
53
        if ($member) {
54
            $this->logIn($member);
55
        } else {
56
            // Get the default user currently logged in via a different way, could be BasicAuth/normal login
57
            $member = Security::getCurrentUser();
58
        }
59
60
        return $member;
61
    }
62
63
    /**
64
     * @param Member $member
65
     * @param bool $persistent
66
     * @param HTTPRequest|null $request
67
     */
68
    public function logIn(Member $member, $persistent = false, HTTPRequest $request = null)
69
    {
70
        Security::setCurrentUser($member);
71
    }
72
73
    /**
74
     * @param HTTPRequest|null $request
75
     */
76
    public function logOut(HTTPRequest $request = null)
77
    {
78
        // A token can actually not be invalidated, only blacklisted
79
        if ($request !== null) {
80
            $request->getSession()->clear('jwt');
81
        }
82
        Security::setCurrentUser(null);
83
    }
84
}
85