Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Token/BedrockTokenProvider.php

raw symbols SCIP
1 <?php
2
3 namespace Aws\Token;
4
5 use Aws\Configuration\ConfigurationResolver;
6 use Aws\Exception\TokenException;
7 use GuzzleHttp\Promise;
8
9 /**
10 * Token provider for Bedrock that sources bearer tokens from environment variables.
11 */
12 class BedrockTokenProvider extends TokenProvider
13 {
14 /** @var string used to resolve the AWS_BEARER_TOKEN_BEDROCK env var */
15 public const TOKEN_ENV_KEY = 'bearer_token_bedrock';
16 public const BEARER_AUTH = 'smithy.api#httpBearerAuth';
17
18 /**
19 * Create a default Bedrock token provider that checks for a bearer token
20 * in the AWS_BEARER_TOKEN_BEDROCK environment variable.
21 *
22 * This provider is automatically wrapped in a memoize function that caches
23 * previously provided tokens.
24 *
25 * @param array $config Optional array of token provider options.
26 *
27 * @return callable
28 */
29 public static function defaultProvider(array $config = []): callable
30 {
31 $defaultChain = ['env' => self::env(self::TOKEN_ENV_KEY)];
32
33 return self::memoize(
34 call_user_func_array(
35 [TokenProvider::class, 'chain'],
36 array_values($defaultChain)
37 )
38 );
39 }
40
41 /**
42 * Token provider that creates a token from an environment variable.
43 *
44 * @param string $configKey The configuration key that will be transformed
45 * to an environment variable name by ConfigurationResolver
46 *
47 * @return callable
48 */
49 public static function env(string $configKey): callable
50 {
51 return static function () use ($configKey) {
52 $tokenValue = ConfigurationResolver::env($configKey);
53 if (empty($tokenValue)) {
54 return Promise\Create::rejectionFor(
55 new TokenException(
56 "No token found in environment variable " .
57 ConfigurationResolver::$envPrefix . strtoupper($configKey)
58 )
59 );
60 }
61
62 return Promise\Create::promiseFor(new Token($tokenValue));
63 };
64 }
65
66 /**
67 * Create a token provider from a raw token value string.
68 * Bedrock bearer tokens sourced from env do not have an expiration
69 *
70 * @param string $tokenValue The bearer token value
71 *
72 * @return callable
73 */
74 public static function fromTokenValue(
75 string $tokenValue,
76 ?TokenSource $source = null
77 ): callable
78 {
79 $token = new Token($tokenValue, null, $source);
80 return self::fromToken($token);
81 }
82
83 /**
84 * Create a Bedrock token provider if the service is 'bedrock' and a token is available.
85 * Sets auth scheme preference to `bearer` auth.
86 *
87 * @param array $args Configuration arguments containing 'config' array
88 *
89 * @return callable|null Returns a token provider if conditions are met, null otherwise
90 */
91 public static function createIfAvailable(array &$args): ?callable
92 {
93 $tokenValue = ConfigurationResolver::env(self::TOKEN_ENV_KEY);
94
95 if ($tokenValue) {
96 $authSchemePreference = $args['config']['auth_scheme_preference'] ?? [];
97 array_unshift($authSchemePreference, self::BEARER_AUTH);
98 $args['config']['auth_scheme_preference'] = $authSchemePreference;
99
100 return self::fromTokenValue($tokenValue, TokenSource::BEARER_SERVICE_ENV_VARS);
101 }
102
103 return null;
104 }
105 }