Akamai {OPEN} EdgeGrid Authentication for PHP
  • Namespace
  • Class
  • Tree

Namespaces

  • Akamai
    • Open
      • EdgeGrid
        • Authentication
          • Exception
            • SignerException
        • Exception
          • HandlerException
        • Handler

Classes

  • Akamai\Open\EdgeGrid\Authentication
  • Akamai\Open\EdgeGrid\Authentication\Nonce
  • Akamai\Open\EdgeGrid\Authentication\Timestamp
  • Akamai\Open\EdgeGrid\Cli
  • Akamai\Open\EdgeGrid\Client
  • Akamai\Open\EdgeGrid\Handler\Authentication
  • Akamai\Open\EdgeGrid\Handler\Debug
  • Akamai\Open\EdgeGrid\Handler\Verbose

Exceptions

  • Akamai\Open\EdgeGrid\Authentication\Exception
  • Akamai\Open\EdgeGrid\Authentication\Exception\ConfigException
  • Akamai\Open\EdgeGrid\Authentication\Exception\SignerException
  • Akamai\Open\EdgeGrid\Authentication\Exception\SignerException\InvalidSignDataException
  • Akamai\Open\EdgeGrid\Exception
  • Akamai\Open\EdgeGrid\Exception\HandlerException
  • Akamai\Open\EdgeGrid\Exception\HandlerException\IOException
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 
<?php
/**
 * Akamai {OPEN} EdgeGrid Auth for PHP
 *
 * @author Davey Shafik <dshafik@akamai.com>
 * @copyright Copyright 2016 Akamai Technologies, Inc. All rights reserved.
 * @license Apache 2.0
 * @link https://github.com/akamai-open/AkamaiOPEN-edgegrid-php
 * @link https://developer.akamai.com
 * @link https://developer.akamai.com/introduction/Client_Auth.html
 */
namespace Akamai\Open\EdgeGrid;

use Akamai\Open\EdgeGrid\Authentication\Nonce;
use Akamai\Open\EdgeGrid\Authentication\Timestamp;
use Akamai\Open\EdgeGrid\Authentication\Exception\ConfigException;
use Akamai\Open\EdgeGrid\Authentication\Exception\SignerException\InvalidSignDataException;

/**
 * Akamai {OPEN} EdgeGrid Request Signer
 *
 * @package \Akamai\Open\EdgeGrid\Authentication
 */
class Authentication
{
    /**
     * @var array Authentication tokens
     */
    protected $auth;

    /**
     * @var string HTTP method
     */
    protected $httpMethod;

    /**
     * @var string HTTP host
     */
    protected $host;

    /**
     * @var array Guzzle config
     */
    protected $config = array();

    /**
     * @var string Request path
     */
    protected $path;

    /**
     * @var Timestamp Request timestamp
     */
    protected $timestamp;

    /**
     * @var Nonce Request nonce
     */
    protected $nonce;

    /**
     * @var int Maximum body size for signing
     */
    protected $max_body_size = 131072;

    /**
     * @var array A list of headers to be included in the signature
     */
    protected $headers_to_sign = array();

    /**
     * Create the Authentication header
     * @return string
     * @throws \Akamai\Open\EdgeGrid\Authentication\Exception\SignerException\InvalidSignDataException
     * @link https://developer.akamai.com/introduction/Client_Auth.html
     */
    public function createAuthHeader()
    {
        if ($this->timestamp === null) {
            $this->setTimestamp();
        }

        if (!$this->timestamp->isValid()) {
            throw new InvalidSignDataException('Timestamp is invalid. Too old?');
        }

        if ($this->nonce === null) {
            $this->nonce = new Nonce();
        }

        $auth_header =
            'EG1-HMAC-SHA256 ' .
            'client_token=' . $this->auth['client_token'] . ';' .
            'access_token=' . $this->auth['access_token'] . ';' .
            'timestamp=' . $this->timestamp . ';' .
            'nonce=' . $this->nonce . ';';

        return $auth_header . 'signature=' . $this->signRequest($auth_header);
    }

    /**
     * Set request HTTP method
     *
     * @param string $method
     * @return Authentication
     */
    public function setHttpMethod($method)
    {
        $this->httpMethod = strtoupper($method);
        return $this;
    }

    /**
     * Get request HTTP method
     *
     * @return string
     */
    public function getHttpMethod()
    {
        return $this->httpMethod;
    }

