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\FetchAuthTokenCache;
|
---|
21 | use Google\Auth\FetchAuthTokenInterface;
|
---|
22 | use Google\Auth\GetQuotaProjectInterface;
|
---|
23 | use Google\Auth\UpdateMetadataInterface;
|
---|
24 | use GuzzleHttp\Psr7\Utils;
|
---|
25 | use Psr\Http\Message\RequestInterface;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * AuthTokenMiddleware is a Guzzle Middleware that adds an Authorization header
|
---|
29 | * provided by an object implementing FetchAuthTokenInterface.
|
---|
30 | *
|
---|
31 | * The FetchAuthTokenInterface#fetchAuthToken is used to obtain a hash; one of
|
---|
32 | * the values value in that hash is added as the authorization header.
|
---|
33 | *
|
---|
34 | * Requests will be accessed with the authorization header:
|
---|
35 | *
|
---|
36 | * 'authorization' 'Bearer <value of auth_token>'
|
---|
37 | */
|
---|
38 | class AuthTokenMiddleware
|
---|
39 | {
|
---|
40 | /**
|
---|
41 | * @var callable
|
---|
42 | */
|
---|
43 | private $httpHandler;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * It must be an implementation of FetchAuthTokenInterface.
|
---|
47 | * It may also implement UpdateMetadataInterface allowing direct
|
---|
48 | * retrieval of auth related headers
|
---|
49 | * @var FetchAuthTokenInterface
|
---|
50 | */
|
---|
51 | private $fetcher;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * @var ?callable
|
---|
55 | */
|
---|
56 | private $tokenCallback;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Creates a new AuthTokenMiddleware.
|
---|
60 | *
|
---|
61 | * @param FetchAuthTokenInterface $fetcher is used to fetch the auth token
|
---|
62 | * @param callable|null $httpHandler (optional) callback which delivers psr7 request
|
---|
63 | * @param callable|null $tokenCallback (optional) function to be called when a new token is fetched.
|
---|
64 | */
|
---|
65 | public function __construct(
|
---|
66 | FetchAuthTokenInterface $fetcher,
|
---|
67 | ?callable $httpHandler = null,
|
---|
68 | ?callable $tokenCallback = null
|
---|
69 | ) {
|
---|
70 | $this->fetcher = $fetcher;
|
---|
71 | $this->httpHandler = $httpHandler;
|
---|
72 | $this->tokenCallback = $tokenCallback;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Updates the request with an Authorization header when auth is 'google_auth'.
|
---|
77 | *
|
---|
78 | * use Google\Auth\Middleware\AuthTokenMiddleware;
|
---|
79 | * use Google\Auth\OAuth2;
|
---|
80 | * use GuzzleHttp\Client;
|
---|
81 | * use GuzzleHttp\HandlerStack;
|
---|
82 | *
|
---|
83 | * $config = [..<oauth config param>.];
|
---|
84 | * $oauth2 = new OAuth2($config)
|
---|
85 | * $middleware = new AuthTokenMiddleware($oauth2);
|
---|
86 | * $stack = HandlerStack::create();
|
---|
87 | * $stack->push($middleware);
|
---|
88 | *
|
---|
89 | * $client = new Client([
|
---|
90 | * 'handler' => $stack,
|
---|
91 | * 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
|
---|
92 | * 'auth' => 'google_auth' // authorize all requests
|
---|
93 | * ]);
|
---|
94 | *
|
---|
95 | * $res = $client->get('myproject/taskqueues/myqueue');
|
---|
96 | *
|
---|
97 | * @param callable $handler
|
---|
98 | * @return \Closure
|
---|
99 | */
|
---|
100 | public function __invoke(callable $handler)
|
---|
101 | {
|
---|
102 | return function (RequestInterface $request, array $options) use ($handler) {
|
---|
103 | // Requests using "auth"="google_auth" will be authorized.
|
---|
104 | if (!isset($options['auth']) || $options['auth'] !== 'google_auth') {
|
---|
105 | return $handler($request, $options);
|
---|
106 | }
|
---|
107 |
|
---|
108 | $request = $this->addAuthHeaders($request);
|
---|
109 |
|
---|
110 | if ($quotaProject = $this->getQuotaProject()) {
|
---|
111 | $request = $request->withHeader(
|
---|
112 | GetQuotaProjectInterface::X_GOOG_USER_PROJECT_HEADER,
|
---|
113 | $quotaProject
|
---|
114 | );
|
---|
115 | }
|
---|
116 |
|
---|
117 | return $handler($request, $options);
|
---|
118 | };
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Adds auth related headers to the request.
|
---|
123 | *
|
---|
124 | * @param RequestInterface $request
|
---|
125 | * @return RequestInterface
|
---|
126 | */
|
---|
127 | private function addAuthHeaders(RequestInterface $request)
|
---|
128 | {
|
---|
129 | if (!$this->fetcher instanceof UpdateMetadataInterface ||
|
---|
130 | ($this->fetcher instanceof FetchAuthTokenCache &&
|
---|
131 | !$this->fetcher->getFetcher() instanceof UpdateMetadataInterface)
|
---|
132 | ) {
|
---|
133 | $token = $this->fetcher->fetchAuthToken();
|
---|
134 | $request = $request->withHeader(
|
---|
135 | 'authorization',
|
---|
136 | 'Bearer ' . ($token['access_token'] ?? $token['id_token'] ?? '')
|
---|
137 | );
|
---|
138 | } else {
|
---|
139 | $headers = $this->fetcher->updateMetadata($request->getHeaders(), null, $this->httpHandler);
|
---|
140 | $request = Utils::modifyRequest($request, ['set_headers' => $headers]);
|
---|
141 | }
|
---|
142 |
|
---|
143 | if ($this->tokenCallback && ($token = $this->fetcher->getLastReceivedToken())) {
|
---|
144 | if (array_key_exists('access_token', $token)) {
|
---|
145 | call_user_func($this->tokenCallback, $this->fetcher->getCacheKey(), $token['access_token']);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | return $request;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * @return string|null
|
---|
154 | */
|
---|
155 | private function getQuotaProject()
|
---|
156 | {
|
---|
157 | if ($this->fetcher instanceof GetQuotaProjectInterface) {
|
---|
158 | return $this->fetcher->getQuotaProject();
|
---|
159 | }
|
---|
160 |
|
---|
161 | return null;
|
---|
162 | }
|
---|
163 | }
|
---|