PluginProbe
WP-Stateless – Google Cloud Storage / 2.1.4
WP-Stateless – Google Cloud Storage v2.1.4
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / vendor / google / auth / src / Middleware / ScopedAccessTokenMiddleware.php

ScopedAccessTokenMiddleware.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/Google/vendor/google/auth/src/Middleware/ScopedAccessTokenMiddleware.php

161 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2015 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 namespace Google\Auth\Middleware;
19
20 use Google\Auth\CacheInterface;
21 use Google\Auth\CacheTrait;
22 use Google\Auth\FetchAuthTokenInterface;
23 use Psr\Http\Message\RequestInterface;
24
25 /**
26 * ScopedAccessTokenMiddleware is a Guzzle Middleware that adds an Authorization
27 * header provided by a closure.
28 *
29 * The closure returns an access token, taking the scope, either a single
30 * string or an array of strings, as its value. If provided, a cache will be
31 * used to preserve the access token for a given lifetime.
32 *
33 * Requests will be accessed with the authorization header:
34 *
35 * 'Authorization' 'Bearer <value of auth_token>'
36 */
37 class ScopedAccessTokenMiddleware
38 {
39 use CacheTrait;
40
41 const DEFAULT_CACHE_LIFETIME = 1500;
42
43 /** @var An implementation of CacheInterface */
44 private $cache;
45
46 /** @var callback */
47 private $httpHandler;
48
49 /** @var An implementation of FetchAuthTokenInterface */
50 private $fetcher;
51
52 /** @var cache configuration */
53 private $cacheConfig;
54
55 /**
56 * Creates a new ScopedAccessTokenMiddleware.
57 *
58 * @param callable $tokenFunc a token generator function
59 * @param array|string $scopes the token authentication scopes
60 * @param array $cacheConfig configuration for the cache when it's present
61 * @param CacheInterface $cache an implementation of CacheInterface
62 */
63 public function __construct(
64 callable $tokenFunc,
65 $scopes,
66 array $cacheConfig = null,
67 CacheInterface $cache = null
68 ) {
69 $this->tokenFunc = $tokenFunc;
70 if (!(is_string($scopes) || is_array($scopes))) {
71 throw new \InvalidArgumentException(
72 'wants scope should be string or array');
73 }
74 $this->scopes = $scopes;
75
76 if (!is_null($cache)) {
77 $this->cache = $cache;
78 $this->cacheConfig = array_merge([
79 'lifetime' => self::DEFAULT_CACHE_LIFETIME,
80 'prefix' => ''
81 ], $cacheConfig);
82 }
83 }
84
85 /**
86 * Updates the request with an Authorization header when auth is 'scoped'.
87 *
88 * E.g this could be used to authenticate using the AppEngine
89 * AppIdentityService.
90 *
91 * use google\appengine\api\app_identity\AppIdentityService;
92 * use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
93 * use GuzzleHttp\Client;
94 * use GuzzleHttp\HandlerStack;
95 *
96 * $scope = 'https://www.googleapis.com/auth/taskqueue'
97 * $middleware = new ScopedAccessTokenMiddleware(
98 * 'AppIdentityService::getAccessToken',
99 * $scope,
100 * [ 'prefix' => 'Google\Auth\ScopedAccessToken::' ],
101 * $cache = new Memcache()
102 * );
103 * $stack = HandlerStack::create();
104 * $stack->push($middleware);
105 *
106 * $client = new Client([
107 * 'handler' => $stack,
108 * 'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
109 * 'auth' => 'google_auth' // authorize all requests
110 * ]);
111 *
112 * $res = $client->get('myproject/taskqueues/myqueue');
113 */
114 public function __invoke(callable $handler)
115 {
116 return function (RequestInterface $request, array $options) use ($handler) {
117 // Requests using "auth"="scoped" will be authorized.
118 if (!isset($options['auth']) || $options['auth'] !== 'scoped') {
119 return $handler($request, $options);
120 }
121
122 $request = $request->withHeader('Authorization', 'Bearer ' . $this->fetchToken());
123 return $handler($request, $options);
124 };
125 }
126
127 /**
128 * @return string
129 */
130 private function getCacheKey()
131 {
132 $key = null;
133
134 if (is_string($this->scopes)) {
135 $key .= $this->scopes;
136 } else if (is_array($this->scopes)) {
137 $key .= implode(":", $this->scopes);
138 }
139 return $key;
140 }
141
142 /**
143 * Determine if token is available in the cache, if not call tokenFunc to
144 * fetch it.
145 *
146 * @return string
147 */
148 private function fetchToken()
149 {
150 $cached = $this->getCachedValue();
151
152 if (!empty($cached)) {
153 return $cached;
154 }
155
156 $token = call_user_func($this->tokenFunc, $this->scopes);
157 $this->setCachedValue($token);
158 return $token;
159 }
160 }
161