    /**
     * Set request host
     *
     * @param mixed $host
     * @return Authentication
     */
    public function setHost($host)
    {
        $this->host = $host;
        if (strpos($host, '/') !== false || strpos($host, '?') !== false) {
            if (strpos($host, 'http') === false) {
                $host = 'https://' . $host;
            }
            $url = parse_url($host);
            $this->host = $url['host'];

            if (isset($url['path'])) {
                $this->setPath($url['path']);
            }

            if (isset($url['query'])) {
                if (!isset($url['path'])) { // for example.org?query=string
                    $this->setPath('/');
                }
                $this->setQuery($url['query']);
            }
        }

        return $this;
    }

    /**
     * Get the request host
     *
     * @return string
     */
    public function getHost()
    {
        return $this->host;
    }

    /**
     * Set Guzzle config
     *
     * This is a convenient way to pass in the
     * body/query/headers options
     *
     * @param mixed $config
     * @return Authentication
     */
    public function setConfig(array $config)
    {
        $this->config = array_merge($this->config, $config);
        return $this;
    }

    /**
     * Get Authentication config array
     *
     * @return array
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * Set GET args
     *
     * If setting to a string, you MUST encode using RFC3986
     * {@see http_build_query()}. When passing in a string,
     * this method will re-encode using RFC3986 unless you
     * explicitly pass in false as the second argument.
     *
     * @param array|string $query
     * @param bool $ensure_encoding
     * @return $this
     */
    public function setQuery($query, $ensure_encoding = true)
    {
        if (is_string($query) && $ensure_encoding) {
            $query_args = array();
            parse_str($query, $query_args);
            $query = $this->buildQueryString($query_args);
        }
        $this->config['query'] = $query;
        return $this;
    }

    /**
     * Get request query string
     *
     * The return value will match
     * the type passed in to setQuery().
     *
     * @return string|array
     */
    public function getQuery()
    {
        return isset($this->config['query']) ? $this->config['query'] : '';
    }

    /**
     * Set request body
     *
     * @param string $body
     * @return $this
     */
    public function setBody($body)
    {
        $this->config['body'] = $body;
        return $this;
    }

    /**
     * Get request body
     *
     * @param bool $truncate
     * @return string
     */
    public function getBody($truncate = false)
    {
        if (!$truncate) {
            return isset($this->config['body']) ? $this->config['body'] : '';
        }

        return isset($this->config['body']) ? substr($this->config['body'], 0, $this->max_body_size) : '';
    }

    /**
     * Set request headers
     *
     * @param array $headers
     * @return $this
     */
    public function setHeaders(array $headers)
    {
        $this->config['headers'] = $headers;
        return $this;
    }

    /**
     * Get request headers
     *
     * @return array
     */
    public function getHeaders()
    {
        return isset($this->config['headers']) ? $this->config['headers'] : array();
    }

    /**
     * Set request path
     *
     * @param mixed $path
     * @return $this
     */
    public function setPath($path)
    {
        $url = parse_url($path);

        $this->path = $url['path'];
        if (isset($url['host'])) {
            $this->setHost($url['host']);
        }

        if (isset($url['query'])) {
            $this->setQuery($url['query']);
        }
        return $this;
    }

    /**
     * Get request path
     *
     * @return string
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * Set signing timestamp
     *
     * @param Timestamp|string $timestamp
     * @return $this
     */
    public function setTimestamp($timestamp = null)
    {
        $this->timestamp = $timestamp;
        if ($timestamp === null) {
            $this->timestamp = new Timestamp();
        }
        return $this;
    }

    /**
     * Set signing nonce
     *
     * @param Nonce|string $nonce
     * @return $this
     */
    public function setNonce($nonce = null)
    {
        $this->nonce = $nonce;
        if ($nonce === null) {
            $this->nonce = new Nonce();
        }
        return $this;
    }

    /**
     * Set headers to sign
     *
     * @param array $headers_to_sign
     * @return $this
     */
    public function setHeadersToSign($headers_to_sign)
    {
        $this->headers_to_sign = $headers_to_sign;
        return $this;
    }

    /**
     * Set max body size to sign
     *
     * @param int $max_body_size Size (in bytes)
     * @return $this
     */
    public function setMaxBodySize($max_body_size)
    {
        $this->max_body_size = trim($max_body_size);
        return $this;
    }

    /**
     * Set Akamai EdgeGrid Authentication Tokens/Secret
     *
     * @param string $client_token
     * @param string $client_secret
     * @param string $access_token
     * @return $this
     */
    public function setAuth($client_token, $client_secret, $access_token)
    {
        $this->auth = array(
            'client_token' => trim($client_token),
            'client_secret' => trim($client_secret),
            'access_token' => trim($access_token),
        );
        return $this;
    }

