44namespace Hawkbit \DataStream ;
55
66
7+ use Firebase \JWT \JWT ;
8+
79class InputStream extends AbstractDataStream implements DataStream
810{
911
1012 /**
11- * @var string
12- */
13- private $ fingerPrint ;
14-
15- /**
16- * @var int
17- */
18- private $ expirationTime ;
19-
20- /**
21- * @var mixed
22- */
23- private $ data ;
24-
25- /**
26- * DataStream constructor.
13+ * Load data from compressed jwt
2714 *
2815 * @param $data
29- * @param \Hawkbit\DataStream\Serializer|null $serializer
30- * @param \Hawkbit\DataStream\Hasher|null $hasher
31- * @param \Hawkbit\DataStream\Compressor|null $compressor
32- */
33- public function __construct ($ data , Serializer $ serializer = null , Hasher $ hasher = null , Compressor $ compressor = null )
34- {
35- parent ::__construct ($ data , $ serializer , $ hasher , $ compressor );
36- $ this ->data = $ this ->parse ();
37- }
38-
39-
40- /**
41- * Get converted data
4216 *
4317 * @return mixed
4418 */
45- public function getData ( )
19+ protected function decorateData ( $ data )
4620 {
47- return $ this ->data ;
48- }
49-
50- /**
51- * Get MD5 Hash fingerprint
52- *
53- * @return string
54- */
55- public function getFingerprint (): string
56- {
57- return $ this ->fingerPrint ;
58- }
59-
60- /**
61- * Get expiration for data
62- *
63- * @return int
64- */
65- public function getExpirationTime (): int
66- {
67- return $ this ->expirationTime ;
68- }
69-
70- private function parse ()
71- {
72-
73- // hex data
74- $ stream = base64_decode ($ this ->getRaw ());
75-
76- // get binary representation
77- $ bin = @$ this ->getCompressor ()->uncompress ($ stream );
7821
79- $ data = explode (DataStream::MESSAGE_ESCAPE_STRING , $ bin , 3 );
22+ // load jwt config
23+ $ config = $ this ->getJwtConfig ();
8024
81- $ this ->fingerPrint = $ data [0 ];
82- $ this ->expirationTime = (int )$ data [1 ];
83- $ payload = $ data [2 ];
25+ // compressed jwt
26+ $ compressed = base64_decode ($ data );
8427
85- if ( $ this -> getHasher ()-> hash ( $ payload ) !== $ this -> getFingerprint ())
86- {
87- throw new \ RuntimeException ( ' Data are not equal! ' );
88- }
28+ // get inflated jwt
29+ $ jwt = $ this -> getCompressor ()-> uncompress ( $ compressed );
30+ $ secret = $ config -> getSecret ( );
31+ $ alg = $ config -> getAlg ();
8932
90- if (time () > $ this ->getExpirationTime ())
91- {
92- throw new \RuntimeException ('Data transfer expired! ' );
93- }
33+ // decode data
34+ $ payload = JWT ::decode ($ jwt , $ secret , [$ alg ]);
9435
95- // transform to json
96- return $ this ->getSerializer ()->unserialize ($ payload );
36+ // return payload data
37+ // workaround to get always assoc arrays instead of objects
38+ return json_decode (json_encode ($ payload ->data ), true );
9739 }
9840}
0 commit comments