123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace addons\blog\library\aip\lib;
- class AipSampleSigner
- {
- const BCE_AUTH_VERSION = "bce-auth-v1";
- const BCE_PREFIX = 'x-bce-';
-
-
-
-
-
- public static $defaultHeadersToSign;
- public static function __init()
- {
- AipSampleSigner::$defaultHeadersToSign = array(
- "host",
- "content-length",
- "content-type",
- "content-md5",
- );
- }
-
- public static function sign(
- array $credentials,
- $httpMethod,
- $path,
- $headers,
- $params,
- $options = array()
- )
- {
-
- if (!isset($options[AipSignOption::EXPIRATION_IN_SECONDS])) {
-
- $expirationInSeconds = AipSignOption::DEFAULT_EXPIRATION_IN_SECONDS;
- } else {
- $expirationInSeconds = $options[AipSignOption::EXPIRATION_IN_SECONDS];
- }
-
- $accessKeyId = $credentials['ak'];
- $secretAccessKey = $credentials['sk'];
-
- if (!isset($options[AipSignOption::TIMESTAMP])) {
-
- $timestamp = gmdate('Y-m-d\TH:i:s\Z');
- } else {
- $timestamp = $options[AipSignOption::TIMESTAMP];
- }
-
- $authString = AipSampleSigner::BCE_AUTH_VERSION . '/' . $accessKeyId . '/'
- . $timestamp . '/' . $expirationInSeconds;
-
- $signingKey = hash_hmac('sha256', $authString, $secretAccessKey);
-
- $canonicalURI = AipHttpUtil::getCanonicalURIPath($path);
-
- $canonicalQueryString = AipHttpUtil::getCanonicalQueryString($params);
-
- $headersToSign = null;
- if (isset($options[AipSignOption::HEADERS_TO_SIGN])) {
- $headersToSign = $options[AipSignOption::HEADERS_TO_SIGN];
- }
-
- $canonicalHeader = AipHttpUtil::getCanonicalHeaders(
- AipSampleSigner::getHeadersToSign($headers, $headersToSign)
- );
-
- $signedHeaders = '';
- if ($headersToSign !== null) {
- $signedHeaders = strtolower(
- trim(implode(";", $headersToSign))
- );
- }
-
- $canonicalRequest = "$httpMethod\n$canonicalURI\n"
- . "$canonicalQueryString\n$canonicalHeader";
-
- $signature = hash_hmac('sha256', $canonicalRequest, $signingKey);
-
- $authorizationHeader = "$authString/$signedHeaders/$signature";
- return $authorizationHeader;
- }
-
- public static function getHeadersToSign($headers, $headersToSign)
- {
-
- $filter_empty = function ($v) {
- return trim((string)$v) !== '';
- };
- $headers = array_filter($headers, $filter_empty);
-
- $trim_and_lower = function ($str) {
- return strtolower(trim($str));
- };
- $temp = array();
- $process_keys = function ($k, $v) use (&$temp, $trim_and_lower) {
- $temp[$trim_and_lower($k)] = $v;
- };
- array_map($process_keys, array_keys($headers), $headers);
- $headers = $temp;
-
- $header_keys = array_keys($headers);
- $filtered_keys = null;
- if ($headersToSign !== null) {
-
-
- $headersToSign = array_map($trim_and_lower, $headersToSign);
-
- $filtered_keys = array_intersect_key($header_keys, $headersToSign);
- } else {
-
- $filter_by_default = function ($k) {
- return AipSampleSigner::isDefaultHeaderToSign($k);
- };
- $filtered_keys = array_filter($header_keys, $filter_by_default);
- }
-
- return array_intersect_key($headers, array_flip($filtered_keys));
- }
-
- public static function isDefaultHeaderToSign($header)
- {
- $header = strtolower(trim($header));
- if (in_array($header, AipSampleSigner::$defaultHeadersToSign)) {
- return true;
- }
- return substr_compare($header, AipSampleSigner::BCE_PREFIX, 0, strlen(AipSampleSigner::BCE_PREFIX)) == 0;
- }
- }
- AipSampleSigner::__init();
|