Skip to content

Commit

Permalink
feat(token-extractor): introduce token extractor component
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ckbon3 committed Jan 7, 2024
1 parent 7713c29 commit 19e8e4a
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/split-monorepo.yaml
Expand Up @@ -95,6 +95,8 @@ jobs:
split_repository: 'event'
- local_path: 'Component/Ddd'
split_repository: 'ddd'
- local_path: 'Component/TokenExtractor'
split_repository: 'token-extractor'

# Contracts
- local_path: 'Contracts/Config'
Expand All @@ -121,6 +123,8 @@ jobs:
split_repository: 'event-contracts'
- local_path: 'Contracts/Ddd'
split_repository: 'ddd-contracts'
- local_path: 'Contracts/TokenExtractor'
split_repository: 'token-extractor-contracts'

# Annotations
- local_path: 'Annotations/Eloquent'
Expand Down
29 changes: 29 additions & 0 deletions src/Component/TokenExtractor/BearerTokenExtractor.php
@@ -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;
}
}
38 changes: 38 additions & 0 deletions src/Component/TokenExtractor/ChainTokenExtractor.php
@@ -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;
}
}
21 changes: 21 additions & 0 deletions src/Component/TokenExtractor/QueryParamTokenExtractor.php
@@ -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;
}
}
29 changes: 29 additions & 0 deletions src/Component/TokenExtractor/composer.json
@@ -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"
}
15 changes: 15 additions & 0 deletions src/Contracts/TokenExtractor/TokenExtractorInterface.php
@@ -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;
}
28 changes: 28 additions & 0 deletions src/Contracts/TokenExtractor/composer.json
@@ -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"
}

0 comments on commit 19e8e4a

Please sign in to comment.