Passed
Push — master ( 42aa8f...8137e8 )
by Simon
02:14
created

ValidateTokenQueryCreator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 58
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attributes() 0 5 1
A type() 0 3 1
A args() 0 3 1
B resolve() 0 28 5
1
<?php
2
3
namespace Firesphere\GraphQLJWT;
4
5
use GraphQL\Type\Definition\ResolveInfo;
0 ignored issues
show
Bug introduced by
The type GraphQL\Type\Definition\ResolveInfo 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...
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