Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(token-extractor): introduce token extractor component
- Loading branch information
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pandawa\Component\TokenExtractor; | ||
|
||
use Pandawa\Contracts\TokenExtractor\TokenExtractorInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* @author Iqbal Maulana <iq.bluejack@gmail.com> | ||
*/ | ||
final class BearerTokenExtractor implements TokenExtractorInterface | ||
{ | ||
public function extract(ServerRequestInterface $request): ?string | ||
{ | ||
$header = $request->getHeaderLine('Authorization'); | ||
|
||
$position = strrpos($header, 'Bearer '); | ||
|
||
if (false !== $position) { | ||
$header = substr($header, $position + 7); | ||
|
||
return str_contains($header, ',') ? strstr($header, ',', true) : $header; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pandawa\Component\TokenExtractor; | ||
|
||
use Pandawa\Contracts\TokenExtractor\TokenExtractorInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* @author Iqbal Maulana <iq.bluejack@gmail.com> | ||
*/ | ||
final class ChainTokenExtractor implements TokenExtractorInterface | ||
{ | ||
/** | ||
* @var TokenExtractorInterface[] | ||
*/ | ||
private array $tokenExtractors; | ||
|
||
public function __construct() | ||
{ | ||
$this->tokenExtractors = [ | ||
new BearerTokenExtractor(), | ||
new QueryParamTokenExtractor(), | ||
]; | ||
} | ||
|
||
public function extract(ServerRequestInterface $request): ?string | ||
{ | ||
foreach ($this->tokenExtractors as $tokenExtractor) { | ||
if (null !== $token = $tokenExtractor->extract($request)) { | ||
return $token; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pandawa\Component\TokenExtractor; | ||
|
||
use Pandawa\Contracts\TokenExtractor\TokenExtractorInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* @author Iqbal Maulana <iq.bluejack@gmail.com> | ||
*/ | ||
final class QueryParamTokenExtractor implements TokenExtractorInterface | ||
{ | ||
public static string $PARAM = '_token'; | ||
|
||
public function extract(ServerRequestInterface $request): ?string | ||
{ | ||
return $request->getQueryParams()[self::$PARAM] ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "pandawa/token-extractor", | ||
"description": "The pandawa token extractor component", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Iqbal Maulana", | ||
"email": "iq.bluejack@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.1", | ||
"pandawa/token-extractor-contracts": "^5.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Pandawa\\Component\\TokenExtractor\\": "" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "5.x-dev" | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"minimum-stability": "dev" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pandawa\Contracts\TokenExtractor; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* @author Iqbal Maulana <iq.bluejack@gmail.com> | ||
*/ | ||
interface TokenExtractorInterface | ||
{ | ||
public function extract(ServerRequestInterface $request): ?string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "pandawa/token-extractor-contracts", | ||
"description": "The pandawa token extractor contracts", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Iqbal Maulana", | ||
"email": "iq.bluejack@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Pandawa\\Contracts\\TokenExtractor\\": "" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "5.x-dev" | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"minimum-stability": "dev" | ||
} |