PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.3
WP-Stateless – Google Cloud Storage v3.0.3
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 3.0.3, at lib/Google/vendor/google/auth/src/Middleware/ScopedAccessTokenMiddleware.php

176 lines 5.0 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\CacheTrait;
21 use Psr\Cache\CacheItemPoolInterface;
22 use Psr\Http\Message\RequestInterface;
23
24 /**
25 * ScopedAccessTokenMiddleware is a Guzzle Middleware that adds an Authorization
26 * header provided by a closure.
27 *
28 * The closure returns an access token, taking the scope, either a single
29 * string or an array of strings, as its value. If provided, a cache will be
30 * used to preserve the access token for a given lifetime.
31 *
32 * Requests will be accessed with the authorization header:
33 *
34 * 'authorization' 'Bearer <value of auth_token>'
35 */
36 class ScopedAccessTokenMiddleware
37 {
38 use CacheTrait;
39
40 const DEFAULT_CACHE_LIFETIME = 1500;
41
42 /**
43 * @var CacheItemPoolInterface
44 */
45 private $cache;
46
47 /**
48 * @var array configuration
49 */
50 private $cacheConfig;
51
52 /**
53 * @var callable
54 */
55 private $tokenFunc;
56
57 /**
58 * @var array|string
59 */
60 private $scopes;
61
62 /**
63 * Creates a new ScopedAccessTokenMiddleware.
64 *
65 * @param callable $tokenFunc a token generator function
66 * @param array|string $scopes the token authentication scopes
67 * @param array $cacheConfig configuration for the cache when it's present
68 * @param CacheItemPoolInterface $cache an implementation of CacheItemPoolInterface
69 */
70 public function __construct(
71 callable $tokenFunc,
72 $scopes,
73 array $cacheConfig = null,
74 CacheItemPoolInterface $cache = null
75 ) {
76 $this->tokenFunc = $tokenFunc;
77 if (!(is_string($scopes) || is_array($scopes))) {
78 throw new \InvalidArgumentException(
79 'wants scope should be string or array'
80 );
81 }
82 $this->scopes = $scopes;
83
84 if (!is_null($cache)) {
85 $this->cache = $cache;
86 $this->cacheConfig = array_merge([
87 'lifetime' => self::DEFAULT_CACHE_LIFETIME,
88 'prefix' => '',
89 ], $cacheConfig);
90 }
91 }
92
93 /**
94 * Updates the request with an Authorization header when auth is 'scoped'.
95 *
96 * E.g this could be used to authenticate using the AppEngine
97 * AppIdentityService.
98 *
99 * use google\appengine\api\app_identity\AppIdentityService;
100 * use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
101 * use GuzzleHttp\Client;
102 * use GuzzleHttp\HandlerStack;
103 *
104 * $scope = 'https://www.googleapis.com/auth/taskqueue'
105 * $middleware = new ScopedAccessTokenMiddleware(
106 * 'AppIdentityService::getAccessToken',
107 * $scope,
108 * [ 'prefix' => 'Google\Auth\ScopedAccessToken::' ],
109 * $cache = new Memcache()
110 * );
111 * $stack = HandlerStack::create();
112 * $stack->push($middleware);
113 *
114 * $client = new Client([
115 * 'handler' => $stack,
116 * 'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
117 * 'auth' => 'scoped' // authorize all requests
118 * ]);
119 *
120 * $res = $client->get('myproject/taskqueues/myqueue');
121 *
122 * @param callable $handler
123 * @return \Closure
124 */
125 public function __invoke(callable $handler)
126 {
127 return function (RequestInterface $request, array $options) use ($handler) {
128 // Requests using "auth"="scoped" will be authorized.
129 if (!isset($options['auth']) || $options['auth'] !== 'scoped') {
130 return $handler($request, $options);
131 }
132
133 $request = $request->withHeader('authorization', 'Bearer ' . $this->fetchToken());
134
135 return $handler($request, $options);
136 };
137 }
138
139 /**
140 * @return string
141 */
142 private function getCacheKey()
143 {
144 $key = null;
145
146 if (is_string($this->scopes)) {
147 $key .= $this->scopes;
148 } elseif (is_array($this->scopes)) {
149 $key .= implode(':', $this->scopes);
150 }
151
152 return $key;
153 }
154
155 /**
156 * Determine if token is available in the cache, if not call tokenFunc to
157 * fetch it.
158 *
159 * @return string
160 */
161 private function fetchToken()
162 {
163 $cacheKey = $this->getCacheKey();
164 $cached = $this->getCachedValue($cacheKey);
165
166 if (!empty($cached)) {
167 return $cached;
168 }
169
170 $token = call_user_func($this->tokenFunc, $this->scopes);
171 $this->setCachedValue($cacheKey, $token);
172
173 return $token;
174 }
175 }
176