Completed
Push — master ( dd6027...6d621d )
by Tobias
06:47
created

CreateTokenMutationCreator::resolve()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
nc 12
nop 4
1
<?php
2
3
namespace Firesphere\GraphQLJWT;
4
5
use GraphQL\Type\Definition\ResolveInfo;
6
use GraphQL\Type\Definition\Type;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\GraphQL\MutationCreator;
10
use SilverStripe\GraphQL\OperationResolver;
11
use SilverStripe\Security\Authenticator;
12
use SilverStripe\Security\IdentityStore;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Security\Security;
15
16
class CreateTokenMutationCreator extends MutationCreator implements OperationResolver
17
{
18
    public function attributes()
19
    {
20
        return [
21
            'name'        => 'createToken',
22
            'description' => 'Creates a JWT token for a valid user'
23
        ];
24
    }
25
26
    public function type()
27
    {
28
        return $this->manager->getType('MemberToken');
29
    }
30
31
    public function args()
32
    {
33
        return [
34
            'Email'    => ['type' => Type::nonNull(Type::string())],
35
            'Password' => ['type' => Type::nonNull(Type::string())]
36
        ];
37
    }
38
39
    /**
40
     * @param mixed $object
41
     * @param array $args
42
     * @param mixed $context
43
     * @param ResolveInfo $info
44
     * @return null|Member|static
45
     */
46
    public function resolve($object, array $args, $context, ResolveInfo $info)
47
    {
48
        $security = Injector::inst()->get(Security::class);
49
        $authenticators = $security->getApplicableAuthenticators(Authenticator::LOGIN);
50
        $request = Controller::curr()->getRequest();
51
        $member = null;
52
53
        if (count($authenticators)) {
54
            foreach ($authenticators as $authenticator) {
55
                $member = $authenticator->authenticate($args, $request, $result);
0 ignored issues
show
Bug introduced by
The variable $result does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
56
                if ($result->isValid()) {
57
                    break;
58
                }
59
            }
60
        }
61
        $authenticator = Injector::inst()->get(JWTAuthenticator::class);
62
63
        if ($member instanceof Member) {
64
            $member->Token = $authenticator->generateToken($member);
65
        } elseif (JWTAuthenticator::config()->get('anonymous_allowed')) {
66
            $member = Member::create(['ID' => 0, 'FirstName' => 'Anonymous']);
67
            // Create an anonymous token
68
            $member->Token = $authenticator->generateToken($member);
69
        } else {
70
            Security::setCurrentUser(null);
71
            Injector::inst()->get(IdentityStore::class)->logOut();
72
73
            // Return a token-less member
74
            return Member::create();
75
        }
76
77
        return $member;
78
    }
79
}
80