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\Credentials\CredentialsInterface;
1441 ariadna 5
use AWS\CRT\Auth\SignatureType;
6
use AWS\CRT\Auth\SigningAlgorithm;
7
use AWS\CRT\Auth\SigningConfigAWS;
1 efrain 8
use Psr\Http\Message\RequestInterface;
9
 
10
/**
11
 * Amazon S3 signature version 4 support.
12
 */
13
class S3SignatureV4 extends SignatureV4
14
{
15
    /**
16
     * S3-specific signing logic
17
     *
18
     * {@inheritdoc}
19
     */
20
    use SignatureTrait;
21
 
22
    public function signRequest(
23
        RequestInterface $request,
24
        CredentialsInterface $credentials,
25
        $signingService = null
26
    ) {
27
        // Always add a x-amz-content-sha-256 for data integrity
28
        if (!$request->hasHeader('x-amz-content-sha256')) {
29
            $request = $request->withHeader(
30
                'x-amz-content-sha256',
31
                $this->getPayload($request)
32
            );
33
        }
34
        $useCrt =
35
            strpos($request->getUri()->getHost(), "accesspoint.s3-global")
36
            !== false;
37
        if (!$useCrt) {
38
            if (strpos($request->getUri()->getHost(), "s3-object-lambda")) {
39
                return parent::signRequest($request, $credentials, "s3-object-lambda");
40
            }
41
            return parent::signRequest($request, $credentials);
42
        }
43
        $signingService = $signingService ?: 's3';
44
        return $this->signWithV4a($credentials, $request, $signingService);
45
    }
46
 
47
    /**
1441 ariadna 48
     * @param CredentialsInterface $credentials
49
     * @param RequestInterface $request
50
     * @param $signingService
51
     * @param SigningConfigAWS|null $signingConfig
52
     * @return RequestInterface
53
     *
54
     * Instantiates a separate sigv4a signing config.  All services except S3
55
     * use double encoding.  All services except S3 require path normalization.
56
     */
57
    protected function signWithV4a(
58
        CredentialsInterface $credentials,
59
        RequestInterface $request,
60
        $signingService,
61
        ?SigningConfigAWS $signingConfig = null
62
    ){
63
        $this->verifyCRTLoaded();
64
        $credentials_provider = $this->createCRTStaticCredentialsProvider($credentials);
65
        $signingConfig = new SigningConfigAWS([
66
            'algorithm' => SigningAlgorithm::SIGv4_ASYMMETRIC,
67
            'signature_type' => SignatureType::HTTP_REQUEST_HEADERS,
68
            'credentials_provider' => $credentials_provider,
69
            'signed_body_value' => $this->getPayload($request),
70
            'region' => $this->region,
71
            'should_normalize_uri_path' => false,
72
            'use_double_uri_encode' => false,
73
            'service' => $signingService,
74
            'date' => time(),
75
        ]);
76
 
77
        return parent::signWithV4a($credentials, $request, $signingService, $signingConfig);
78
    }
79
 
80
    /**
1 efrain 81
     * Always add a x-amz-content-sha-256 for data integrity.
82
     *
83
     * {@inheritdoc}
84
     */
85
    public function presign(
86
        RequestInterface $request,
87
        CredentialsInterface $credentials,
88
        $expires,
89
        array $options = []
90
    ) {
91
        if (!$request->hasHeader('x-amz-content-sha256')) {
92
            $request = $request->withHeader(
93
                'X-Amz-Content-Sha256',
94
                $this->getPresignedPayload($request)
95
            );
96
        }
1441 ariadna 97
 
1 efrain 98
        if (strpos($request->getUri()->getHost(), "accesspoint.s3-global")) {
99
            $request = $request->withHeader("x-amz-region-set", "*");
100
        }
101
 
102
        return parent::presign($request, $credentials, $expires, $options);
103
    }
104
 
105
    /**
106
     * Override used to allow pre-signed URLs to be created for an
107
     * in-determinate request payload.
108
     */
109
    protected function getPresignedPayload(RequestInterface $request)
110
    {
111
        return SignatureV4::UNSIGNED_PAYLOAD;
112
    }
113
 
114
    /**
115
     * Amazon S3 does not double-encode the path component in the canonical request
116
     */
117
    protected function createCanonicalizedPath($path)
118
    {
119
        // Only remove one slash in case of keys that have a preceding slash
120
        if (substr($path, 0, 1) === '/') {
121
            $path = substr($path, 1);
122
        }
123
        return '/' . $path;
124
    }
125
}