Ubiquity  2.0.2
php rapid development framework
RestServer.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
15 class RestServer {
20  protected $config;
21  protected $headers;
22  protected $tokensFolder;
23  protected $tokensCacheKey="_apiTokens";
28  protected $apiTokens;
29 
30  public function __construct($config) {
31  $this->config=$config;
32  $this->headers=[ 'Access-Control-Allow-Origin' => 'http://127.0.0.1:4200','Access-Control-Allow-Credentials' => 'true','Access-Control-Max-Age' => '86400','Access-Control-Allow-Methods' => 'GET, POST, OPTIONS, PUT, DELETE, PATCH, HEAD' ];
33  }
34 
35  public function connect(RestController $controller) {
36  if (!isset($this->apiTokens)) {
37  $this->apiTokens=$this->_getApiTokens();
38  }
39  $token=$this->apiTokens->addToken();
40  $this->_addHeaderToken($token);
41  echo $controller->_format([ "access_token" => $token,"token_type" => "Bearer","expires_in" => $this->apiTokens->getDuration() ]);
42  }
43 
48  public function isValid() {
49  $this->apiTokens=$this->_getApiTokens();
50  $key=$this->_getHeaderToken();
51  if ($this->apiTokens->isExpired($key)) {
52  return false;
53  } else {
54  $this->_addHeaderToken($key);
55  return true;
56  }
57  }
58 
59  public function _getHeaderToken() {
60  $authHeader=$this->_getHeader("Authorization");
61  if ($authHeader !== false) {
62  list ( $type, $data )=explode(" ", $authHeader, 2);
63  if (\strcasecmp($type, "Bearer") == 0) {
64  return $data;
65  } else {
66  throw new RestException("Bearer is required in authorization header.");
67  }
68  } else {
69  throw new RestException("The header Authorization is required in http headers.");
70  }
71  }
72 
73  public function finalizeTokens() {
74  if (isset($this->apiTokens)) {
75  $this->apiTokens->removeExpireds();
76  $this->apiTokens->storeToCache();
77  }
78  }
79 
80  public function _getHeader($header) {
81  $headers=getallheaders();
82  if (isset($headers[$header])) {
83  return $headers[$header];
84  }
85  return false;
86  }
87 
88  public function _addHeaderToken($token) {
89  $this->_header("Authorization", "Bearer " . $token);
90  }
91 
96  public function _getApiTokens() {
97  return ApiTokens::getFromCache(ROOT . CacheManager::getCacheDirectory() . DS, $this->tokensCacheKey);
98  }
99 
106  public function _header($headerField, $value=null, $replace=null) {
107  if (!isset($value)) {
108  if (isset($this->headers[$headerField])) {
109  $value=$this->headers[$headerField];
110  } else
111  return;
112  }
113  \header(trim($headerField) . ": " . trim($value), $replace);
114  }
115 
121  public function _setContentType($contentType, $charset=null) {
122  $value=$contentType;
123  if (isset($charset))
124  $value.="; charset=" . $charset;
125  $this->_header("Content-type", $value);
126  }
127 
128  public function cors() {
129  $this->_header('Access-Control-Allow-Origin');
130  $this->_header('Access-Control-Allow-Credentials');
131  $this->_header('Access-Control-Max-Age');
132  if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
133  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
134  $this->_header('Access-Control-Allow-Methods');
135 
136  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
137  $this->_header('Access-Control-Allow-Headers', $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
138  } else {
139  $this->_header('Access-Control-Allow-Headers', '*');
140  }
141  throw new RestException("cors exit normally");
142  }
143  }
144 
145  public static function getRestNamespace() {
147  $controllerNS=$config["mvcNS"]["controllers"];
148  $restNS="";
149  if (isset($config["mvcNS"]["rest"])) {
150  $restNS=$config["mvcNS"]["rest"];
151  }
152  return ClassUtils::getNamespaceFromParts([ $controllerNS,$restNS ]);
153  }
154 }
Exceptions for Rest service.
_setContentType($contentType, $charset=null)
Definition: RestServer.php:121
$replace
Definition: traits.php:14
static getFromCache($folder, $key="_apiTokens", $length=10, $duration=3600)
Definition: ApiTokens.php:87
static getNamespaceFromParts($parts)
Returns a cleanly namespace.
Definition: ClassUtils.php:29
isValid()
Check if token is valid.
Definition: RestServer.php:48
_getApiTokens()
To override for defining another ApiToken type.
Definition: RestServer.php:96
connect(RestController $controller)
Definition: RestServer.php:35
_header($headerField, $value=null, $replace=null)
Definition: RestServer.php:106