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;
5
use AWS\CRT\Auth\Signable;
6
use AWS\CRT\Auth\SignatureType;
1441 ariadna 7
use AWS\CRT\Auth\SignedBodyHeaderType;
1 efrain 8
use AWS\CRT\Auth\Signing;
9
use AWS\CRT\Auth\SigningAlgorithm;
10
use AWS\CRT\Auth\SigningConfigAWS;
11
use AWS\CRT\Auth\StaticCredentialsProvider;
12
use AWS\CRT\HTTP\Request;
13
use Aws\Exception\CommonRuntimeException;
14
use Aws\Exception\CouldNotCreateChecksumException;
15
use GuzzleHttp\Psr7;
16
use Psr\Http\Message\RequestInterface;
17
 
18
/**
19
 * Signature Version 4
20
 * @link http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
21
 */
22
class SignatureV4 implements SignatureInterface
23
{
24
    use SignatureTrait;
25
    const ISO8601_BASIC = 'Ymd\THis\Z';
26
    const UNSIGNED_PAYLOAD = 'UNSIGNED-PAYLOAD';
27
    const AMZ_CONTENT_SHA256_HEADER = 'X-Amz-Content-Sha256';
28
 
29
    /** @var string */
30
    private $service;
31
 
32
    /** @var string */
33
    protected $region;
34
 
35
    /** @var bool */
36
    private $unsigned;
37
 
38
    /** @var bool */
39
    private $useV4a;
40
 
41
    /**
42
     * The following headers are not signed because signing these headers
43
     * would potentially cause a signature mismatch when sending a request
44
     * through a proxy or if modified at the HTTP client level.
45
     *
46
     * @return array
47
     */
48
    protected function getHeaderBlacklist()
49
    {
50
        return [
51
            'cache-control'         => true,
52
            'content-type'          => true,
53
            'content-length'        => true,
54
            'expect'                => true,
55
            'max-forwards'          => true,
56
            'pragma'                => true,
57
            'range'                 => true,
58
            'te'                    => true,
59
            'if-match'              => true,
60
            'if-none-match'         => true,
61
            'if-modified-since'     => true,
62
            'if-unmodified-since'   => true,
63
            'if-range'              => true,
64
            'accept'                => true,
65
            'authorization'         => true,
66
            'proxy-authorization'   => true,
67
            'from'                  => true,
68
            'referer'               => true,
69
            'user-agent'            => true,
70
            'X-Amz-User-Agent'      => true,
71
            'x-amzn-trace-id'       => true,
72
            'aws-sdk-invocation-id' => true,
73
            'aws-sdk-retry'         => true,
74
        ];
75
    }
76
 
77
    /**
78
     * @param string $service Service name to use when signing
79
     * @param string $region  Region name to use when signing
80
     * @param array $options Array of configuration options used when signing
81
     *      - unsigned-body: Flag to make request have unsigned payload.
82
     *        Unsigned body is used primarily for streaming requests.
83
     */
84
    public function __construct($service, $region, array $options = [])
85
    {
86
        $this->service = $service;
87
        $this->region = $region;
88
        $this->unsigned = isset($options['unsigned-body']) ? $options['unsigned-body'] : false;
89
        $this->useV4a = isset($options['use_v4a']) && $options['use_v4a'] === true;
90
    }
91
 
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function signRequest(
96
        RequestInterface $request,
97
        CredentialsInterface $credentials,
98
        $signingService = null
99
    ) {
100
        $ldt = gmdate(self::ISO8601_BASIC);
101
        $sdt = substr($ldt, 0, 8);
102
        $parsed = $this->parseRequest($request);
103
        $parsed['headers']['X-Amz-Date'] = [$ldt];
104
 
105
        if ($token = $credentials->getSecurityToken()) {
106
            $parsed['headers']['X-Amz-Security-Token'] = [$token];
107
        }
108
        $service = isset($signingService) ? $signingService : $this->service;
109
 
110
        if ($this->useV4a) {
111
            return $this->signWithV4a($credentials, $request, $service);
112
        }
113
 
114
        $cs = $this->createScope($sdt, $this->region, $service);
115
        $payload = $this->getPayload($request);
116
 
117
        if ($payload == self::UNSIGNED_PAYLOAD) {
118
            $parsed['headers'][self::AMZ_CONTENT_SHA256_HEADER] = [$payload];
119
        }
120
 
121
        $context = $this->createContext($parsed, $payload);
122
        $toSign = $this->createStringToSign($ldt, $cs, $context['creq']);
123
        $signingKey = $this->getSigningKey(
124
            $sdt,
125
            $this->region,
126
            $service,
127
            $credentials->getSecretKey()
128
        );
129
        $signature = hash_hmac('sha256', $toSign, $signingKey);
130
        $parsed['headers']['Authorization'] = [
131
            "AWS4-HMAC-SHA256 "
132
            . "Credential={$credentials->getAccessKeyId()}/{$cs}, "
133
            . "SignedHeaders={$context['headers']}, Signature={$signature}"
134
        ];
135
 
136
        return $this->buildRequest($parsed);
137
    }
138
 
139
    /**
140
     * Get the headers that were used to pre-sign the request.
141
     * Used for the X-Amz-SignedHeaders header.
142
     *
143
     * @param array $headers
144
     * @return array
145
     */
146
    private function getPresignHeaders(array $headers)
147
    {
148
        $presignHeaders = [];
149
        $blacklist = $this->getHeaderBlacklist();
150
        foreach ($headers as $name => $value) {
151
            $lName = strtolower($name);
152
            if (!isset($blacklist[$lName])
153
                && $name !== self::AMZ_CONTENT_SHA256_HEADER
154
            ) {
155
                $presignHeaders[] = $lName;
156
            }
157
        }
158
        return $presignHeaders;
159
    }
160
 
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function presign(
165
        RequestInterface $request,
166
        CredentialsInterface $credentials,
167
        $expires,
168
        array $options = []
169
    ) {
170
        $startTimestamp = isset($options['start_time'])
171
            ? $this->convertToTimestamp($options['start_time'], null)
172
            : time();
173
        $expiresTimestamp = $this->convertToTimestamp($expires, $startTimestamp);
174
 
175
        if ($this->useV4a) {
176
            return $this->presignWithV4a(
177
                $request,
178
                $credentials,
179
                $this->convertExpires($expiresTimestamp, $startTimestamp)
180
            );
181
        }
182
 
183
        $parsed = $this->createPresignedRequest($request, $credentials);
184
 
185
        $payload = $this->getPresignedPayload($request);
186
        $httpDate = gmdate(self::ISO8601_BASIC, $startTimestamp);
187
        $shortDate = substr($httpDate, 0, 8);
188
        $scope = $this->createScope($shortDate, $this->region, $this->service);
189
        $credential = $credentials->getAccessKeyId() . '/' . $scope;
190
        if ($credentials->getSecurityToken()) {
191
            unset($parsed['headers']['X-Amz-Security-Token']);
192
        }
193
        $parsed['query']['X-Amz-Algorithm'] = 'AWS4-HMAC-SHA256';
194
        $parsed['query']['X-Amz-Credential'] = $credential;
195
        $parsed['query']['X-Amz-Date'] = gmdate('Ymd\THis\Z', $startTimestamp);
196
        $parsed['query']['X-Amz-SignedHeaders'] = implode(';', $this->getPresignHeaders($parsed['headers']));
197
        $parsed['query']['X-Amz-Expires'] = $this->convertExpires($expiresTimestamp, $startTimestamp);
198
        $context = $this->createContext($parsed, $payload);
199
        $stringToSign = $this->createStringToSign($httpDate, $scope, $context['creq']);
200
        $key = $this->getSigningKey(
201
            $shortDate,
202
            $this->region,
203
            $this->service,
204
            $credentials->getSecretKey()
205
        );
206
        $parsed['query']['X-Amz-Signature'] = hash_hmac('sha256', $stringToSign, $key);
207
 
208
        return $this->buildRequest($parsed);
209
    }
210
 
211
    /**
212
     * Converts a POST request to a GET request by moving POST fields into the
213
     * query string.
214
     *
215
     * Useful for pre-signing query protocol requests.
216
     *
217
     * @param RequestInterface $request Request to clone
218
     *
219
     * @return RequestInterface
220
     * @throws \InvalidArgumentException if the method is not POST
221
     */
222
    public static function convertPostToGet(RequestInterface $request, $additionalQueryParams = "")
223
    {
224
        if ($request->getMethod() !== 'POST') {
225
            throw new \InvalidArgumentException('Expected a POST request but '
226
                . 'received a ' . $request->getMethod() . ' request.');
227
        }
228
 
229
        $sr = $request->withMethod('GET')
230
            ->withBody(Psr7\Utils::streamFor(''))
231
            ->withoutHeader('Content-Type')
232
            ->withoutHeader('Content-Length');
233
 
234
        // Move POST fields to the query if they are present
235
        if ($request->getHeaderLine('Content-Type') === 'application/x-www-form-urlencoded') {
236
            $body = (string) $request->getBody() . $additionalQueryParams;
237
            $sr = $sr->withUri($sr->getUri()->withQuery($body));
238
        }
239
 
240
        return $sr;
241
    }
242
 
243
    protected function getPayload(RequestInterface $request)
244
    {
245
        if ($this->unsigned && $request->getUri()->getScheme() == 'https') {
246
            return self::UNSIGNED_PAYLOAD;
247
        }
248
        // Calculate the request signature payload
249
        if ($request->hasHeader(self::AMZ_CONTENT_SHA256_HEADER)) {
250
            // Handle streaming operations (e.g. Glacier.UploadArchive)
251
            return $request->getHeaderLine(self::AMZ_CONTENT_SHA256_HEADER);
252
        }
253
 
254
        if (!$request->getBody()->isSeekable()) {
255
            throw new CouldNotCreateChecksumException('sha256');
256
        }
257
 
258
        try {
259
            return Psr7\Utils::hash($request->getBody(), 'sha256');
260
        } catch (\Exception $e) {
261
            throw new CouldNotCreateChecksumException('sha256', $e);
262
        }
263
    }
264
 
265
    protected function getPresignedPayload(RequestInterface $request)
266
    {
267
        return $this->getPayload($request);
268
    }
269
 
270
    protected function createCanonicalizedPath($path)
271
    {
272
        $doubleEncoded = rawurlencode(ltrim($path, '/'));
273
 
274
        return '/' . str_replace('%2F', '/', $doubleEncoded);
275
    }
276
 
277
    private function createStringToSign($longDate, $credentialScope, $creq)
278
    {
279
        $hash = hash('sha256', $creq);
280
 
281
        return "AWS4-HMAC-SHA256\n{$longDate}\n{$credentialScope}\n{$hash}";
282
    }
283
 
284
    private function createPresignedRequest(
285
        RequestInterface $request,
286
        CredentialsInterface $credentials
287
    ) {
288
        $parsedRequest = $this->parseRequest($request);
289
 
290
        // Make sure to handle temporary credentials
291
        if ($token = $credentials->getSecurityToken()) {
292
            $parsedRequest['headers']['X-Amz-Security-Token'] = [$token];
293
        }
294
 
295
        return $this->moveHeadersToQuery($parsedRequest);
296
    }
297
 
298
    /**
299
     * @param array  $parsedRequest
300
     * @param string $payload Hash of the request payload
301
     * @return array Returns an array of context information
302
     */
303
    private function createContext(array $parsedRequest, $payload)
304
    {
305
        $blacklist = $this->getHeaderBlacklist();
306
 
307
        // Normalize the path as required by SigV4
308
        $canon = $parsedRequest['method'] . "\n"
309
            . $this->createCanonicalizedPath($parsedRequest['path']) . "\n"
310
            . $this->getCanonicalizedQuery($parsedRequest['query']) . "\n";
311
 
312
        // Case-insensitively aggregate all of the headers.
313
        $aggregate = [];
314
        foreach ($parsedRequest['headers'] as $key => $values) {
315
            $key = strtolower($key);
316
            if (!isset($blacklist[$key])) {
317
                foreach ($values as $v) {
318
                    $aggregate[$key][] = $v;
319
                }
320
            }
321
        }
322
 
323
        ksort($aggregate);
324
        $canonHeaders = [];
325
        foreach ($aggregate as $k => $v) {
326
            if (count($v) > 0) {
327
                sort($v);
328
            }
329
            $canonHeaders[] = $k . ':' . preg_replace('/\s+/', ' ', implode(',', $v));
330
        }
331
 
332
        $signedHeadersString = implode(';', array_keys($aggregate));
333
        $canon .= implode("\n", $canonHeaders) . "\n\n"
334
            . $signedHeadersString . "\n"
335
            . $payload;
336
 
337
        return ['creq' => $canon, 'headers' => $signedHeadersString];
338
    }
339
 
340
    private function getCanonicalizedQuery(array $query)
341
    {
342
        unset($query['X-Amz-Signature']);
343
 
344
        if (!$query) {
345
            return '';
346
        }
347
 
348
        $qs = '';
349
        ksort($query);
350
        foreach ($query as $k => $v) {
351
            if (!is_array($v)) {
352
                $qs .= rawurlencode($k) . '=' . rawurlencode($v !== null ? $v : '') . '&';
353
            } else {
354
                sort($v);
355
                foreach ($v as $value) {
356
                    $qs .= rawurlencode($k) . '=' . rawurlencode($value !== null ? $value : '') . '&';
357
                }
358
            }
359
        }
360
 
361
        return substr($qs, 0, -1);
362
    }
363
 
364
    private function convertToTimestamp($dateValue, $relativeTimeBase = null)
365
    {
366
        if ($dateValue instanceof \DateTimeInterface) {
367
            $timestamp = $dateValue->getTimestamp();
368
        } elseif (!is_numeric($dateValue)) {
369
            $timestamp = strtotime($dateValue,
370
                $relativeTimeBase === null ? time() : $relativeTimeBase
371
            );
372
        } else {
373
            $timestamp = $dateValue;
374
        }
375
 
376
        return $timestamp;
377
    }
378
 
379
    private function convertExpires($expiresTimestamp, $startTimestamp)
380
    {
381
        $duration = $expiresTimestamp - $startTimestamp;
382
 
383
        // Ensure that the duration of the signature is not longer than a week
384
        if ($duration > 604800) {
385
            throw new \InvalidArgumentException('The expiration date of a '
386
                . 'signature version 4 presigned URL must be less than one '
387
                . 'week');
388
        }
389
 
390
        return $duration;
391
    }
392
 
393
    private function moveHeadersToQuery(array $parsedRequest)
394
    {
395
        //x-amz-user-agent shouldn't be put in a query param
396
        unset($parsedRequest['headers']['X-Amz-User-Agent']);
397
 
398
        foreach ($parsedRequest['headers'] as $name => $header) {
399
            $lname = strtolower($name);
400
            if (substr($lname, 0, 5) == 'x-amz') {
401
                $parsedRequest['query'][$name] = $header;
402
            }
403
            $blacklist = $this->getHeaderBlacklist();
404
            if (isset($blacklist[$lname])
405
                || $lname === strtolower(self::AMZ_CONTENT_SHA256_HEADER)
406
            ) {
407
                unset($parsedRequest['headers'][$name]);
408
            }
409
        }
410
 
411
        return $parsedRequest;
412
    }
413
 
414
    private function parseRequest(RequestInterface $request)
415
    {
416
        // Clean up any previously set headers.
417
        /** @var RequestInterface $request */
418
        $request = $request
419
            ->withoutHeader('X-Amz-Date')
420
            ->withoutHeader('Date')
421
            ->withoutHeader('Authorization');
422
        $uri = $request->getUri();
423
 
424
        return [
425
            'method'  => $request->getMethod(),
426
            'path'    => $uri->getPath(),
427
            'query'   => Psr7\Query::parse($uri->getQuery()),
428
            'uri'     => $uri,
429
            'headers' => $request->getHeaders(),
430
            'body'    => $request->getBody(),
431
            'version' => $request->getProtocolVersion()
432
        ];
433
    }
434
 
435
    private function buildRequest(array $req)
436
    {
437
        if ($req['query']) {
438
            $req['uri'] = $req['uri']->withQuery(Psr7\Query::build($req['query']));
439
        }
440
 
441
        return new Psr7\Request(
442
            $req['method'],
443
            $req['uri'],
444
            $req['headers'],
445
            $req['body'],
446
            $req['version']
447
        );
448
    }
449
 
1441 ariadna 450
    protected function verifyCRTLoaded()
1 efrain 451
    {
452
        if (!extension_loaded('awscrt')) {
453
            throw new CommonRuntimeException(
454
                "AWS Common Runtime for PHP is required to use Signature V4A"
455
                . ".  Please install it using the instructions found at"
456
                . " https://github.com/aws/aws-sdk-php/blob/master/CRT_INSTRUCTIONS.md"
457
            );
458
        }
459
    }
460
 
1441 ariadna 461
    protected function createCRTStaticCredentialsProvider($credentials)
1 efrain 462
    {
463
        return new StaticCredentialsProvider([
464
            'access_key_id' => $credentials->getAccessKeyId(),
465
            'secret_access_key' => $credentials->getSecretKey(),
466
            'session_token' => $credentials->getSecurityToken(),
467
        ]);
468
    }
469
 
470
    private function removeIllegalV4aHeaders(&$request)
471
    {
1441 ariadna 472
        static $illegalV4aHeaders = [
1 efrain 473
            self::AMZ_CONTENT_SHA256_HEADER,
1441 ariadna 474
            'aws-sdk-invocation-id',
475
            'aws-sdk-retry',
476
            'x-amz-region-set',
477
            'transfer-encoding'
1 efrain 478
        ];
479
        $storedHeaders = [];
480
 
481
        foreach ($illegalV4aHeaders as $header) {
1441 ariadna 482
            if ($request->hasHeader($header)) {
1 efrain 483
                $storedHeaders[$header] = $request->getHeader($header);
484
                $request = $request->withoutHeader($header);
485
            }
486
        }
487
 
488
        return $storedHeaders;
489
    }
490
 
491
    private function CRTRequestFromGuzzleRequest($request)
492
    {
493
        return new Request(
494
            $request->getMethod(),
495
            (string) $request->getUri(),
496
            [], //leave empty as the query is parsed from the uri object
497
            array_map(function ($header) {return $header[0];}, $request->getHeaders())
498
        );
499
    }
500
 
501
    /**
502
     * @param CredentialsInterface $credentials
503
     * @param RequestInterface $request
504
     * @param $signingService
1441 ariadna 505
     * @param SigningConfigAWS|null $signingConfig
1 efrain 506
     * @return RequestInterface
507
     */
1441 ariadna 508
    protected function signWithV4a(
509
        CredentialsInterface $credentials,
510
        RequestInterface $request,
511
        $signingService,
512
        ?SigningConfigAWS $signingConfig = null
513
    ){
1 efrain 514
        $this->verifyCRTLoaded();
1441 ariadna 515
        $signingConfig = $signingConfig ?? new SigningConfigAWS([
1 efrain 516
            'algorithm' => SigningAlgorithm::SIGv4_ASYMMETRIC,
517
            'signature_type' => SignatureType::HTTP_REQUEST_HEADERS,
1441 ariadna 518
            'credentials_provider' => $this->createCRTStaticCredentialsProvider($credentials),
1 efrain 519
            'signed_body_value' => $this->getPayload($request),
1441 ariadna 520
            'should_normalize_uri_path' => true,
521
            'use_double_uri_encode' => true,
522
            'region' => $this->region,
1 efrain 523
            'service' => $signingService,
524
            'date' => time(),
525
        ]);
526
 
527
        $removedIllegalHeaders = $this->removeIllegalV4aHeaders($request);
528
        $http_request = $this->CRTRequestFromGuzzleRequest($request);
529
 
530
        Signing::signRequestAws(
531
            Signable::fromHttpRequest($http_request),
532
            $signingConfig, function ($signing_result, $error_code) use (&$http_request) {
533
            $signing_result->applyToHttpRequest($http_request);
534
        });
535
        foreach ($removedIllegalHeaders as $header => $value) {
536
            $request = $request->withHeader($header, $value);
537
        }
538
 
539
        $sigV4AHeaders = $http_request->headers();
540
        foreach ($sigV4AHeaders->toArray() as $h => $v) {
541
            $request = $request->withHeader($h, $v);
542
        }
543
 
544
        return $request;
545
    }
546
 
547
    protected function presignWithV4a(
548
        RequestInterface $request,
549
        CredentialsInterface $credentials,
550
        $expires
551
    )
552
    {
553
        $this->verifyCRTLoaded();
554
        $credentials_provider = $this->createCRTStaticCredentialsProvider($credentials);
555
        $signingConfig = new SigningConfigAWS([
556
            'algorithm' => SigningAlgorithm::SIGv4_ASYMMETRIC,
557
            'signature_type' => SignatureType::HTTP_REQUEST_QUERY_PARAMS,
558
            'credentials_provider' => $credentials_provider,
559
            'signed_body_value' => $this->getPresignedPayload($request),
560
            'region' => "*",
561
            'service' => $this->service,
562
            'date' => time(),
563
            'expiration_in_seconds' => $expires
564
        ]);
565
 
566
        $this->removeIllegalV4aHeaders($request);
567
        foreach ($this->getHeaderBlacklist() as $headerName => $headerValue) {
568
            if ($request->hasHeader($headerName)) {
569
                $request = $request->withoutHeader($headerName);
570
            }
571
        }
572
 
573
        $http_request = $this->CRTRequestFromGuzzleRequest($request);
574
        Signing::signRequestAws(
575
            Signable::fromHttpRequest($http_request),
576
            $signingConfig, function ($signing_result, $error_code) use (&$http_request) {
577
            $signing_result->applyToHttpRequest($http_request);
578
        });
579
 
580
        return $request->withUri(
581
            new Psr7\Uri($http_request->pathAndQuery())
582
        );
583
    }
584
}