Skip to content

Commit 86f4a14

Browse files
committed
Version 0.1
1 parent a72bb11 commit 86f4a14

18 files changed

+291
-135
lines changed

src/Business/Provider/SecurityProvider.php

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,90 @@
22

33
namespace Micro\Plugin\Security\Business\Provider;
44

5-
use Micro\Plugin\Security\Business\Token\Context\TokenConfiguration;
5+
use Micro\Plugin\Security\Business\Token\Decoder\DecoderFactoryInterface;
66
use Micro\Plugin\Security\Configuration\Provider\ProviderConfigurationInterface;
7+
use Micro\Plugin\Security\Exception\TokenExpiredException;
8+
use Micro\Plugin\Security\Business\Token\Encoder\EncoderFactoryInterface;
9+
use Micro\Plugin\Security\Token\Token;
710
use Micro\Plugin\Security\Token\TokenInterface;
811

912
class SecurityProvider implements SecurityProviderInterface
1013
{
1114
/**
15+
* @param EncoderFactoryInterface $encoderFactory
16+
* @param DecoderFactoryInterface $decoderFactory
1217
* @param ProviderConfigurationInterface $providerConfiguration
1318
*/
1419
public function __construct(
15-
private readonly ProviderConfigurationInterface $providerConfiguration
20+
private readonly EncoderFactoryInterface $encoderFactory,
21+
private readonly DecoderFactoryInterface $decoderFactory,
22+
private readonly ProviderConfigurationInterface $providerConfiguration,
1623
)
1724
{
1825
}
1926

20-
public function generateToken(TokenConfiguration $tokenConfiguration): TokenInterface
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public function generateToken(array $sourceData, int $lifetime = null): TokenInterface
2131
{
22-
// TODO: Implement generateToken() method.
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+
41+
$generatedTokenString = $this->encoderFactory
42+
->create($this->providerConfiguration)
43+
->encode($tokenContainerData);
44+
45+
return $this->createToken(
46+
$generatedTokenString,
47+
$createdAt,
48+
$lifetime,
49+
$sourceData
50+
);
2351
}
2452

53+
/**
54+
* {@inheritDoc}
55+
*/
2556
public function decodeToken(string $encoded): TokenInterface
2657
{
27-
// TODO: Implement decodeToken() method.
58+
$decoded = $this->decoderFactory
59+
->create($this->providerConfiguration)
60+
->decode($encoded);
61+
62+
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]
67+
);
2868
}
2969

