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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
AbstractService.php
001 <?php
002 namespace OAuth\OAuth1\Service;
003
004 use OAuth\Common\Consumer\Credentials;
005 use OAuth\Common\Storage\TokenStorageInterface;
006 use OAuth\Common\Http\Exception\TokenResponseException;
007 use OAuth\Common\Http\Client\ClientInterface;
008 use OAuth\Common\Http\Uri\UriInterface;
009 use OAuth\OAuth1\Signature\SignatureInterface;
010 use OAuth\OAuth1\Token\TokenInterface;
011 use OAuth\OAuth1\Token\StdOAuth1Token;
012 use OAuth\Common\Service\AbstractService as BaseAbstractService;
013
014 abstract class AbstractService extends BaseAbstractService implements ServiceInterface
015 {
016
017 /** @const OAUTH_VERSION */
018 const OAUTH_VERSION = 1;
019
020 /** @var \OAuth\OAuth1\Signature\SignatureInterface */
021 protected $signature;
022
023 /** @var \OAuth\Common\Http\Uri\UriInterface|null */
024 protected $baseApiUri;
025
026 /**
027 * @param \OAuth\Common\Consumer\Credentials $credentials
028 * @param \OAuth\Common\Http\Client\ClientInterface $httpClient
029 * @param \OAuth\Common\Storage\TokenStorageInterface $storage
030 * @param \OAuth\OAuth1\Signature\SignatureInterface $signature
031 * @param UriInterface|null $baseApiUri
032 */
033 public function __construct(Credentials $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, SignatureInterface $signature, UriInterface $baseApiUri = null)
034 {
035 parent::__construct($credentials, $httpClient, $storage);
036
037 $this->signature = $signature;
038 $this->baseApiUri = $baseApiUri;
039
040 $this->signature->setHashingAlgorithm($this->getSignatureMethod());
041 }
042
043 /**
044 * Retrieves and stores the OAuth1 request token obtained from the service.
045 *
046 * @return TokenInterface $token
047 * @throws TokenResponseException
048 */
049 public function requestRequestToken()
050 {
051 $authorizationHeader = array('Authorization' => $this->buildAuthorizationHeaderForTokenRequest());
052 $headers = array_merge($authorizationHeader, $this->getExtraOAuthHeaders());
053
054 $responseBody = $this->httpClient->retrieveResponse($this->getRequestTokenEndpoint(), array(), $headers);
055
056 $token = $this->parseRequestTokenResponse($responseBody);
057 $this->storage->storeAccessToken($this->service(), $token);
058
059 return $token;
060 }
061
062 /**
063 * Returns the url to redirect to for authorization purposes.
064 *
065 * @param array $additionalParameters
066 * @return string
067 */
068 public function getAuthorizationUri( array $additionalParameters = array() )
069 {
070 // Build the url
071 $url = clone $this->getAuthorizationEndpoint();
072 foreach($additionalParameters as $key => $val)
073 {
074 $url->addToQuery($key, $val);
075 }
076
077 return $url;
078 }
079
080 /**
081 * Retrieves and stores/returns the OAuth1 access token after a successful authorization.
082 *
083 * @abstract
084 * @param string $token The request token from the callback.
085 * @param string $verifier
086 * @param string $tokenSecret
087 * @return TokenInterface $token
088 * @throws TokenResponseException
089 */
090 public function requestAccessToken($token, $verifier, $tokenSecret = null)
091 {
092 if(is_null($tokenSecret)) {
093 $storedRequestToken = $this->storage->retrieveAccessToken($this->service());
094 $tokenSecret = $storedRequestToken->getRequestTokenSecret();
095 }
096 $this->signature->setTokenSecret($tokenSecret);
097
098 $extraAuthenticationHeaders = array(
099 'oauth_token' => $token,
100 );
101
102 $bodyParams = array(
103 'oauth_verifier' => $verifier,
104 );
105
106 $authorizationHeader = array(
107 'Authorization' =>
108 $this->buildAuthorizationHeaderForAPIRequest(
109 'POST',
110 $this->getAccessTokenEndpoint(),
111 $this->storage->retrieveAccessToken($this->service()),
112 $bodyParams
113 )
114 );
115
116 $headers = array_merge($authorizationHeader, $this->getExtraOAuthHeaders());
117
118 $responseBody = $this->httpClient->retrieveResponse($this->getAccessTokenEndpoint(), $bodyParams, $headers);
119
120 $token = $this->parseAccessTokenResponse($responseBody);
121 $this->storage->storeAccessToken($this->service(), $token);
122
123 return $token;
124 }
125
126 /**
127 * Sends an authenticated API request to the path provided.
128 * If the path provided is not an absolute URI, the base API Uri (must be passed into constructor) will be used.
129 * @param $path string|UriInterface
130 * @param string $method HTTP method
131 * @param array $body Request body if applicable (key/value pairs)
132 * @param array $extraHeaders Extra headers if applicable. These will override service-specific any defaults.
133 * @return string
134 */
135 public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
136 {
137 $uri = $this->determineRequestUriFromPath($path, $this->baseApiUri);
138
139 /** @var $token \OAuth\OAuth1\Token\StdOAuth1Token */
140 $token = $this->storage->retrieveAccessToken($this->service());
141 $extraHeaders = array_merge( $this->getExtraApiHeaders(), $extraHeaders );
142 $authorizationHeader = array('Authorization' => $this->buildAuthorizationHeaderForAPIRequest($method, $uri, $token, $body));
143 $headers = array_merge($authorizationHeader, $extraHeaders);
144
145 return $this->httpClient->retrieveResponse($uri, $body, $headers, $method);
146 }
147
148 /**
149 * Return any additional headers always needed for this service implementation's OAuth calls.
150 *
151 * @return array
152 */
153 protected function getExtraOAuthHeaders()
154 {
155 return array();
156 }
157
158 /**
159 * Return any additional headers always needed for this service implementation's API calls.
160 *
161 * @return array
162 */
163 protected function getExtraApiHeaders()
164 {
165 return array();
166 }
167
168 /**
169 * Builds the authorization header for getting an access or request token.
170 *
171 * @param array $extraParameters
172 * @return string
173 */
174 protected function buildAuthorizationHeaderForTokenRequest(array $extraParameters = array())
175 {
176 $parameters = $this->getBasicAuthorizationHeaderInfo();
177 $parameters = array_merge($parameters, $extraParameters);
178 $parameters['oauth_signature'] = $this->signature->getSignature($this->getRequestTokenEndpoint(), $parameters, 'POST');
179
180 $authorizationHeader = 'OAuth ';
181 $delimiter = '';
182 foreach($parameters as $key => $value) {
183 $authorizationHeader .= $delimiter . rawurlencode($key) . '="' . rawurlencode($value) . '"';
184
185 $delimiter = ', ';
186 }
187
188 return $authorizationHeader;
189 }
190
191 /**
192 * Builds the authorization header for an authenticated API request
193 * @param string $method
194 * @param UriInterface $uri the uri the request is headed
195 * @param \OAuth\OAuth1\Token\TokenInterface $token
196 * @param $bodyParams Request body if applicable (key/value pairs)
197 * @return string
198 */
199 protected function buildAuthorizationHeaderForAPIRequest($method, UriInterface $uri, TokenInterface $token, $bodyParams = null)
200 {
201 $this->signature->setTokenSecret($token->getAccessTokenSecret());
202 $parameters = $this->getBasicAuthorizationHeaderInfo();
203 if( isset($parameters['oauth_callback'] ) ){
204 unset($parameters['oauth_callback']);
205 }
206
207 $parameters = array_merge($parameters, array('oauth_token' => $token->getAccessToken()) );
208
209 $mergedParams = (is_array($bodyParams)) ? array_merge($parameters, $bodyParams) : $parameters;
210
211 $parameters['oauth_signature'] = $this->signature->getSignature($uri, $mergedParams, $method);
212
213 $authorizationHeader = 'OAuth ';
214 $delimiter = '';
215
216 foreach($parameters as $key => $value) {
217 $authorizationHeader .= $delimiter . rawurlencode($key) . '="' . rawurlencode($value) . '"';
218 $delimiter = ', ';
219 }
220
221 return $authorizationHeader;
222 }
223
224 /**
225 * Builds the authorization header array.
226 *
227 * @return array
228 */
229 protected function getBasicAuthorizationHeaderInfo()
230 {
231 $dateTime = new \DateTime();
232 $headerParameters = array(
233 'oauth_callback' => $this->credentials->getCallbackUrl(),
234 'oauth_consumer_key' => $this->credentials->getConsumerId(),
235 'oauth_nonce' => $this->generateNonce(),
236 'oauth_signature_method' => $this->getSignatureMethod(),
237 'oauth_timestamp' => $dateTime->format('U'),
238 'oauth_version' => $this->getVersion(),
239 );
240
241 return $headerParameters;
242 }
243
244 /**
245 * Pseudo random string generator used to build a unique string to sign each request
246 *
247 * @param int $length
248 * @return string
249 */
250 protected function generateNonce($length = 32)
251 {
252 $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
253
254 $nonce = '';
255 $maxRand = strlen($characters)-1;
256 for($i = 0; $i < $length; $i++) {
257 $nonce.= $characters[rand(0, $maxRand)];
258 }
259
260 return $nonce;
261 }
262
263 /**
264 * @return string
265 */
266 protected function getSignatureMethod()
267 {
268 return 'HMAC-SHA1';
269 }
270
271 /**
272 * This returns the version used in the authorization header of the requests
273 *
274 * @return string
275 */
276 protected function getVersion()
277 {
278 return '1.0';
279 }
280
281
282 /**
283 * Parses the request token response and returns a TokenInterface.
284 *
285 * @abstract
286 * @return \OAuth\OAuth1\Token\TokenInterface
287 * @param string $responseBody
288 */
289 abstract protected function parseRequestTokenResponse($responseBody);
290
291 /**
292 * Parses the access token response and returns a TokenInterface.
293 *
294 * @abstract
295 * @return \OAuth\OAuth1\Token\TokenInterface
296 * @param string $responseBody
297 */
298 abstract protected function parseAccessTokenResponse($responseBody);
299 }
300