Skip to content

Commit

Permalink
Refactoring header auth methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vince-scarpa committed Jun 20, 2020
1 parent f1e0bb1 commit f40f88e
Showing 1 changed file with 56 additions and 49 deletions.
105 changes: 56 additions & 49 deletions src/core/headers/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit f40f88e

Please sign in to comment.