Source of file Authenticator.php
Size: 2,212 Bytes - Last Modified: 2022-02-21T10:00:46+00:00
/var/www/docs.ssmods.com/process/src/src/GraphQL/Authenticator.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 | <?php namespace AdvancedLearning\Oauth2Server\GraphQL; use AdvancedLearning\Oauth2Server\Exceptions\AuthenticationException; use AdvancedLearning\Oauth2Server\Models\Client; use function is_null; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Injector\Injector; use SilverStripe\GraphQL\Auth\AuthenticatorInterface; use SilverStripe\ORM\ValidationException; use SilverStripe\Security\Member; use function substr; class Authenticator implements AuthenticatorInterface { public function authenticate(HTTPRequest $request) { $authenticator = Injector::inst()->get(\AdvancedLearning\Oauth2Server\Services\Authenticator::class); try { $request = $authenticator->authenticate($request); if ($userId = $request->getHeader('oauth_user_id')) { return Member::get()->filter(['Email' => $userId])->first(); // return a fake member for the client } else if ($clientId = $request->getHeader('oauth_client_id')) { $member = new Member(); $client = Client::get()->filter(['Identifier' => $clientId])->first(); if (!$client) { throw new ValidationException('Could not find a valid client/user', 403); } $member->FirstName = $client->Name; return $member; } throw new ValidationException('Could not find a valid client/user', 403); } catch (AuthenticationException $exception) { throw new ValidationException($exception->getMessage(), 403); } } public function isApplicable(HTTPRequest $request) { return !is_null($this->getToken($request)); } /** * Extract the token from the authorization header. * * @param HTTPRequest $request The request container the token. * * @return null|string */ protected function getToken(HTTPRequest $request): ?string { if ($authHeader = $request->getHeader('Authorization')) { if (stripos($authHeader, 'Bearer ') === 0) { return substr($authHeader, 6); } } return null; } } |