diff --git a/src/core/headers/header.php b/src/core/headers/header.php index 9c54c75..09bbb43 100644 --- a/src/core/headers/header.php +++ b/src/core/headers/header.php @@ -357,90 +357,97 @@ public function getHeaderStatus() } /** - * [hasBearerToken Check if bearer token is present] - * @return string|null + * [authorizationHeaders Scan for "Authorization" header] + * @return string|array [mixed: string / error] */ - public function hasBearerToken() + public function authorizationHeaders($skipError = false) + { + if ($grant = $this->isGrantRequest()) { + return $grant; + } + + if ($clientToken = $this->hasBearerToken()) { + return $clientToken; + } + + if (!$skipError) { + $this->unauthorised(); + } + } + + /** + * [hasBearerValue Check if Authorization headers has Bearer value] + * @throws Exception + * Unauthorised + * @return boolean + */ + private function hasBearerValue() { $auth_headers = $this->getHeaders(); if (isset($auth_headers["Authorization"]) && !empty($auth_headers["Authorization"])) { - + list($type, $clientToken) = explode(" ", $auth_headers["Authorization"], 2); - if (strcasecmp($type, "Bearer") == 0 && !empty($clientToken)) { - return $clientToken; + if (strcasecmp(trim($type), "Bearer") == 0) { + return true; } } - return; + + return false; } /** - * Check if the request is a token grant - * @return array|boolean + * [hasBearerToken Check if bearer token is present] + * @return string|null */ - public function isGrantRequest($auth_headers) + public function hasBearerToken() { - $helper = new helper; - - if( $grantType = $helper->checkVal($_REQUEST, 'grant_type') ) { - - $refreshToken = false; + $auth_headers = $this->getHeaders(); - if ($grantType == 'client_credentials') { - $refreshToken = $this->accessCredentialHeaders($auth_headers); - } + if( $this->hasBearerValue() ) { - if ($grantType == 'refresh_token') { - $refreshToken = $this->accessRefreshHeaders($auth_headers); - } + list($type, $clientToken) = explode(" ", $auth_headers["Authorization"], 2); - if ($refreshToken) { - return [ - 'client_access_request' => $refreshToken, - ]; + if (strcasecmp(trim($type), "Bearer") == 0 && !empty($clientToken)) { + return $clientToken; } } - return false; + + return; } /** - * [authorizationHeaders Scan for "Authorization" header] - * @return string|array [mixed: string / error] + * Check if the request is a token grant + * @return array|boolean */ - public function authorizationHeaders($skipError = false) + public function isGrantRequest() { $auth_headers = $this->getHeaders(); + $helper = new helper; if (isset($auth_headers["Authorization"]) && !empty($auth_headers["Authorization"])) { + if( $grantType = $helper->checkVal($_REQUEST, 'grant_type') ) { - if ($grant = $this->isGrantRequest($auth_headers)) { - return $grant; - } + $refreshToken = false; - /** - * Test if it's a Authorization Bearer token - */ - if (strcasecmp(trim($auth_headers["Authorization"]), "Bearer") == 0) { - $this->unauthorised(); - } + if ($grantType == 'client_credentials') { + $refreshToken = $this->accessCredentialHeaders($auth_headers); + } - list($type, $clientToken) = explode(" ", $auth_headers["Authorization"], 2); + if ($grantType == 'refresh_token') { + $refreshToken = $this->accessRefreshHeaders($auth_headers); + } - if (strcasecmp($type, "Bearer") == 0 && !empty($clientToken)) { - return $clientToken; - } else { - if (!$skipError) { - $this->unauthorised(); + if ($refreshToken) { + return [ + 'client_access_request' => $refreshToken, + ]; } } - } else { - if (!$skipError) { - $this->unauthorised(); - } } - return ''; + return false; } /**