BearerTokenAuthorization.php 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace Aws\Token;
  3. use InvalidArgumentException;
  4. use Psr\Http\Message\RequestInterface;
  5. /**
  6. * Interface used to provide interchangeable strategies for adding authorization
  7. * to requests using the various AWS signature protocols.
  8. */
  9. class BearerTokenAuthorization implements TokenAuthorization
  10. {
  11. /**
  12. * Adds the specified token to a request by adding the required headers.
  13. *
  14. * @param RequestInterface $request Request to sign
  15. * @param TokenInterface $token Token
  16. *
  17. * @return RequestInterface Returns the modified request.
  18. */
  19. public function authorizeRequest(
  20. RequestInterface $request,
  21. TokenInterface $token
  22. ) {
  23. if (empty($token) || empty($token->getToken())) {
  24. throw new InvalidArgumentException(
  25. "Cannot authorize a request with an empty token"
  26. );
  27. }
  28. $accessToken = $token->getToken();
  29. return $request->withHeader('Authorization', "Bearer {$accessToken}");
  30. }
  31. }