42 $this->headers = [
'Access-Control-Allow-Origin' =>
'*',
'Access-Control-Allow-Credentials' =>
'true',
'Access-Control-Max-Age' =>
'86400',
'Access-Control-Allow-Methods' =>
'GET, POST, OPTIONS, PUT, DELETE, PATCH, HEAD',
'Content-Type' =>
'application/json; charset=utf8' ];
44 $this->headers = \array_merge ( $this->headers,
$headers );
50 if (! isset ( $this->apiTokens )) {
55 return [
'access_token' => $token,
'token_type' =>
'Bearer',
'expires_in' => $this->apiTokens->getDuration () ];
64 return $this->apiTokens->addToken ($datas);
76 return $this->apiTokens->refreshToken($key);
87 $this->apiTokens = $this->_loadApiTokens ();
88 $key = $this->_getHeaderToken ();
89 if ($this->apiTokens->isExpired ( $key )) {
92 $token=$this->apiTokens->getToken($key);
93 if($callback($token[
'datas']??
null)) {
94 $this->_addHeaderToken($key);
102 $authHeader = $this->_getHeader (
'Authorization' );
103 if ($authHeader !==
false) {
104 $headerDatas = \explode (
' ', $authHeader, 2 );
105 if (\count( $headerDatas ) === 2) {
106 list ( $type, $data ) = $headerDatas;
107 if (\strcasecmp ( $type,
'Bearer' ) == 0) {
110 throw new RestException (
'Bearer is required in authorization header.' );
113 throw new RestException (
'The header Authorization is required in http headers.' );
116 throw new RestException (
'The header Authorization is required in http headers.' );
121 if (isset ( $this->apiTokens )) {
122 $this->apiTokens->removeExpireds ();
123 $this->apiTokens->storeToCache ();
128 $headers = getallheaders ();
129 if (isset ( $headers [$header] )) {
130 return $headers [$header];
136 $this->_header (
'Authorization',
'Bearer ' . $token,
true );
140 return $this->getApiTokens ()->getFromCache ( CacheManager::getAbsoluteCacheDirectory () . \DS, $this->tokensCacheKey );
144 if (! isset ( $this->apiTokens )) {
145 $this->apiTokens = $this->newApiTokens ();
147 return $this->apiTokens;
156 return new ApiTokens ( $this->tokenLength, $this->tokenDuration );
160 $http_origin = URequest::getOrigin ();
161 if (\is_array ( $this->allowedOrigins )) {
162 if (\array_search ( $http_origin, $this->allowedOrigins ) !==
false) {
171 $origin = $this->getAllowedOrigin ();
172 unset ( $this->headers [
'Access-Control-Allow-Origin'] );
173 \header (
'Access-Control-Allow-Origin: ' . $origin,
true );
177 foreach ( $this->headers as $k => $v ) {
178 $this->_header ( $k, $v );
188 public function _header($headerField, $value =
null,
bool $replace =
true) {
189 if (! isset ( $value )) {
190 if (isset ( $this->headers [$headerField] )) {
191 $value = $this->headers [$headerField];
192 unset ( $this->headers [$headerField] );
196 \header ( \trim ( $headerField ) .
": " . \trim ( $value ), $replace );
205 $value = $contentType;
206 if (isset ( $charset )){
207 $value .=
'; charset=' . $charset;
209 $this->_header (
'Content-type', $value );
213 $this->setAccessControlAllowOriginHeader ();
214 $this->_header (
'Access-Control-Allow-Credentials' );
215 $this->_header (
'Access-Control-Max-Age' );
216 if ($_SERVER [
'REQUEST_METHOD'] ==
'OPTIONS') {
217 if (isset ( $_SERVER [
'HTTP_ACCESS_CONTROL_REQUEST_METHOD'] )){
218 $this->_header (
'Access-Control-Allow-Methods' );
220 if (isset ( $_SERVER [
'HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] )) {
221 $this->_header (
'Access-Control-Allow-Headers', $_SERVER [
'HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] );
223 $this->_header (
'Access-Control-Allow-Headers',
'*' );
225 Logger::info (
'Rest',
'cors exit normally',
'Cors' );
227 $this->addOtherHeaders ();
231 $config = Startup::getConfig ();
232 $controllerNS = Startup::getNS(
'controllers');
233 $restNS = $config [
'mvcNS'][
'rest']??
"";
234 return ClassUtils::getNamespaceFromParts ( [ $controllerNS,$restNS ] );
243 if ($address !==
'*') {
244 $this->allowedOrigins = [ $address ];
246 $this->allowedOrigins = [ ];
256 $this->allowedOrigins = $addresses;
265 $this->allowedOrigins = [ $address ];
273 $this->tokenLength = $tokenLength;
281 $this->tokenDuration = $tokenDuration;
Manager for caches (Router, Rest, models).
Manipulates class and namespace names Ubiquity\cache$ClassUtils This class is part of Ubiquity.
Manage the token api for the Rest part.
newApiTokens()
To override for defining another ApiToken type.
setTokenDuration($tokenDuration)
setAllowedOrigin($address=' *')
Adds an unique allowed origin for access control.
refreshToken()
Refresh an active token.
addAllowedOrigin($address)
Adds an allowed origin for access control.
setAllowedOrigins($addresses)
Sets the allowed origins for access control.
static getRestNamespace()
_setContentType($contentType=null, $charset=null)
isValid($callback)
Check if token is valid.
__construct(&$config, $headers=null)
_header($headerField, $value=null, bool $replace=true)
setAccessControlAllowOriginHeader()
setTokenLength($tokenLength)
connect($datas=null)
Establishes the connection with the server, returns an added token in the Authorization header of the...
Exceptions for Rest service.
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.