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' ];
36 if (!isset($this->apiTokens)) {
39 $token=$this->apiTokens->addToken();
41 echo $controller->
_format([
"access_token" => $token,
"token_type" =>
"Bearer",
"expires_in" => $this->apiTokens->getDuration() ]);
51 if ($this->apiTokens->isExpired($key)) {
60 $authHeader=$this->
_getHeader(
"Authorization");
61 if ($authHeader !==
false) {
62 list ( $type, $data )=explode(
" ", $authHeader, 2);
63 if (\strcasecmp($type,
"Bearer") == 0) {
66 throw new RestException(
"Bearer is required in authorization header.");
69 throw new RestException(
"The header Authorization is required in http headers.");
74 if (isset($this->apiTokens)) {
75 $this->apiTokens->removeExpireds();
76 $this->apiTokens->storeToCache();
89 $this->
_header(
"Authorization",
"Bearer " . $token);
107 if (!isset($value)) {
108 if (isset($this->headers[$headerField])) {
109 $value=$this->headers[$headerField];
113 \header(trim($headerField) .
": " . trim($value),
$replace);
124 $value.=
"; charset=" . $charset;
125 $this->
_header(
"Content-type", $value);
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');
136 if (isset($_SERVER[
'HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
137 $this->
_header(
'Access-Control-Allow-Headers', $_SERVER[
'HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
139 $this->
_header(
'Access-Control-Allow-Headers',
'*');
147 $controllerNS=
$config[
"mvcNS"][
"controllers"];
149 if (isset(
$config[
"mvcNS"][
"rest"])) {
150 $restNS=
$config[
"mvcNS"][
"rest"];
Exceptions for Rest service.
_setContentType($contentType, $charset=null)
static getFromCache($folder, $key="_apiTokens", $length=10, $duration=3600)
static getNamespaceFromParts($parts)
Returns a cleanly namespace.
isValid()
Check if token is valid.
static getRestNamespace()
_getApiTokens()
To override for defining another ApiToken type.
connect(RestController $controller)
static getCacheDirectory()
_header($headerField, $value=null, $replace=null)