    /**
     * Create instance using environment (prefered) or .edgerc file (fallback)
     * automatically.
     *
     * This method will check in order:
     * - AKAMAI_{SECTION}_* environment variables
     * - if using the "default" section, AKAMAI_* environment variables
     * - the specified (or "default" if none) section in .edgerc
     * - if not using the "default" section, AKAMAI_* environment variables
     *
     * @param string $section
     * @param null $path
     * @return Authentication
     * @throws \Akamai\Open\EdgeGrid\Authentication\Exception\ConfigException
     */
    public static function createInstance($section = 'default', $path = null)
    {
        $previousError = null;

        if (isset($_ENV['AKAMAI_' .strtoupper($section). '_HOST'])
            || (isset($_ENV['AKAMAI_HOST']) && $section === 'default')) {
            try {
                return self::createFromEnv($section);
            } catch (ConfigException $previousError) {
            }
        }

        try {
            return self::createFromEdgeRcFile($section, $path);
        } catch (ConfigException $previousError) {
            try {
                if (isset($_ENV['AKAMAI_HOST']) && $section !== 'default') {
                    return self::createFromEnv();
                }
            } catch (ConfigException $previousError) {
                // fall through to the below throw
            }
        }

        throw new ConfigException('Unable to create instance using environment or .edgerc file', 0, $previousError);
    }

    /**
     * Create instance using environment variables
     *
     * @param string $section
     * @return Authentication
     * @throws \Akamai\Open\EdgeGrid\Authentication\Exception\ConfigException
     */
    public static function createFromEnv($section = 'default')
    {
        $section = strtoupper($section);

        $prefix = isset($_ENV['AKAMAI_' . $section . '_HOST']) ? 'AKAMAI_' . $section . '_' : 'AKAMAI_';

        $vars = array('HOST', 'CLIENT_TOKEN', 'CLIENT_SECRET', 'ACCESS_TOKEN');

        foreach ($vars as $var) {
            if (!isset($_ENV[$prefix . $var])) {
                throw new ConfigException(sprintf(
                    'Environment variable%s %sAKAMAI_%s_%s do%s not exist',
                    $section === 'DEFAULT' ? 's' : '',
                    $section === 'DEFAULT' ? 'AKAMAI_' . $var . ' or ' : '',
                    $section,
                    $var,
                    $section === 'DEFAULT' ? '' : 'es'
                ));
            }
        }

        $auth = new static();
        $auth->setAuth(
            $_ENV[$prefix . 'CLIENT_TOKEN'],
            $_ENV[$prefix . 'CLIENT_SECRET'],
            $_ENV[$prefix . 'ACCESS_TOKEN']
        );

        $auth->setHost($_ENV[$prefix . 'HOST']);

        if (isset($_ENV[$prefix . 'MAX_SIZE'])) {
            $auth->setMaxBodySize($_ENV[$prefix . 'MAX_SIZE']);
        }

        return $auth;
    }

    /**
     * Create instance using an .edgerc configuration file
     *
     * @param string $section
     * @param string|null $path
     *
     * @return Authentication
     * @throws \Akamai\Open\EdgeGrid\Authentication\Exception\ConfigException
     */
    public static function createFromEdgeRcFile($section = 'default', $path = null)
    {
        if ($section === null) {
            $section = 'default';
        }

        $ini = self::parseEdgeRcFile($path);

        if (!isset($ini[$section])) {
            throw new ConfigException("Section \"$section\" does not exist!");
        }

        $auth = new static();
        $auth->setAuth(
            $ini[$section]['client_token'],
            $ini[$section]['client_secret'],
            $ini[$section]['access_token']
        );

        if (isset($ini[$section]['host'])) {
            $auth->setHost($ini[$section]['host']);
        }

        if (isset($ini[$section]['max-size'])) {
            $auth->setMaxBodySize($ini[$section]['max-size']);
        }

        return $auth;
    }

