Overview

Namespaces

  • Racoon
    • Api
      • Auth
      • Exception
      • Response
        • Format
        • Generate
      • Schema

Classes

  • Racoon\Api\App
  • Racoon\Api\Auth\ApiKeyAuthenticator
  • Racoon\Api\Auth\NoAuthenticator
  • Racoon\Api\Controller
  • Racoon\Api\Request
  • Racoon\Api\Response\Format\JsonFormatter
  • Racoon\Api\Response\Generate\DetailedResponse
  • Racoon\Api\Schema\Item
  • Racoon\Api\Schema\Schema
  • Racoon\Api\Schema\Translator

Interfaces

  • Racoon\Api\Auth\AuthInterface
  • Racoon\Api\Response\Format\FormatterInterface
  • Racoon\Api\Response\Generate\GeneratorInterface

Exceptions

  • Racoon\Api\Exception\AuthenticationException
  • Racoon\Api\Exception\Exception
  • Racoon\Api\Exception\InvalidArgumentException
  • Racoon\Api\Exception\InvalidJsonException
  • Racoon\Api\Exception\InvalidRouteException
  • Racoon\Api\Exception\NotFoundException
  • Racoon\Api\Exception\ResponseFormattingException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Racoon\Api\Auth;
  4: 
  5: 
  6: use Racoon\Api\Exception\AuthenticationException;
  7: use Racoon\Api\Request;
  8: 
  9: /**
 10:  * Basic authenticator which can be used to set a static list of API keys.
 11:  * Class ApiKeyAuthenticator
 12:  * @package Racoon\Api\Auth
 13:  */
 14: class ApiKeyAuthenticator implements AuthInterface
 15: {
 16: 
 17:     /**
 18:      * @var string
 19:      */
 20:     protected $apiKeyName;
 21: 
 22:     /**
 23:      * @var string[]
 24:      */
 25:     protected $validApiKeys = [];
 26: 
 27: 
 28:     public function __construct()
 29:     {
 30:         $this->setApiKeyName('api_key');
 31:     }
 32: 
 33: 
 34:     /**
 35:      * @param Request $request
 36:      * @return bool
 37:      * @throws AuthenticationException
 38:      */
 39:     public function authenticate(Request $request)
 40:     {
 41:         $data = $request->getFullRequestData();
 42:         
 43:         if (! (is_object($data) && isset($data->{$this->getApiKeyName()}))) {
 44:             throw new AuthenticationException($request, 'API Key not found.');
 45:         }
 46: 
 47:         $apiKey = $data->{$this->getApiKeyName()};
 48:         
 49:         if (! in_array($apiKey, $this->getValidApiKeys())) {
 50:             throw new AuthenticationException($request, 'Invalid API Key.');
 51:         }
 52: 
 53:         return true;
 54:     }
 55: 
 56: 
 57:     /**
 58:      * @return string
 59:      */
 60:     public function getApiKeyName()
 61:     {
 62:         return $this->apiKeyName;
 63:     }
 64: 
 65: 
 66:     /**
 67:      * @param string $apiKeyName
 68:      */
 69:     public function setApiKeyName($apiKeyName)
 70:     {
 71:         $this->apiKeyName = $apiKeyName;
 72:     }
 73: 
 74: 
 75:     /**
 76:      * @return string[]
 77:      */
 78:     public function getValidApiKeys()
 79:     {
 80:         return $this->validApiKeys;
 81:     }
 82: 
 83: 
 84:     /**
 85:      * @param string[] $validApiKeys
 86:      */
 87:     public function setValidApiKeys($validApiKeys)
 88:     {
 89:         $this->validApiKeys = $validApiKeys;
 90:     }
 91: 
 92: 
 93:     /**
 94:      * @param string $apiKey
 95:      */
 96:     public function addValidApiKey($apiKey)
 97:     {
 98:         if (! in_array($apiKey, $this->validApiKeys)) {
 99:             $this->validApiKeys[] = $apiKey;
100:         }
101:     }
102: 
103: }
API documentation generated by ApiGen