Source of file AuthenticationService.php
Size: 4,421 Bytes - Last Modified: 2022-02-21T10:00:46+00:00
/var/www/docs.ssmods.com/process/src/src/Services/AuthenticationService.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 | <?php namespace AdvancedLearning\Oauth2Server\Services; use AdvancedLearning\Oauth2Server\Exceptions\AuthenticationException; use AdvancedLearning\Oauth2Server\Repositories\AccessTokenRepository; use GuzzleHttp\Psr7\Response; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\ResourceServer; use Robbie\Psr7\HttpRequestAdapter; use Robbie\Psr7\HttpResponseAdapter; use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Environment; class AuthenticationService implements Authenticator { protected $server; /** * AuthenticationService constructor. * * @param ResourceServer|null $server Optional resource server. */ public function __construct(ResourceServer $server = null) { $this->server = $server ?: $this->createServer(); } /** * Authenticate the request. Adds oauth fields as headers on the request. * * @param HTTPRequest $request The SilverStripe request object to be authenticated. * * @return HTTPRequest * @throws AuthenticationException */ public function authenticate(HTTPRequest $request): HTTPRequest { $requestAdapter = new HttpRequestAdapter(); $responseAdapter = new HttpResponseAdapter(); // missing vars (cli) $this->addMissingServerVariables($requestAdapter); $server = $this->getServer(); $psrRequest = $requestAdapter->toPsr7($request); $psrResponse = new Response(); try { $psrRequest = $server->validateAuthenticatedRequest($psrRequest); } catch (OAuthServerException $exception) { // convert to authentication exception throw new AuthenticationException( $exception->getMessage(), $exception->getCode(), $responseAdapter->fromPsr7($exception->generateHttpResponse($psrResponse)) ); } catch (\Exception $exception) { // convert to authentication exception throw new AuthenticationException( $exception->getMessage(), $exception->getCode(), $responseAdapter->fromPsr7( (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500)) ->generateHttpResponse($psrResponse) ) ); } // add the request attributes as custom auth headers foreach ($psrRequest->getAttributes() as $attribute => $value) { $request->addHeader($attribute, $value); } return $request; } /** * Override the default ResourceServer. * * @param ResourceServer $v The new ResourceServer to use. * * @return $this */ public function setServer(ResourceServer $v): Authenticator { $this->server = $v; return $this; } /** * Get the ResourceServer. * * @return ResourceServer */ public function getServer(): ResourceServer { return $this->server; } /** * Create a default ResourceServer. Used if one isn't provided. * * @return ResourceServer */ protected function createServer(): ResourceServer { // Init our repositories $accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface // Path to authorization server's public key $publicKeyPath = Environment::getEnv('OAUTH_PUBLIC_KEY_PATH'); // Relative paths to the web root $publicKeyPath = str_replace('{BASE_DIR}', Director::baseFolder(), $publicKeyPath); // Setup the authorization server return new ResourceServer( $accessTokenRepository, $publicKeyPath ); } /** * Cli is missing some $_SERVER variables. * * @param HttpRequestAdapter $adapter */ protected function addMissingServerVariables(HttpRequestAdapter $adapter) { $vars = $adapter->getServerVars() ?: []; $defaults = [ 'SERVER_PORT' => 80, 'HTTP_HOST' => Environment::getEnv('SS_BASE_URL') ]; foreach ($defaults as $key => $value) { if (empty($vars[$key])) { $vars[$key] = $value; } } $adapter->setServerVars($vars); } } |