|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2022 Google LLC |
| 4 | + * |
| 5 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 6 | + * or more contributor license agreements. See the NOTICE file |
| 7 | + * distributed with this work for additional information |
| 8 | + * regarding copyright ownership. The ASF licenses this file |
| 9 | + * to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance |
| 11 | + * with the License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, |
| 16 | + * software distributed under the License is distributed on an |
| 17 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 18 | + * KIND, either express or implied. See the License for the |
| 19 | + * specific language governing permissions and limitations |
| 20 | + * under the License. |
| 21 | + */ |
| 22 | + |
| 23 | +namespace Google\Tests\AuthHandler; |
| 24 | + |
| 25 | +use Google\AuthHandler\AuthHandlerFactory; |
| 26 | +use Google\Auth\Cache\MemoryCacheItemPool; |
| 27 | +use GuzzleHttp\Client; |
| 28 | +use GuzzleHttp\Psr7\Request; |
| 29 | +use Google\Tests\BaseTest; |
| 30 | + |
| 31 | +class AuthHandlerTest extends BaseTest |
| 32 | +{ |
| 33 | + public function testSetAccessTokenResultsInAuthCacheMiss() |
| 34 | + { |
| 35 | + $client = new Client(); |
| 36 | + $cache = new MemoryCacheItemPool(); |
| 37 | + $authHandler = AuthHandlerFactory::build($cache); |
| 38 | + $scopes = ['scope1', 'scope2']; |
| 39 | + |
| 40 | + // Attach the first token to the HTTP client |
| 41 | + $http1 = $authHandler->attachToken( |
| 42 | + $client, |
| 43 | + ['access_token' => '1234'], |
| 44 | + $scopes |
| 45 | + ); |
| 46 | + |
| 47 | + // Call our middleware and verify the token is set |
| 48 | + $scopedMiddleware = $this->getGoogleAuthMiddleware($http1); |
| 49 | + $request = $scopedMiddleware(new Request('GET', '/'), ['auth' => 'scoped']); |
| 50 | + $this->assertEquals(['Bearer 1234'], $request->getHeader('Authorization')); |
| 51 | + |
| 52 | + // Attach a new token to the HTTP client |
| 53 | + $http2 = $authHandler->attachToken( |
| 54 | + $client, |
| 55 | + ['access_token' => '5678'], |
| 56 | + $scopes |
| 57 | + ); |
| 58 | + |
| 59 | + // Call our middleware and verify the NEW token is set |
| 60 | + $scopedMiddleware = $this->getGoogleAuthMiddleware($http2); |
| 61 | + $request = $scopedMiddleware(new Request('GET', '/'), ['auth' => 'scoped']); |
| 62 | + $this->assertEquals(['Bearer 5678'], $request->getHeader('Authorization')); |
| 63 | + } |
| 64 | + |
| 65 | + private function getGoogleAuthMiddleware(Client $http) |
| 66 | + { |
| 67 | + // All sorts of horrible reflection to get at our middleware |
| 68 | + $handler = $http->getConfig()['handler']; |
| 69 | + $reflectionMethod = new \ReflectionMethod($handler, 'findByName'); |
| 70 | + $reflectionMethod->setAccessible(true); |
| 71 | + $authMiddlewareIdx = $reflectionMethod->invoke($handler, 'google_auth'); |
| 72 | + |
| 73 | + $reflectionProperty = new \ReflectionProperty($handler, 'stack'); |
| 74 | + $reflectionProperty->setAccessible(true); |
| 75 | + $stack = $reflectionProperty->getValue($handler); |
| 76 | + |
| 77 | + $callable = $stack[$authMiddlewareIdx][0]; |
| 78 | + return $callable(function ($request) { |
| 79 | + return $request; |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
0 commit comments