The start of something beautiful
This commit is contained in:
Vendored
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* AuthorizationHeaderTokenExtractor.
|
||||
*
|
||||
* @author Nicolas Cabot <n.cabot@lexik.fr>
|
||||
*/
|
||||
class AuthorizationHeaderTokenExtractor implements TokenExtractorInterface
|
||||
{
|
||||
protected ?string $prefix;
|
||||
|
||||
protected string $name;
|
||||
|
||||
public function __construct(?string $prefix, string $name)
|
||||
{
|
||||
$this->prefix = $prefix;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function extract(Request $request)
|
||||
{
|
||||
if (!$request->headers->has($this->name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$authorizationHeader = $request->headers->get($this->name);
|
||||
|
||||
if (empty($this->prefix)) {
|
||||
return $authorizationHeader;
|
||||
}
|
||||
|
||||
$headerParts = explode(' ', (string) $authorizationHeader);
|
||||
|
||||
if (!(2 === count($headerParts) && 0 === strcasecmp($headerParts[0], $this->prefix))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $headerParts[1];
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* ChainTokenExtractor is the class responsible of extracting a JWT token
|
||||
* from a {@link Request} object using all mapped token extractors.
|
||||
*
|
||||
* Note: The extractor map is reinitialized to the configured extractors for
|
||||
* each different instance.
|
||||
*
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
*/
|
||||
class ChainTokenExtractor implements \IteratorAggregate, TokenExtractorInterface
|
||||
{
|
||||
private array $map;
|
||||
|
||||
public function __construct(array $map)
|
||||
{
|
||||
$this->map = $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new token extractor to the map.
|
||||
*/
|
||||
public function addExtractor(TokenExtractorInterface $extractor)
|
||||
{
|
||||
$this->map[] = $extractor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a token extractor from the map.
|
||||
*
|
||||
* @param \Closure $filter A function taking an extractor as argument, used to find the extractor to remove.
|
||||
*
|
||||
* @return bool True in case of success, false otherwise
|
||||
*/
|
||||
public function removeExtractor(\Closure $filter)
|
||||
{
|
||||
$filtered = array_filter($this->map, $filter);
|
||||
|
||||
if (!$extractorToUnmap = current($filtered)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = array_search($extractorToUnmap, $this->map);
|
||||
unset($this->map[$key]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the token extractor map.
|
||||
*/
|
||||
public function clearMap()
|
||||
{
|
||||
$this->map = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over the token extractors map calling {@see extract()}
|
||||
* until a token is found.
|
||||
*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function extract(Request $request)
|
||||
{
|
||||
foreach ($this->getIterator() as $extractor) {
|
||||
if ($token = $extractor->extract($request)) {
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over the mapped token extractors while generating them.
|
||||
*
|
||||
* @return \Traversable<int, TokenExtractorInterface>
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getIterator()
|
||||
{
|
||||
foreach ($this->map as $extractor) {
|
||||
if ($extractor instanceof TokenExtractorInterface) {
|
||||
yield $extractor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* CookieTokenExtractor.
|
||||
*
|
||||
* @author Nicolas Cabot <n.cabot@lexik.fr>
|
||||
*/
|
||||
class CookieTokenExtractor implements TokenExtractorInterface
|
||||
{
|
||||
protected string $name;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function extract(Request $request)
|
||||
{
|
||||
return $request->cookies->get($this->name, false);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* QueryParameterTokenExtractor.
|
||||
*
|
||||
* @author Nicolas Cabot <n.cabot@lexik.fr>
|
||||
*/
|
||||
class QueryParameterTokenExtractor implements TokenExtractorInterface
|
||||
{
|
||||
protected string $parameterName;
|
||||
|
||||
public function __construct(string $parameterName)
|
||||
{
|
||||
$this->parameterName = $parameterName;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function extract(Request $request)
|
||||
{
|
||||
return $request->query->get($this->parameterName, false);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* SplitCookieExtractor.
|
||||
*
|
||||
* @author Adam Lukacovic <adam@adamlukacovic.sk>
|
||||
*/
|
||||
class SplitCookieExtractor implements TokenExtractorInterface
|
||||
{
|
||||
private array $cookies;
|
||||
|
||||
public function __construct(array $cookies)
|
||||
{
|
||||
$this->cookies = $cookies;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function extract(Request $request)
|
||||
{
|
||||
$jwtCookies = [];
|
||||
|
||||
foreach ($this->cookies as $cookie) {
|
||||
$jwtCookies[] = $request->cookies->get($cookie, false);
|
||||
}
|
||||
|
||||
if (count($this->cookies) !== count(array_filter($jwtCookies))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return implode('.', $jwtCookies);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* TokenExtractorInterface.
|
||||
*
|
||||
* @author Nicolas Cabot <n.cabot@lexik.fr>
|
||||
*/
|
||||
interface TokenExtractorInterface
|
||||
{
|
||||
/**
|
||||
* @return string|false
|
||||
*/
|
||||
public function extract(Request $request);
|
||||
}
|
||||
Reference in New Issue
Block a user