Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

AbstractService.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 8.18 KiB


 001  <?php
 002  namespace OAuth\OAuth2\Service;
 003   
 004  use OAuth\Common\Consumer\Credentials;
 005  use OAuth\Common\Exception\Exception;
 006  use OAuth\Common\Service\AbstractService as BaseAbstractService;
 007  use OAuth\Common\Storage\TokenStorageInterface;
 008  use OAuth\Common\Http\Exception\TokenResponseException;
 009  use OAuth\Common\Http\Client\ClientInterface;
 010  use OAuth\Common\Http\Uri\UriInterface;
 011  use OAuth\OAuth2\Service\Exception\InvalidScopeException;
 012  use OAuth\OAuth2\Service\Exception\MissingRefreshTokenException;
 013  use OAuth\Common\Token\TokenInterface;
 014  use OAuth\Common\Token\Exception\ExpiredTokenException;
 015   
 016  abstract class AbstractService extends BaseAbstractService implements ServiceInterface
 017  {
 018   
 019      /** @const OAUTH_VERSION */
 020      const OAUTH_VERSION = 2;
 021   
 022      /** @var array */
 023      protected $scopes;
 024   
 025      /** @var \OAuth\Common\Http\Uri\UriInterface|null */
 026      protected $baseApiUri;
 027   
 028      /**
 029       * @param \OAuth\Common\Consumer\Credentials $credentials
 030       * @param \OAuth\Common\Http\Client\ClientInterface $httpClient
 031       * @param \OAuth\Common\Storage\TokenStorageInterface $storage
 032       * @param array $scopes
 033       * @param UriInterface|null $baseApiUri
 034       * @throws InvalidScopeException
 035       */
 036      public function __construct(Credentials $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, $scopes = array(), UriInterface $baseApiUri = null)
 037      {
 038          parent::__construct($credentials, $httpClient, $storage);
 039   
 040          foreach($scopes as $scope)
 041          {
 042              if( !$this->isValidScope($scope) ) {
 043                  throw new InvalidScopeException('Scope ' . $scope . ' is not valid for service ' . get_class($this) );
 044              }
 045          }
 046   
 047          $this->scopes = $scopes;
 048   
 049          $this->baseApiUri = $baseApiUri;
 050      }
 051   
 052      /**
 053       * Returns the url to redirect to for authorization purposes.
 054       *
 055       * @param array $additionalParameters
 056       * @return string
 057       */
 058      public function getAuthorizationUri( array $additionalParameters = array() )
 059      {
 060          $parameters = array_merge($additionalParameters, array(
 061              'type'          => 'web_server',
 062              'client_id'     => $this->credentials->getConsumerId(),
 063              'redirect_uri'  => $this->credentials->getCallbackUrl(),
 064              'response_type' => 'code',
 065          ));
 066   
 067          $parameters['scope'] = implode(' ', $this->scopes);
 068   
 069          // Build the url
 070          $url = clone $this->getAuthorizationEndpoint();
 071          foreach($parameters as $key => $val)
 072          {
 073              $url->addToQuery($key, $val);
 074          }
 075   
 076          return $url;
 077      }
 078   
 079   
 080      /**
 081       * Retrieves and stores the OAuth2 access token after a successful authorization.
 082       *
 083       * @param string $code The access code from the callback.
 084       * @return TokenInterface $token
 085       * @throws TokenResponseException
 086       */
 087      public function requestAccessToken($code)
 088      {
 089          $bodyParams = array(
 090              'code'          => $code,
 091              'client_id'     => $this->credentials->getConsumerId(),
 092              'client_secret' => $this->credentials->getConsumerSecret(),
 093              'redirect_uri'  => $this->credentials->getCallbackUrl(),
 094              'grant_type'    => 'authorization_code',
 095          );
 096   
 097          $responseBody = $this->httpClient->retrieveResponse($this->getAccessTokenEndpoint(), $bodyParams, $this->getExtraOAuthHeaders());
 098          $token = $this->parseAccessTokenResponse( $responseBody );
 099          $this->storage->storeAccessToken($this->service(), $token );
 100   
 101          return $token;
 102      }
 103   
 104      /**
 105       * Sends an authenticated API request to the path provided.
 106       * If the path provided is not an absolute URI, the base API Uri (must be passed into constructor) will be used.
 107       *
 108       * @param $path string|UriInterface
 109       * @param string $method HTTP method
 110       * @param array $body Request body if applicable.
 111       * @param array $extraHeaders Extra headers if applicable. These will override service-specific any defaults.
 112       * @return string
 113       * @throws ExpiredTokenException
 114       * @throws Exception
 115       */
 116      public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
 117      {
 118          $uri = $this->determineRequestUriFromPath($path, $this->baseApiUri);
 119          $token = $this->storage->retrieveAccessToken($this->service());
 120   
 121          if( ( $token->getEndOfLife() !== TokenInterface::EOL_NEVER_EXPIRES ) &&
 122              ( $token->getEndOfLife() !== TokenInterface::EOL_UNKNOWN ) &&
 123              ( time() > $token->getEndOfLife() ) ) {
 124   
 125              throw new ExpiredTokenException('Token expired on ' . date('m/d/Y', $token->getEndOfLife()) . ' at ' . date('h:i:s A', $token->getEndOfLife()) );
 126          }
 127   
 128          // add the token where it may be needed
 129          if( static::AUTHORIZATION_METHOD_HEADER_OAUTH === $this->getAuthorizationMethod() ) {
 130              $extraHeaders = array_merge( array('Authorization' => 'OAuth ' . $token->getAccessToken()), $extraHeaders );
 131          } elseif( static::AUTHORIZATION_METHOD_QUERY_STRING === $this->getAuthorizationMethod() ) {
 132              $uri->addToQuery( 'access_token', $token->getAccessToken() );
 133          } elseif( static::AUTHORIZATION_METHOD_QUERY_STRING_V2 === $this->getAuthorizationMethod() ) {
 134              $uri->addToQuery( 'oauth2_access_token', $token->getAccessToken() );
 135          } elseif( static::AUTHORIZATION_METHOD_HEADER_BEARER === $this->getAuthorizationMethod() ) {
 136              $extraHeaders = array_merge( array('Authorization' => 'Bearer ' . $token->getAccessToken()), $extraHeaders );
 137          }
 138   
 139          $extraHeaders = array_merge( $this->getExtraApiHeaders(), $extraHeaders );
 140   
 141          return $this->httpClient->retrieveResponse($uri, $body, $extraHeaders, $method);
 142      }
 143   
 144      /**
 145      * Accessor to the storage adapter to be able to retrieve tokens
 146      *
 147      * @return OAuth\Common\Storage\TokenStorageInterface
 148      */
 149      public function getStorage()
 150      {
 151          return $this->storage;
 152      }
 153   
 154      /**
 155       * Refreshes an OAuth2 access token.
 156       *
 157       * @param \OAuth\Common\Token\TokenInterface $token
 158       * @return \OAuth\Common\Token\TokenInterface $token
 159       * @throws \OAuth\OAuth2\Service\Exception\MissingRefreshTokenException
 160       */
 161      public function refreshAccessToken(TokenInterface $token)
 162      {
 163          $refreshToken = $token->getRefreshToken();
 164   
 165          if ( empty( $refreshToken ) ) {
 166              throw new MissingRefreshTokenException();
 167          }
 168   
 169          $parameters = array(
 170              'grant_type'    => 'refresh_token',
 171              'type'          => 'web_server',
 172              'client_id'     => $this->credentials->getConsumerId(),
 173              'client_secret' => $this->credentials->getConsumerSecret(),
 174              'refresh_token' => $refreshToken,
 175          );
 176   
 177          $responseBody = $this->httpClient->retrieveResponse($this->getAccessTokenEndpoint(), $parameters, $this->getExtraOAuthHeaders());
 178          $token = $this->parseAccessTokenResponse($responseBody);
 179          $this->storage->storeAccessToken($this->service(), $token);
 180   
 181          return $token;
 182      }
 183   
 184      /**
 185       * Return whether or not the passed scope value is valid.
 186       *
 187       * @param $scope
 188       * @return bool
 189       */
 190      public function isValidScope($scope)
 191      {
 192          $reflectionClass = new \ReflectionClass(get_class($this));
 193          return in_array( $scope, $reflectionClass->getConstants() );
 194      }
 195   
 196      /**
 197       * Return any additional headers always needed for this service implementation's OAuth calls.
 198       *
 199       * @return array
 200       */
 201      protected function getExtraOAuthHeaders()
 202      {
 203          return array();
 204      }
 205   
 206      /**
 207       * Return any additional headers always needed for this service implementation's API calls.
 208       *
 209       * @return array
 210       */
 211      protected function getExtraApiHeaders()
 212      {
 213          return array();
 214      }
 215   
 216      /**
 217       * Parses the access token response and returns a TokenInterface.
 218       *
 219       * @abstract
 220       * @return \OAuth\Common\Token\TokenInterface
 221       * @param string $responseBody
 222       */
 223      abstract protected function parseAccessTokenResponse($responseBody);
 224   
 225      /**
 226       * Returns a class constant from ServiceInterface defining the authorization method used for the API
 227       * Header is the sane default.
 228       *
 229       * @return int
 230       */
 231      protected function getAuthorizationMethod()
 232      {
 233          return static::AUTHORIZATION_METHOD_HEADER_OAUTH;
 234      }
 235  }
 236