RefreshTokenMutationCreator::attributes()   A
last analyzed

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 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Firesphere\GraphQLJWT\Mutations;
4
5
use BadMethodCallException;
6
use Exception;
7
use Firesphere\GraphQLJWT\Helpers\HeaderExtractor;
8
use Firesphere\GraphQLJWT\Helpers\MemberTokenGenerator;
9
use Firesphere\GraphQLJWT\Helpers\RequiresAuthenticator;
10
use Firesphere\GraphQLJWT\Model\JWTRecord;
11
use Firesphere\GraphQLJWT\Types\TokenStatusEnum;
12
use GraphQL\Type\Definition\ResolveInfo;
13
use GraphQL\Type\Definition\Type;
14
use OutOfBoundsException;
15
use Psr\Container\NotFoundExceptionInterface;
16
use SilverStripe\Control\Controller;
17
use SilverStripe\Core\Extensible;
18
use SilverStripe\GraphQL\MutationCreator;
19
use SilverStripe\GraphQL\OperationResolver;
20
use SilverStripe\ORM\ValidationException;
21
22
class RefreshTokenMutationCreator extends MutationCreator implements OperationResolver
23
{
24
    use RequiresAuthenticator;
25
    use HeaderExtractor;
26
    use MemberTokenGenerator;
27
    use Extensible;
28
29
    public function attributes(): array
30
    {
31
        return [
32
            'name'        => 'refreshToken',
33
            'description' => 'Refreshes a JWT token for a valid user. To be done'
34
        ];
35
    }
36
37
    public function type(): Type
38
    {
39
        return $this->manager->getType('MemberToken');
40
    }
41
42
    /**
43
     * @param mixed $object
44
     * @param array $args
45
     * @param mixed $context
46
     * @param ResolveInfo $info
47
     * @return array
48
     * @throws NotFoundExceptionInterface
49
     * @throws ValidationException
50
     * @throws BadMethodCallException
51
     * @throws OutOfBoundsException
52
     * @throws Exception
53
     */
54
    public function resolve($object, array $args, $context, ResolveInfo $info): array
55
    {
56
        $authenticator = $this->getJWTAuthenticator();
57
        $request = Controller::curr()->getRequest();
58
        $token = $this->getAuthorizationHeader($request);
59
60
        // Check status of existing token
61
        /** @var JWTRecord $record */
62
        list($record, $status) = $authenticator->validateToken($token, $request);
63
        $member = null;
64
        switch ($status) {
65
            case TokenStatusEnum::STATUS_OK:
66
            case TokenStatusEnum::STATUS_EXPIRED:
67
                $member = $record->Member();
68
                $renewable = true;
69
                break;
70
            case TokenStatusEnum::STATUS_DEAD:
71
            case TokenStatusEnum::STATUS_INVALID:
72
            default:
73
                $member = null;
74
                $renewable = false;
75
                break;
76
        }
77
78
        // Check if renewable
79
        if (!$renewable) {
80
            return $this->generateResponse($status);
81
        }
82
83
        // Create new token for member
84
        $newToken = $authenticator->generateToken($request, $member);
0 ignored issues
show
Bug introduced by
It seems like $member can also be of type null; however, parameter $member of Firesphere\GraphQLJWT\Au...icator::generateToken() does only seem to accept SilverStripe\Security\Member, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $newToken = $authenticator->generateToken($request, /** @scrutinizer ignore-type */ $member);
Loading history...
85
        return $this->generateResponse(TokenStatusEnum::STATUS_OK, $member, $newToken->__toString());
0 ignored issues
show
Deprecated Code introduced by
The function Lcobucci\JWT\Token::__toString() has been deprecated: This method has been removed from the interface in v4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

85
        return $this->generateResponse(TokenStatusEnum::STATUS_OK, $member, /** @scrutinizer ignore-deprecated */ $newToken->__toString());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
86
    }
87
}
88