설명 없음

SimpleMiddleware.php 2.8KB

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