public function __construct(SettingsInterface $settings) { $this->settings = $settings; $this->storageUrl = 'https://' . $this->settings->getName() . '.blob.core.windows.net'; $this->restClient = new RestClient($this->storageUrl, $settings, 'BlobAuthorization'); $date = gmdate("D, d M Y H:i:s") . ' GMT'; $this->defaultHeader['x-ms-date'] = $date; $this->defaultHeader['x-ms-version'] = '2015-02-21'; }
/** * getCanonicalized * * @param string $url * @param array $queryParams * @param string $method * @return string */ public function getAuthorizationString($url, $method, array $queryParams, array $header = []) { $canonicalizedResource = '/' . $this->settings->getName() . parse_url($url, PHP_URL_PATH); if (count($queryParams) > 0) { ksort($queryParams); } foreach ($queryParams as $key => $value) { // Grouping query parameters $values = explode(',', $value); sort($values); $separated = implode(',', $values); $canonicalizedResource .= "\n" . $key . ':' . $separated; } $stringToSign = []; $stringToSign[] = strtoupper($method); foreach ($this->fixedHeaders as $headerKey => $headerValue) { $stringToSign[] = !empty($header[$headerKey]) || is_numeric($header[$headerKey]) ? $header[$headerKey] : $headerValue; } if (!empty($header)) { foreach ($this->canonicalizedHeaders as $headerKey => $headerValue) { if (!empty($header[$headerKey])) { $this->canonicalizedHeaders[$headerKey] = $header[$headerKey]; } } } $this->canonicalizedHeaders = array_filter($this->canonicalizedHeaders); if (count($this->canonicalizedHeaders) > 0) { $canonicalizedHeaders = []; foreach ($this->canonicalizedHeaders as $headerKey => $headerValue) { $canonicalizedHeaders[] = $headerKey . ':' . $headerValue; } $stringToSign[] = implode("\n", $canonicalizedHeaders); } $stringToSign[] = $canonicalizedResource; $signature = implode("\n", $stringToSign); return 'SharedKey ' . $this->settings->getName() . ':' . base64_encode(hash_hmac('sha256', $signature, base64_decode($this->settings->getKey()), true)); }
/** * getCanonicalized * * @param string $url * @param array $queryParams * @param string $method * @return string */ public function getAuthorizationString($url, $method, array $queryParams, array $header = []) { $result = $this->restClient->send('', 'post', [], [Constants::OAUTH_GRANT_TYPE => Constants::OAUTH_GT_CLIENT_CREDENTIALS, Constants::OAUTH_CLIENT_ID => $this->settings->getName(), Constants::OAUTH_CLIENT_SECRET => $this->settings->getKey(), Constants::OAUTH_SCOPE => Constants::MEDIA_SERVICES_OAUTH_SCOPE], ['Content-Type', 'application/x-www-form-urlencoded']); return 'Bearer ' . $result->access_token; }