|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Firesphere\GraphQLJWT; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
6
|
|
|
use SilverStripe\ORM\ValidationException; |
|
7
|
|
|
use SilverStripe\Security\AuthenticationHandler; |
|
8
|
|
|
use SilverStripe\Security\Member; |
|
9
|
|
|
use SilverStripe\Security\Security; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class JWTAuthenticationHandler |
|
13
|
|
|
* |
|
14
|
|
|
* |
|
15
|
|
|
* @package Firesphere\GraphQLJWT |
|
16
|
|
|
*/ |
|
17
|
|
|
class JWTAuthenticationHandler implements AuthenticationHandler |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var JWTAuthenticator |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $authenticator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getAuthenticator() |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->authenticator; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param mixed $authenticator |
|
35
|
|
|
*/ |
|
36
|
|
|
public function setAuthenticator($authenticator) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->authenticator = $authenticator; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param HTTPRequest $request |
|
43
|
|
|
* @return null|Member |
|
44
|
|
|
* @throws \OutOfBoundsException |
|
45
|
|
|
* @throws \BadMethodCallException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function authenticateRequest(HTTPRequest $request) |
|
48
|
|
|
{ |
|
49
|
|
|
$matches = HeaderExtractor::getAuthorizationHeader($request); |
|
50
|
|
|
// Get the default user currently logged in via a different way, could be BasicAuth/normal login |
|
51
|
|
|
$member = Security::getCurrentUser(); |
|
52
|
|
|
|
|
53
|
|
|
if (!empty($matches[1])) { |
|
54
|
|
|
// Validate the token. This is critical for security |
|
55
|
|
|
$member = $this->authenticator->authenticate(['token' => $matches[1]], $request); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($member) { |
|
59
|
|
|
$this->logIn($member); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $member; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Authenticate on every run, based on the header, not relying on sessions or cookies |
|
67
|
|
|
* JSON Web Tokens are stateless |
|
68
|
|
|
* |
|
69
|
|
|
* @param Member $member |
|
70
|
|
|
* @param bool $persistent |
|
71
|
|
|
* @param HTTPRequest|null $request |
|
72
|
|
|
*/ |
|
73
|
|
|
public function logIn(Member $member, $persistent = false, HTTPRequest $request = null) |
|
74
|
|
|
{ |
|
75
|
|
|
Security::setCurrentUser($member); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param HTTPRequest|null $request |
|
80
|
|
|
* @throws ValidationException |
|
81
|
|
|
*/ |
|
82
|
|
|
public function logOut(HTTPRequest $request = null) |
|
83
|
|
|
{ |
|
84
|
|
|
// A token can actually not be invalidated, but let's invalidate it's unique ID |
|
85
|
|
|
// A member actually can be null though! |
|
86
|
|
|
if ($request !== null) { // If we don't have a request, we're most probably in test mode |
|
87
|
|
|
$member = Security::getCurrentUser(); |
|
88
|
|
|
if ($member) { |
|
89
|
|
|
// Set the unique ID to 0, as it can't be nullified due to indexes. |
|
90
|
|
|
$member->JWTUniqueID = 0; |
|
91
|
|
|
$member->write(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
// Empty the current user and pray to god it's not valid anywhere else anymore :) |
|
95
|
|
|
Security::setCurrentUser(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|