1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\GraphQLJWT\Mutations; |
4
|
|
|
|
5
|
|
|
use Firesphere\GraphQLJWT\Authentication\JWTAuthenticator; |
6
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
8
|
|
|
use SilverStripe\Control\Controller; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\GraphQL\MutationCreator; |
11
|
|
|
use SilverStripe\GraphQL\OperationResolver; |
12
|
|
|
use SilverStripe\Security\Authenticator; |
13
|
|
|
use SilverStripe\Security\IdentityStore; |
14
|
|
|
use SilverStripe\Security\Member; |
15
|
|
|
use SilverStripe\Security\Security; |
16
|
|
|
|
17
|
|
|
class CreateTokenMutationCreator extends MutationCreator implements OperationResolver |
18
|
|
|
{ |
19
|
|
|
public function attributes() |
20
|
|
|
{ |
21
|
|
|
return [ |
22
|
|
|
'name' => 'createToken', |
23
|
|
|
'description' => 'Creates a JWT token for a valid user' |
24
|
|
|
]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function type() |
28
|
|
|
{ |
29
|
|
|
return $this->manager->getType('MemberToken'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function args() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
'Email' => ['type' => Type::nonNull(Type::string())], |
36
|
|
|
'Password' => ['type' => Type::nonNull(Type::string())] |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed $object |
42
|
|
|
* @param array $args |
43
|
|
|
* @param mixed $context |
44
|
|
|
* @param ResolveInfo $info |
45
|
|
|
* @return null|Member|static |
46
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
47
|
|
|
*/ |
48
|
|
|
public function resolve($object, array $args, $context, ResolveInfo $info) |
49
|
|
|
{ |
50
|
|
|
$security = Injector::inst()->get(Security::class); |
51
|
|
|
$authenticators = $security->getApplicableAuthenticators(Authenticator::LOGIN); |
52
|
|
|
$request = Controller::curr()->getRequest(); |
53
|
|
|
$member = null; |
54
|
|
|
|
55
|
|
|
if (count($authenticators)) { |
56
|
|
|
foreach ($authenticators as $authenticator) { |
57
|
|
|
$member = $authenticator->authenticate($args, $request, $result); |
|
|
|
|
58
|
|
|
if ($result->isValid()) { |
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
64
|
|
|
|
65
|
|
|
if ($member instanceof Member) { |
66
|
|
|
$member->Token = $authenticator->generateToken($member); |
67
|
|
|
} elseif (JWTAuthenticator::config()->get('anonymous_allowed')) { |
68
|
|
|
$member = Member::create(['ID' => 0, 'FirstName' => 'Anonymous']); |
69
|
|
|
// Create an anonymous token |
70
|
|
|
$member->Token = $authenticator->generateToken($member); |
71
|
|
|
} else { |
72
|
|
|
Security::setCurrentUser(null); |
73
|
|
|
Injector::inst()->get(IdentityStore::class)->logOut(); |
74
|
|
|
|
75
|
|
|
// Return a token-less member |
76
|
|
|
$member = Member::create(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $member; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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