1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\GraphQLJWT; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
|
|
|
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
use SilverStripe\GraphQL\OperationResolver; |
9
|
|
|
use SilverStripe\GraphQL\QueryCreator; |
10
|
|
|
use SilverStripe\ORM\ValidationResult; |
11
|
|
|
|
12
|
|
|
class ValidateTokenQueryCreator extends QueryCreator implements OperationResolver |
13
|
|
|
{ |
14
|
|
|
public function attributes() |
15
|
|
|
{ |
16
|
|
|
return [ |
17
|
|
|
'name' => 'validateToken', |
18
|
|
|
'description' => 'Validates a given token from the Bearer header' |
19
|
|
|
]; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function args() |
23
|
|
|
{ |
24
|
|
|
return []; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function type() |
28
|
|
|
{ |
29
|
|
|
return $this->manager->getType('ValidateToken'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param mixed $object |
34
|
|
|
* @param array $args |
35
|
|
|
* @param mixed $context |
36
|
|
|
* @param ResolveInfo $info |
37
|
|
|
* @return array |
38
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
39
|
|
|
* @throws \OutOfBoundsException |
40
|
|
|
* @throws \BadMethodCallException |
41
|
|
|
*/ |
42
|
|
|
public function resolve($object, array $args, $context, ResolveInfo $info) |
43
|
|
|
{ |
44
|
|
|
/** @var JWTAuthenticator $authenticator */ |
45
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
46
|
|
|
$msg = []; |
47
|
|
|
$request = Controller::curr()->getRequest(); |
48
|
|
|
$matches = HeaderExtractor::getAuthorizationHeader($request); |
49
|
|
|
$result = new ValidationResult(); |
50
|
|
|
$code = 401; |
51
|
|
|
|
52
|
|
|
if (!empty($matches[1])) { |
53
|
|
|
$authenticator->authenticate(['token' => $matches[1]], $request, $result); |
54
|
|
|
if ($result->isValid()) { |
55
|
|
|
$code = 200; |
56
|
|
|
} |
57
|
|
|
} else { |
58
|
|
|
$result->addError('No Bearer token found'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($result->getMessages() as $message) { |
62
|
|
|
if (strpos($message['message'], 'Token is expired') === 0) { |
63
|
|
|
// An expired token is code 426 `Update required` |
64
|
|
|
$code = 426; |
65
|
|
|
} |
66
|
|
|
$msg[] = $message['message']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return ['Valid' => $result->isValid(), 'Message' => implode('; ', $msg), 'Code' => $code]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths