-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from cgauge/web-token-library
Upgrade Web token library
- Loading branch information
Showing
12 changed files
with
138 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
composer.phar | ||
composer.lock | ||
/vendor/ | ||
.phpunit.result.cache | ||
.phpunit.result.cache | ||
.phpunit.cache/ |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false"> | ||
<testsuites> | ||
<testsuite name="Unit Tests"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CustomerGauge\Cognito\Contracts; | ||
|
||
use Illuminate\Contracts\Auth\Authenticatable; | ||
|
||
interface UserFactory | ||
{ | ||
public function make(array $payload): ?Authenticatable; | ||
} | ||
/** @param mixed[] $payload */ | ||
public function make(array $payload): Authenticatable|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
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
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 |
---|---|---|
@@ -1,51 +1,66 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CustomerGauge\Cognito\Testing; | ||
|
||
use InvalidArgumentException; | ||
use Jose\Component\Core\AlgorithmManager; | ||
use Jose\Component\Core\JWKSet; | ||
use Jose\Easy\Build; | ||
use Jose\Component\Core\Util\JsonConverter; | ||
use Jose\Component\Signature\Algorithm\RS256; | ||
use Jose\Component\Signature\JWSBuilder; | ||
use Jose\Component\Signature\Serializer\CompactSerializer; | ||
|
||
use function file_get_contents; | ||
use function time; | ||
|
||
final class TokenGenerator | ||
{ | ||
private $jwk; | ||
public string $jti = 'token-id'; | ||
|
||
public $jti = 'token-id'; | ||
public string $algorithm = 'RS256'; | ||
|
||
public $algorithm = 'RS256'; | ||
public string $issuer = 'https://cognito-idp.local.amazonaws.com/phpunit-pool-id'; | ||
|
||
public $issuer = 'https://cognito-idp.local.amazonaws.com/phpunit-pool-id'; | ||
public string $subject = 'testing'; | ||
|
||
public $subject = 'testing'; | ||
|
||
public function __construct(JWKSet $jwk) | ||
public function __construct(private JWKSet $jwk) | ||
{ | ||
$this->jwk = $jwk; | ||
} | ||
|
||
public static function fromFile(string $path): self | ||
{ | ||
$key = file_get_contents($path); | ||
|
||
if ($key === false) { | ||
throw new InvalidArgumentException('Invalid file'); | ||
} | ||
|
||
return new self(JWKSet::createFromJson($key)); | ||
} | ||
|
||
/** @param mixed[] $attributes */ | ||
public function sign(array $attributes): string | ||
{ | ||
$time = time(); | ||
|
||
$builder = Build::jws() | ||
->exp($time + 3600) | ||
->iat($time) | ||
->nbf($time) | ||
->jti($this->jti, true) | ||
->alg($this->algorithm) | ||
->iss($this->issuer) | ||
->sub($this->subject); | ||
|
||
foreach ($attributes as $key => $value) { | ||
$builder->claim($key, $value, true); | ||
} | ||
$algorithmManager = new AlgorithmManager([new RS256()]); | ||
$jwsBuilder = new JWSBuilder($algorithmManager); | ||
$payload = JsonConverter::encode([ | ||
'iat' => $time, | ||
'nbf' => $time, | ||
'exp' => $time + 3600, | ||
'iss' => $this->issuer, | ||
'jti' => $this->jti, | ||
'sub' => $this->subject, | ||
] + $attributes); | ||
|
||
$jws = $jwsBuilder->create() | ||
->withPayload($payload) | ||
->addSignature($this->jwk->get(0), ['alg' => $this->algorithm]) | ||
->build(); | ||
|
||
return $builder->sign($this->jwk->get(0)); | ||
return (new CompactSerializer())->serialize($jws); | ||
} | ||
} | ||
} |
Oops, something went wrong.