Passed
Push — master ( 291050...8bc8e6 )
by Simon
01:52
created

ValidateTokenQueryCreator::attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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