Skip to content

Allowed token detection methods to be defined in config file #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions config/jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,21 @@

'middleware_match' => true,


/*
|--------------------------------------------------------------------------
| Token detection from request methods
|--------------------------------------------------------------------------
|
| Use an array of strings including: parameter, header
|
| Header will check the request for the Authorization: Bearer TOKEN header
| Parameter will check for the token the request body "token" parameters
|
*/
'token_detections' => [
'parameter',
'header'
]

];
45 changes: 39 additions & 6 deletions src/Auth/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ class Guard implements GuardContract
*/
protected $name;

/**
* Methods used to detect the token in the request
*
* @var array
*/
protected $detections = [
'header',
'parameter'
];

/**
* The currently authenticated user.
*
Expand Down Expand Up @@ -269,15 +279,28 @@ protected function getTokenFromParameter()
*/
protected function detectedToken()
{
// retrieve the token from request.
$detectedToken = $this->getTokenFromHeader() ?? $this->getTokenFromParameter();
if (in_array('header', $this->detections, true)) {
// retrieve the token from the Authorization header.
$headerToken = $this->getTokenFromHeader();

// if a token was found...
if ($headerToken) {
// return a new token instance.
$this->token = $this->manager()->parseToken($headerToken);
}
}

if (in_array('parameter', $this->detections, true)) {
// try to find a token passed as parameter on the request.
$parameterToken = $this->getTokenFromParameter();

if ($detectedToken) {
// update the currently used token
$this->token = $this->manager()->parseToken($detectedToken);
// if found...
if ($parameterToken) {
$this->token = $this->manager()->parseToken($parameterToken);
}
}

// return current token in use
// return null if no token could be found.
return $this->token;
}

Expand Down Expand Up @@ -456,6 +479,16 @@ public function setDispatcher(Dispatcher $events)
$this->events = $events;
}

/**
* Set the token detection methods. Accepted: parameter, header
*
* @param array $detections
*/
public function setTokenDetections(array $detections)
{
$this->detections = $detections;
}

/**
* Get the current request instance.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Auth/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function register()
// set a event dispatcher on the guard.
$guard->setDispatcher(resolve(Dispatcher::class));

$detections = $this->app['config']->get('jwt.token_detections', ['parameter', 'header']);

// set the token detection methods
$guard->setTokenDetections($detections);

// returns the guard instance.
return new Guard($app, $name, $userProvider, $tokenManager);
});
Expand Down