Use token type to determine access token in header

This commit is contained in:
Alex Bilbie 2014-05-07 17:21:24 +01:00
parent 0d293e7c30
commit aae99c2487
3 changed files with 24 additions and 3 deletions

View File

@ -16,6 +16,7 @@ use League\OAuth2\Server\Storage\AccessTokenInterface;
use League\OAuth2\Server\Storage\SessionInterface; use League\OAuth2\Server\Storage\SessionInterface;
use League\OAuth2\Server\Storage\ScopeInterface; use League\OAuth2\Server\Storage\ScopeInterface;
use League\OAuth2\Server\Entity\AccessTokenEntity; use League\OAuth2\Server\Entity\AccessTokenEntity;
use League\OAuth2\Server\TokenType\Bearer;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
@ -194,9 +195,8 @@ class ResourceServer extends AbstractServer
*/ */
public function determineAccessToken($headersOnly = false) public function determineAccessToken($headersOnly = false)
{ {
if ($header = $this->getRequest()->headers->get('Authorization')) { if ($this->getRequest()->headers->get('Authorization') !== null) {
$accessToken = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $header)); $accessToken = $this->getTokenType()->determineAccessTokenInHeader($this->getRequest());
$accessToken = ($accessToken === 'Bearer') ? '' : $accessToken;
} elseif ($headersOnly === false) { } elseif ($headersOnly === false) {
$accessToken = ($this->getRequest()->server->get('REQUEST_METHOD') === 'GET') ? $accessToken = ($this->getRequest()->server->get('REQUEST_METHOD') === 'GET') ?
$this->getRequest()->query->get($this->tokenKey) : $this->getRequest()->query->get($this->tokenKey) :

View File

@ -11,6 +11,8 @@
namespace League\OAuth2\Server\TokenType; namespace League\OAuth2\Server\TokenType;
use Symfony\Component\HttpFoundation\Request;
abstract class AbstractTokenType abstract class AbstractTokenType
{ {
/** /**
@ -38,4 +40,11 @@ abstract class AbstractTokenType
{ {
return isset($this->response[$key]) ? $this->response[$key] : null; return isset($this->response[$key]) ? $this->response[$key] : null;
} }
/**
* Determine the access token in the authorization header
* @param \Symfony\Component\HttpFoundation\Request $request
* @return string
*/
abstract public function determineAccessTokenInHeader(Request $request);
} }

View File

@ -11,6 +11,8 @@
namespace League\OAuth2\Server\TokenType; namespace League\OAuth2\Server\TokenType;
use Symfony\Component\HttpFoundation\Request;
class Bearer extends AbstractTokenType implements TokenTypeInterface class Bearer extends AbstractTokenType implements TokenTypeInterface
{ {
/** /**
@ -31,4 +33,14 @@ class Bearer extends AbstractTokenType implements TokenTypeInterface
return $return; return $return;
} }
/**
* {@inheritdoc}
*/
public function determineAccessTokenInHeader(Request $request)
{
$header = $request->headers->get('Authorization');
$accessToken = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $header));
return ($accessToken === 'Bearer') ? '' : $accessToken;
}
} }