Skip to content

Commit

Permalink
Update TokenMiddleware.php
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandPilania committed Aug 14, 2019
1 parent 9df49db commit d908a80
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions TokenMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,23 @@ protected function validate($handler, $args, $alias) {
$authHeader = $this->app->get($verb . '.' . $this->app->get('TOKEN.KEY'));
}

if(!$authHeader) {
$startsWith = $this->app->get('TOKEN.STARTS_WITH');
if(!$authHeader || (($type === 'HEADER' && $startsWith) && !$this->startsWith($authHeader, $startsWith))) {
$this->app->call($handler, array($this->app, $args, $alias));
return false;
}

$authToken = $authHeader;
if($type === 'HEADER' && $this->app->get('TOKEN.STARTS_WITH')) {
$_ex = explode(' ', $authHeader);
if($startsWith && $type === 'HEADER') {
$_ex = explode($startsWith . ' ', $authHeader);
$authToken = isset($_ex[1]) ? $_ex[1] : null;
}

if(!$authToken) {
$this->app->call($handler, array($this->app, $args, $alias));
return false;
}

$model = $this->app->get('TOKEN.TABLE');

if(!class_exists($model)) {
Expand All @@ -114,4 +120,13 @@ protected function validate($handler, $args, $alias) {
$this->app->set('TOKEN.user', $model->user);
}
}
}

private function startsWith($haystack, $needles) {
foreach ((array) $needles as $needle) {
if ($needle !== '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
return true;
}
}
return false;
}
}

0 comments on commit d908a80

Please sign in to comment.