3070
/**
31-
* @return string
71+
* @param string $encoded
72+
* @param int $createdAt
73+
* @param int $lifetime
74+
* @param array $tokenData
75+
*
76+
* @return Token
3277
*/
33-
public function getName(): string
78+
protected function createToken(string $encoded, int $createdAt, int $lifetime, array $tokenData): TokenInterface
3479
{
35-
return $this->getName();
80+
if($lifetime > 0 && (time() > $lifetime + $createdAt)) {
81+
throw new TokenExpiredException($encoded);
82+
}
83+
84+
return new Token(
85+
source: $encoded,
86+
createdAt: $createdAt,
87+
lifetime: $lifetime,
88+
parameters: $tokenData
89+
);
3690
}
3791
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Security\Business\Provider;
4+
5+
use Micro\Plugin\Security\Business\Token\Decoder\DecoderFactoryInterface;
6+
use Micro\Plugin\Security\Configuration\SecurityPluginConfigurationInterface;
7+
use Micro\Plugin\Security\Business\Token\Encoder\EncoderFactoryInterface;
8+
9+
class SecurityProviderFactory implements SecurityProviderFactoryInterface
10+
{
11+
/**
12+
* @param EncoderFactoryInterface $encoderFactory
13+
* @param DecoderFactoryInterface $decoderFactory
14+
* @param SecurityPluginConfigurationInterface $securityPluginConfiguration
15+
*/
16+
public function __construct(
17+
private readonly EncoderFactoryInterface $encoderFactory,
18+
private readonly DecoderFactoryInterface $decoderFactory,
19+
private readonly SecurityPluginConfigurationInterface $securityPluginConfiguration
20+
)
21+
{
22+
}
23+
24+
/**
25+
* @param string $providerName
26+
*
27+
* @return SecurityProviderInterface
28+
*/
29+
public function create(string $providerName): SecurityProviderInterface
30+
{
31+
return new SecurityProvider(
32+
$this->encoderFactory,
33+
$this->decoderFactory,
34+
$this->securityPluginConfiguration->getProviderConfiguration($providerName)
35+
);
36+
}
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Security\Business\Provider;
4+
5+
interface SecurityProviderFactoryInterface
6+
{
7+
/**
8+
* @param string $providerName
9+
*
10+
* @return SecurityProviderInterface
11+
*/
12+
public function create(string $providerName): SecurityProviderInterface;
13+
}

src/Business/Provider/SecurityProviderInterface.php

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

55

6+
use Micro\Plugin\Security\Token\TokenInterface;
7+
68
interface SecurityProviderInterface
79
{
810
/**
9-
* @return string
11+
* @param array $sourceData
12+
* @param int|null $lifetime
13+
*
14+
* @return TokenInterface
15+
*/
16+
public function generateToken(array $sourceData, int $lifetime = null): TokenInterface;
17+
18+
/**
19+
* @param string $encoded
20+
*
21+
* @return TokenInterface
1022
*/
11-
public function getName(): string;
23+
public function decodeToken(string $encoded): TokenInterface;
1224
}

src/Business/Token/Decoder/DecoderFactoryInterface.php

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

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

5+
use Micro\Plugin\Security\Configuration\Provider\ProviderConfigurationInterface;
6+
57
interface DecoderFactoryInterface
68
{
79
/**
10+
* @param ProviderConfigurationInterface $providerConfiguration
11+
*
812
* @return DecoderInterface
913
*/
10-
public function create(): DecoderInterface;
14+
public function create(ProviderConfigurationInterface $providerConfiguration): DecoderInterface;
1115
}

src/Business/Token/Decoder/JWTDecoderFactory.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

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

5+
use Micro\Plugin\Security\Configuration\Provider\ProviderConfigurationInterface;
6+
57
class JWTDecoderFactory implements DecoderFactoryInterface
68
{
79
/**
810
* {@inheritDoc}
911
*/
10-
public function create(): DecoderInterface
12+
public function create(ProviderConfigurationInterface $providerConfiguration): DecoderInterface
1113
{
12-
return new JWTDecoder();
14+
return new JWTDecoder(
15+
publicKey: $providerConfiguration->getPublicKey(),
16+
encryptAlgorithm: $providerConfiguration->getEncryptionAlgorithm()
17+
);
1318
}
1419
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?php
22

3-
namespace Micro\Plugin\Security\Token\Encoder;
3+
namespace Micro\Plugin\Security\Business\Token\Encoder;
4+
5+
use Micro\Plugin\Security\Configuration\Provider\ProviderConfigurationInterface;
46

57
interface EncoderFactoryInterface
68
{
79
/**
10+
* @param ProviderConfigurationInterface $providerConfiguration
11+
*
812
* @return EncoderInterface
913
*/
10-
public function create(): EncoderInterface;
14+
public function create(ProviderConfigurationInterface $providerConfiguration): EncoderInterface;
1115
}

src/Business/Token/Encoder/EncoderInterface.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\Token\Encoder;
3+
namespace Micro\Plugin\Security\Business\Token\Encoder;
44

55
interface EncoderInterface
66
{

src/Business/Token/Encoder/JWTEncoder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<?php
22

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

55
use Firebase\JWT\JWT;
66

77
class JWTEncoder implements EncoderInterface
88
{
99
/**
1010
* @param string $privateKey
11-
* @param string $passphrase
1211
* @param string $encryptAlgorithm
12+
* @param string|null $passphrase
1313
*/
1414
public function __construct(
1515
private readonly string $privateKey,
16-
private readonly string $passphrase,
1716
private readonly string $encryptAlgorithm,
17+
private readonly null|string $passphrase = null,
1818
)
1919
{
2020
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Security\Business\Token\Encoder;
4+
5+
use Micro\Plugin\Security\Configuration\Provider\ProviderConfigurationInterface;
6+
7+
class JWTEncoderFactory implements EncoderFactoryInterface
8+
{
9+
/**
10+
* {@inheritDoc}
11+
*/
12+
public function create(ProviderConfigurationInterface $providerConfiguration): EncoderInterface
13+
{
14+
return new JWTEncoder(
15+
privateKey: $providerConfiguration->getSecretKey(),
16+
encryptAlgorithm: $providerConfiguration->getEncryptionAlgorithm(),
17+
passphrase: $providerConfiguration->getPassphrase(),
18+
);
19+
}
20+
}

0 commit comments

Comments
 (0)