    /**
     * Returns headers in normalized form
     *
     * @return string
     */
    protected function canonicalizeHeaders()
    {
        $canonical = array();
        $headers = array();
        if (isset($this->config['headers'])) {
            $headers = array_map('strtolower', array_keys($this->config['headers']));
            if (count($this->config['headers']) > 0) {
                $headers = array_combine(
                    $headers,
                    array_values($this->config['headers'])
                );
            }
        }

        foreach ($this->headers_to_sign as $key) {
            $key = strtolower($key);
            if (isset($headers[$key])) {
                if (is_array($headers[$key]) && count($headers[$key]) >= 1) {
                    $value = trim($headers[$key][0]);
                } elseif (is_array($headers[$key]) && count($headers[$key]) === 0) {
                    continue;
                } else {
                    $value = trim($headers[$key]);
                }

                if (!empty($value)) {
                    $canonical[$key] = preg_replace('/\s+/', ' ', $value);
                }
            }
        }

        ksort($canonical);
        $serialized_header = '';
        foreach ($canonical as $key => $value) {
            $serialized_header .= $key . ':' . $value . "\t";
        }

        return rtrim($serialized_header);
    }

    /**
     * Returns Base64 encoded HMAC-SHA256 Hash
     *
     * @param string $data
     * @param string $key
     * @return string
     */
    protected function makeBase64HmacSha256($data, $key)
    {
        return base64_encode(hash_hmac('sha256', (string) $data, $key, true));
    }

    /**
     * Returns Base64 encoded SHA256 Hash
     *
     * @param string $data
     * @return string
     */
    protected function makeBase64Sha256($data)
    {
        return base64_encode(hash('sha256', (string) $data, true));
    }

    /**
     * Returns a hash of the HTTP POST body
     *
     * @return string
     */
    protected function makeContentHash()
    {
        if (empty($this->config['body'])) {
            return '';
        } else {
            // Just substr, it'll return as much as it can
            return $this->makeBase64Sha256($this->getBody(true));
        }
    }

    /**
     * Returns a string with all data that will be signed
     *
     * @param string $auth_header
     * @return string
     */
    protected function makeDataToSign($auth_header)
    {
        $query = '';
        if (isset($this->config['query']) && $this->config['query']) {
            $query .= '?';
            if (is_string($this->config['query'])) {
                $query .= $this->config['query'];
            } else {
                $query .= $this->buildQueryString($this->config['query']);
            }
        }

        $data = array(
            strtoupper($this->httpMethod),
            'https',
            $this->host,
            $this->path . $query,
            $this->canonicalizeHeaders(),
            (strtoupper($this->httpMethod) === 'POST') ? $this->makeContentHash() : '',
            $auth_header
        );

        return implode("\t", $data);
    }

    /**
     * Creates a signing key based on the secret and timestamp
     *
     * @return string
     */
    protected function makeSigningKey()
    {
        return $this->makeBase64HmacSha256((string)$this->timestamp, $this->auth['client_secret']);
    }

    /**
     * Returns a signature of the given request, timestamp and auth_header
     *
     * @param string $auth_header
     * @return string
     */
    protected function signRequest($auth_header)
    {
        return $this->makeBase64HmacSha256(
            $this->makeDataToSign($auth_header),
            $this->makeSigningKey()
        );
    }

    /**
     * Parse a .edgerc File
     *
     * @param $path
     * @return array
     * @throws \Akamai\Open\EdgeGrid\Authentication\Exception\ConfigException
     */
    protected static function parseEdgeRcFile($path)
    {
        if ($path === null) {
            if (isset($_SERVER['HOME']) && file_exists($_SERVER['HOME'] . '/.edgerc')) {
                $path = $_SERVER['HOME'] . '/.edgerc';
            } elseif (file_exists('./.edgerc')) {
                $path = './.edgerc';
            }
        }

        $file = !$path ? false : realpath($path);
        if (!$file) {
            throw new ConfigException("Path to .edgerc file \"$path\" does not exist!");
        }

        if (!is_readable($file)) {
            throw new ConfigException('Unable to read .edgerc file!');
        }

        // Handle : assignments in .edgerc files
        $ini = file_get_contents($file);
        $ini = str_replace(':', '=', $ini);

        $ini = parse_ini_string($ini, true, INI_SCANNER_RAW);

        return $ini;
    }

    /**
     * @param $query
     *
     * @return string
     */
    protected function buildQueryString($query)
    {
        if (defined('PHP_QUERY_RFC3986')) {
            return http_build_query($query, null, '&', PHP_QUERY_RFC3986);
        }

        // @codeCoverageIgnoreStart
        return str_replace('+', '%20', http_build_query($query, null, '&'));
        // @codeCoverageIgnoreEnd
    }
}
Akamai {OPEN} EdgeGrid Authentication for PHP API documentation generated by ApiGen