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 / Subscriber / ScopedAccessTokenSubscriber.php

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

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