Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Signature;
3
 
4
use Aws\Exception\UnresolvedSignatureException;
5
use Aws\Token\BearerTokenAuthorization;
6
 
7
/**
8
 * Signature providers.
9
 *
10
 * A signature provider is a function that accepts a version, service, and
11
 * region and returns a {@see SignatureInterface} object on success or NULL if
12
 * no signature can be created from the provided arguments.
13
 *
14
 * You can wrap your calls to a signature provider with the
15
 * {@see SignatureProvider::resolve} function to ensure that a signature object
16
 * is created. If a signature object is not created, then the resolve()
17
 * function will throw a {@see Aws\Exception\UnresolvedSignatureException}.
18
 *
19
 *     use Aws\Signature\SignatureProvider;
20
 *     $provider = SignatureProvider::defaultProvider();
21
 *     // Returns a SignatureInterface or NULL.
22
 *     $signer = $provider('v4', 's3', 'us-west-2');
23
 *     // Returns a SignatureInterface or throws.
24
 *     $signer = SignatureProvider::resolve($provider, 'no', 's3', 'foo');
25
 *
26
 * You can compose multiple providers into a single provider using
27
 * {@see Aws\or_chain}. This function accepts providers as arguments and
28
 * returns a new function that will invoke each provider until a non-null value
29
 * is returned.
30
 *
31
 *     $a = SignatureProvider::defaultProvider();
32
 *     $b = function ($version, $service, $region) {
33
 *         if ($version === 'foo') {
34
 *             return new MyFooSignature();
35
 *         }
36
 *     };
37
 *     $c = \Aws\or_chain($a, $b);
38
 *     $signer = $c('v4', 'abc', '123');     // $a handles this.
39
 *     $signer = $c('foo', 'abc', '123');    // $b handles this.
40
 *     $nullValue = $c('???', 'abc', '123'); // Neither can handle this.
41
 */
42
class SignatureProvider
43
{
44
    private static $s3v4SignedServices = [
45
        's3' => true,
46
        's3control' => true,
1441 ariadna 47
        's3-outposts' => true,
1 efrain 48
        's3-object-lambda' => true,
1441 ariadna 49
        's3express' => true
1 efrain 50
    ];
51
 
52
    /**
53
     * Resolves and signature provider and ensures a non-null return value.
54
     *
55
     * @param callable $provider Provider function to invoke.
56
     * @param string   $version  Signature version.
57
     * @param string   $service  Service name.
58
     * @param string   $region   Region name.
59
     *
60
     * @return SignatureInterface
61
     * @throws UnresolvedSignatureException
62
     */
63
    public static function resolve(callable $provider, $version, $service, $region)
64
    {
65
        $result = $provider($version, $service, $region);
66
        if ($result instanceof SignatureInterface
67
            || $result instanceof BearerTokenAuthorization
68
        ) {
69
            return $result;
70
        }
71
 
72
        throw new UnresolvedSignatureException(
73
            "Unable to resolve a signature for $version/$service/$region.\n"
74
            . "Valid signature versions include v4 and anonymous."
75
        );
76
    }
77
 
78
    /**
79
     * Default SDK signature provider.
80
     *
81
     * @return callable
82
     */
83
    public static function defaultProvider()
84
    {
85
        return self::memoize(self::version());
86
    }
87
 
88
    /**
89
     * Creates a signature provider that caches previously created signature
90
     * objects. The computed cache key is the concatenation of the version,
91
     * service, and region.
92
     *
93
     * @param callable $provider Signature provider to wrap.
94
     *
95
     * @return callable
96
     */
97
    public static function memoize(callable $provider)
98
    {
99
        $cache = [];
100
        return function ($version, $service, $region) use (&$cache, $provider) {
101
            $key = "($version)($service)($region)";
102
            if (!isset($cache[$key])) {
103
                $cache[$key] = $provider($version, $service, $region);
104
            }
105
            return $cache[$key];
106
        };
107
    }
108
 
109
    /**
110
     * Creates signature objects from known signature versions.
111
     *
112
     * This provider currently recognizes the following signature versions:
113
     *
114
     * - v4: Signature version 4.
115
     * - anonymous: Does not sign requests.
116
     *
117
     * @return callable
118
     */
119
    public static function version()
120
    {
121
        return function ($version, $service, $region) {
122
            switch ($version) {
1441 ariadna 123
                case 'v4-s3express':
124
                    return new S3ExpressSignature($service, $region);
1 efrain 125
                case 's3v4':
126
                case 'v4':
127
                    return !empty(self::$s3v4SignedServices[$service])
128
                        ? new S3SignatureV4($service, $region)
129
                        : new SignatureV4($service, $region);
130
                case 'v4a':
131
                    return !empty(self::$s3v4SignedServices[$service])
132
                        ? new S3SignatureV4($service, $region, ['use_v4a' => true])
133
                        : new SignatureV4($service, $region, ['use_v4a' => true]);
134
                case 'v4-unsigned-body':
135
                    return !empty(self::$s3v4SignedServices[$service])
136
                    ? new S3SignatureV4($service, $region, ['unsigned-body' => 'true'])
137
                    : new SignatureV4($service, $region, ['unsigned-body' => 'true']);
138
                case 'bearer':
139
                    return new BearerTokenAuthorization();
140
                case 'anonymous':
141
                    return new AnonymousSignature();
142
                default:
143
                    return null;
144
            }
145
        };
146
    }
147
}