Skip to content

Commit 41a74a0

Browse files
authored
Merge pull request #1 from Micro-PHP/v1.0
v1.0
2 parents 86f4a14 + bf21566 commit 41a74a0

15 files changed

+70
-108
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Micro Framework: Security component",
44
"type": "library",
55
"license": "MIT",
6-
"version": "0.1",
6+
"version": "1.0",
77
"autoload": {
88
"psr-4": {
99
"Micro\\Plugin\\Security\\": "src/"

src/Business/Provider/SecurityProvider.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Micro\Plugin\Security\Business\Token\Decoder\DecoderFactoryInterface;
66
use Micro\Plugin\Security\Configuration\Provider\ProviderConfigurationInterface;
7-
use Micro\Plugin\Security\Exception\TokenExpiredException;
87
use Micro\Plugin\Security\Business\Token\Encoder\EncoderFactoryInterface;
98
use Micro\Plugin\Security\Token\Token;
109
use Micro\Plugin\Security\Token\TokenInterface;
@@ -27,25 +26,14 @@ public function __construct(
2726
/**
2827
* {@inheritDoc}
2928
*/
30-
public function generateToken(array $sourceData, int $lifetime = null): TokenInterface
29+
public function generateToken(array $sourceData): TokenInterface
3130
{
32-
$createdAt = time();
33-
$lifetime = $lifetime ?: $this->providerConfiguration->getLifetimeDefault();
34-
35-
$tokenContainerData = [
36-
TokenInterface::TOKEN_PARAM_DATA => $sourceData,
37-
TokenInterface::TOKEN_PARAM_LIFETIME => $lifetime,
38-
TokenInterface::TOKEN_PARAM_CREATED_AT => $createdAt,
39-
];
40-
4131
$generatedTokenString = $this->encoderFactory
4232
->create($this->providerConfiguration)
43-
->encode($tokenContainerData);
33+
->encode($sourceData);
4434

4535
return $this->createToken(
4636
$generatedTokenString,
47-
$createdAt,
48-
$lifetime,
4937
$sourceData
5038
);
5139
}
@@ -60,31 +48,21 @@ public function decodeToken(string $encoded): TokenInterface
6048
->decode($encoded);
6149

6250
return $this->createToken(
63-
$encoded,
64-
createdAt: $decoded[TokenInterface::TOKEN_PARAM_CREATED_AT ],
65-
lifetime: $decoded[TokenInterface::TOKEN_PARAM_LIFETIME],
66-
tokenData: (array) $decoded[TokenInterface::TOKEN_PARAM_DATA]
51+
encoded: $encoded,
52+
tokenData: $decoded
6753
);
6854
}
6955

7056
/**
7157
* @param string $encoded
72-
* @param int $createdAt
73-
* @param int $lifetime
7458
* @param array $tokenData
7559
*
7660
* @return Token
7761
*/
78-
protected function createToken(string $encoded, int $createdAt, int $lifetime, array $tokenData): TokenInterface
62+
protected function createToken(string $encoded, array $tokenData): TokenInterface
7963
{
80-
if($lifetime > 0 && (time() > $lifetime + $createdAt)) {
81-
throw new TokenExpiredException($encoded);
82-
}
83-
8464
return new Token(
8565
source: $encoded,
86-
createdAt: $createdAt,
87-
lifetime: $lifetime,
8866
parameters: $tokenData
8967
);
9068
}

src/Business/Provider/SecurityProviderInterface.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,36 @@
33
namespace Micro\Plugin\Security\Business\Provider;
44

55

6+
use DomainException;
7+
use UnexpectedValueException;
8+
use Firebase\JWT\BeforeValidException;
9+
use Firebase\JWT\ExpiredException;
10+
use Firebase\JWT\SignatureInvalidException;
611
use Micro\Plugin\Security\Token\TokenInterface;
12+
use InvalidArgumentException;
713

814
interface SecurityProviderInterface
915
{
1016
/**
1117
* @param array $sourceData
12-
* @param int|null $lifetime
1318
*
1419
* @return TokenInterface
1520
*/
16-
public function generateToken(array $sourceData, int $lifetime = null): TokenInterface;
21+
public function generateToken(array $sourceData): TokenInterface;
1722

1823
/**
1924
* @param string $encoded
2025
*
2126
* @return TokenInterface
27+
*
28+
* @throws InvalidArgumentException Provided key/key-array was empty or malformed
29+
* @throws DomainException Provided JWT is malformed
30+
* @throws UnexpectedValueException Provided JWT was invalid
31+
* @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
32+
* @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
33+
* @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
34+
* @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
35+
*
2236
*/
2337
public function decodeToken(string $encoded): TokenInterface;
2438
}

src/Business/Token/Configuration/TokenConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Micro\Plugin\Security\Business\Token\Context;
3+
namespace Micro\Plugin\Security\Business\Token\Configuration;
44

55
class TokenConfiguration
66
{

src/Business/Token/Decoder/DecoderInterface.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@
22

33
namespace Micro\Plugin\Security\Business\Token\Decoder;
44

5-
use Micro\Plugin\Security\Token\TokenInterface;
5+
use DomainException;
6+
use UnexpectedValueException;
7+
use Firebase\JWT\BeforeValidException;
8+
use Firebase\JWT\ExpiredException;
9+
use Firebase\JWT\SignatureInvalidException;
610

711
interface DecoderInterface
812
{
913
/**
1014
* @param string $encodedToken
1115
*
1216
* @return array
17+
*
18+
* @throws InvalidArgumentException Provided key/key-array was empty or malformed
19+
* @throws DomainException Provided JWT is malformed
20+
* @throws UnexpectedValueException Provided JWT was invalid
21+
* @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
22+
* @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
23+
* @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
24+
* @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
1325
*/
1426
public function decode(string $encodedToken): array;
1527
}

src/Configuration/Provider/ProviderConfiguration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function getEncryptionAlgorithm(): string
2727
public function getSecretKey(): string
2828
{
2929
$result = $this->get(self::CFG_PROVIDER_SECRET_KEY);
30-
if(!$result && $this->getEncryptionAlgorithm() === self::HS256) {
30+
if(!$result && $this->getEncryptionAlgorithm() === self::ALGO_DEFAULT) {
3131
return self::SECRET_DEFAULT;
3232
}
3333

@@ -40,7 +40,7 @@ public function getSecretKey(): string
4040
public function getPublicKey(): string
4141
{
4242
$result = $this->get(self::CFG_PROVIDER_PUB_KEY);
43-
if(!$result && $this->getEncryptionAlgorithm() === self::HS256) {
43+
if(!$result && $this->getEncryptionAlgorithm() === self::ALGO_DEFAULT) {
4444
return $this->getSecretKey();
4545
}
4646

src/Configuration/Provider/ProviderConfigurationInterface.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
interface ProviderConfigurationInterface
66
{
7-
const ALGO_EDDSA = 'EdDSA';
8-
const ALGO_RS256 = 'RS256';
9-
const HS256 = 'HS256';
10-
11-
const ALGO_DEFAULT = self::HS256;
7+
const ALGO_DEFAULT = 'HS256';
128
const SECRET_DEFAULT = 'default_secret_phrase';
139

1410
/**
11+
* Supported algorithms are 'ES384','ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
12+
*
1513
* @return string
1614
*/
1715
public function getEncryptionAlgorithm(): string;

src/Configuration/SecurityPluginConfigurationInterface.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@ interface SecurityPluginConfigurationInterface
99

1010
const PROVIDER_DEFAULT = 'default';
1111

12-
/**
13-
* @return array<string>
14-
*/
15-
public function getProviderList(): array;
16-
1712
/**
1813
* @param string $providerName
1914
*
2015
* @return ProviderConfigurationInterface
2116
*/
22-
public function getProviderConfiguration(string $providerName): ProviderConfigurationInterface;
17+
public function getProviderConfiguration(string $providerName = self::PROVIDER_DEFAULT): ProviderConfigurationInterface;
2318
}

src/Exception/TokenExpiredException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class TokenExpiredException extends SecurityException
66
{
7-
public function __construct(string $token) {
8-
parent::__construct(sprintf('Token "%s" was expired.', $token));
7+
public function __construct(string $token, \Throwable $throwable = null) {
8+
parent::__construct(sprintf('Token "%s" was expired.', $token), 0, $throwable);
99
}
1010
}

src/Facade/SecurityFacade.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@ class SecurityFacade implements SecurityFacadeInterface
1111

1212
public function __construct(private readonly SecurityProviderFactoryInterface $securityProviderFactory)
1313
{
14-
1514
}
1615

1716
/**
1817
* {@inheritDoc}
1918
*/
20-
public function generateToken(array $parameters, string $providerName = null, int $lifeTime = null): TokenInterface
19+
public function generateToken(array $parameters, string $providerName = null): TokenInterface
2120
{
2221
if(!$providerName) {
2322
$providerName = SecurityPluginConfigurationInterface::PROVIDER_DEFAULT;
2423
}
2524

2625
return $this->securityProviderFactory
2726
->create($providerName)
28-
->generateToken($parameters, $lifeTime);
27+
->generateToken($parameters);
2928
}
3029

3130
/**

0 commit comments

Comments
 (0)