| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Firesphere\GraphQLJWT\Authentication; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BadMethodCallException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Core\Config\Configurable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Core\Injector\Injectable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\ORM\ValidationResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Security\Authenticator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Security\Member; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class AnonymousUserAuthenticator extends MemberAuthenticator | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     use Configurable; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     use Injectable; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * Anonymous username | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     private static $anonymous_username = 'anonymous'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     public function supportedServices(): int | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         return Authenticator::LOGIN | Authenticator::LOGOUT; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null): ?Member | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // Only applies to request for anonymous user specifically | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $email = $data['Email'] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if ($email !== static::config()->get('anonymous_username')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |         return parent::authenticate($data, $request, $result); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * Attempt to find and authenticate member if possible from the given data | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @skipUpgrade | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @param array $data Form submitted data | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param ValidationResult $result | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param Member $member This third parameter is used in the CMSAuthenticator(s) | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return Member Found member, regardless of successful login | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     protected function authenticateMember($data, ValidationResult &$result = null, Member $member = null): Member | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         // Get user, or create if not exists | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $username = static::config()->get('anonymous_username'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $member = Injector::inst()->get(Member::class . '.anonymous', true, ['username' => $username]); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         // Validate this member is still allowed to login | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $result = $result ?: ValidationResult::create(); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $member->validateCanLogin($result); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         // Emit failure to member and form (if available) | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         if ($result->isValid()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             $member->registerSuccessfulLogin(); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             $member->registerFailedLogin(); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         return $member; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function checkPassword(Member $member, $password, ValidationResult &$result = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |         throw new BadMethodCallException("checkPassword not supported for anonymous users"); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  |