22
33namespace Micro \Plugin \Security \Business \Provider ;
44
5- use Micro \Plugin \Security \Business \Token \Context \ TokenConfiguration ;
5+ use Micro \Plugin \Security \Business \Token \Decoder \ DecoderFactoryInterface ;
66use 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 ;
710use Micro \Plugin \Security \Token \TokenInterface ;
811
912class 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}
0 commit comments