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 / SimpleMiddleware.php

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

87 lines 2.6 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 GuzzleHttp\Psr7;
21 use Psr\Http\Message\RequestInterface;
22
23 /**
24 * SimpleMiddleware is a Guzzle Middleware that implements Google's Simple API
25 * access.
26 *
27 * Requests are accessed using the Simple API access developer key.
28 */
29 class SimpleMiddleware
30 {
31 /** @var configuration */
32 private $config;
33
34 /**
35 * Create a new Simple plugin.
36 *
37 * The configuration array expects one option
38 * - key: required, otherwise InvalidArgumentException is thrown
39 *
40 * @param array $config Configuration array
41 */
42 public function __construct(array $config)
43 {
44 if (!isset($config['key'])) {
45 throw new \InvalidArgumentException('requires a key to have been set');
46 }
47
48 $this->config = array_merge(['key' => null], $config);
49 }
50
51 /**
52 * Updates the request query with the developer key if auth is set to simple
53 *
54 * use Google\Auth\Middleware\SimpleMiddleware;
55 * use GuzzleHttp\Client;
56 * use GuzzleHttp\HandlerStack;
57 *
58 * $my_key = 'is not the same as yours';
59 * $middleware = new SimpleMiddleware(['key' => $my_key]);
60 * $stack = HandlerStack::create();
61 * $stack->push($middleware);
62 *
63 * $client = new Client([
64 * 'handler' => $stack,
65 * 'base_uri' => 'https://www.googleapis.com/discovery/v1/',
66 * 'auth' => 'simple'
67 * ]);
68 *
69 * $res = $client->get('drive/v2/rest');
70 */
71 public function __invoke(callable $handler)
72 {
73 return function (RequestInterface $request, array $options) use ($handler) {
74 // Requests using "auth"="scoped" will be authorized.
75 if (!isset($options['auth']) || $options['auth'] !== 'simple') {
76 return $handler($request, $options);
77 }
78
79 $query = Psr7\parse_query($request->getUri()->getQuery());
80 $params = array_merge($query, $this->config);
81 $uri = $request->getUri()->withQuery(Psr7\build_query($params));
82 $request = $request->withUri($uri);
83 return $handler($request, $options);
84 };
85 }
86 }
87