AuthSchemeResolver.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Aws\Auth;
  3. use Aws\Auth\Exception\UnresolvedAuthSchemeException;
  4. use Aws\Identity\AwsCredentialIdentity;
  5. use Aws\Identity\BearerTokenIdentity;
  6. use GuzzleHttp\Promise\PromiseInterface;
  7. /**
  8. * Houses logic for selecting an auth scheme modeled in a service's `auth` trait.
  9. * The `auth` trait can be modeled either in a service's metadata, or at the operation level.
  10. */
  11. class AuthSchemeResolver implements AuthSchemeResolverInterface
  12. {
  13. const UNSIGNED_BODY = '-unsigned-body';
  14. /**
  15. * @var string[] Default mapping of modeled auth trait auth schemes
  16. * to the SDK's supported signature versions.
  17. */
  18. private static $defaultAuthSchemeMap = [
  19. 'aws.auth#sigv4' => 'v4',
  20. 'aws.auth#sigv4a' => 'v4a',
  21. 'smithy.api#httpBearerAuth' => 'bearer',
  22. 'smithy.api#noAuth' => 'anonymous'
  23. ];
  24. /**
  25. * @var array Mapping of auth schemes to signature versions used in
  26. * resolving a signature version.
  27. */
  28. private $authSchemeMap;
  29. private $tokenProvider;
  30. private $credentialProvider;
  31. public function __construct(
  32. callable $credentialProvider,
  33. callable $tokenProvider = null,
  34. array $authSchemeMap = []
  35. ){
  36. $this->credentialProvider = $credentialProvider;
  37. $this->tokenProvider = $tokenProvider;
  38. $this->authSchemeMap = empty($authSchemeMap)
  39. ? self::$defaultAuthSchemeMap
  40. : $authSchemeMap;
  41. }
  42. /**
  43. * Accepts a priority-ordered list of auth schemes and an Identity
  44. * and selects the first compatible auth schemes, returning a normalized
  45. * signature version. For example, based on the default auth scheme mapping,
  46. * if `aws.auth#sigv4` is selected, `v4` will be returned.
  47. *
  48. * @param array $authSchemes
  49. * @param $identity
  50. *
  51. * @return string
  52. * @throws UnresolvedAuthSchemeException
  53. */
  54. public function selectAuthScheme(
  55. array $authSchemes,
  56. array $args = []
  57. ): string
  58. {
  59. $failureReasons = [];
  60. foreach($authSchemes as $authScheme) {
  61. $normalizedAuthScheme = $this->authSchemeMap[$authScheme] ?? $authScheme;
  62. if ($this->isCompatibleAuthScheme($normalizedAuthScheme)) {
  63. if ($normalizedAuthScheme === 'v4' && !empty($args['unsigned_payload'])) {
  64. return $normalizedAuthScheme . self::UNSIGNED_BODY;
  65. }
  66. return $normalizedAuthScheme;
  67. } else {
  68. $failureReasons[] = $this->getIncompatibilityMessage($normalizedAuthScheme);
  69. }
  70. }
  71. throw new UnresolvedAuthSchemeException(
  72. 'Could not resolve an authentication scheme: '
  73. . implode('; ', $failureReasons)
  74. );
  75. }
  76. /**
  77. * Determines compatibility based on either Identity or the availability
  78. * of the CRT extension.
  79. *
  80. * @param $authScheme
  81. *
  82. * @return bool
  83. */
  84. private function isCompatibleAuthScheme($authScheme): bool
  85. {
  86. switch ($authScheme) {
  87. case 'v4':
  88. case 'anonymous':
  89. return $this->hasAwsCredentialIdentity();
  90. case 'v4a':
  91. return extension_loaded('awscrt') && $this->hasAwsCredentialIdentity();
  92. case 'bearer':
  93. return $this->hasBearerTokenIdentity();
  94. default:
  95. return false;
  96. }
  97. }
  98. /**
  99. * Provides incompatibility messages in the event an incompatible auth scheme
  100. * is encountered.
  101. *
  102. * @param $authScheme
  103. *
  104. * @return string
  105. */
  106. private function getIncompatibilityMessage($authScheme): string
  107. {
  108. switch ($authScheme) {
  109. case 'v4':
  110. return 'Signature V4 requires AWS credentials for request signing';
  111. case 'anonymous':
  112. return 'Anonymous signatures require AWS credentials for request signing';
  113. case 'v4a':
  114. return 'The aws-crt-php extension and AWS credentials are required to use Signature V4A';
  115. case 'bearer':
  116. return 'Bearer token credentials must be provided to use Bearer authentication';
  117. default:
  118. return "The service does not support `{$authScheme}` authentication.";
  119. }
  120. }
  121. /**
  122. * @return bool
  123. */
  124. private function hasAwsCredentialIdentity(): bool
  125. {
  126. $fn = $this->credentialProvider;
  127. $result = $fn();
  128. if ($result instanceof PromiseInterface) {
  129. return $result->wait() instanceof AwsCredentialIdentity;
  130. }
  131. return $result instanceof AwsCredentialIdentity;
  132. }
  133. /**
  134. * @return bool
  135. */
  136. private function hasBearerTokenIdentity(): bool
  137. {
  138. if ($this->tokenProvider) {
  139. $fn = $this->tokenProvider;
  140. $result = $fn();
  141. if ($result instanceof PromiseInterface) {
  142. return $result->wait() instanceof BearerTokenIdentity;
  143. }
  144. return $result instanceof BearerTokenIdentity;
  145. }
  146. return false;
  147. }
  148. }