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