Skip to content

Commit 4491147

Browse files
authored
Merge pull request #1 from HawkBitPhp/feature/jwt
Remove core hashing and serialisation logic with more secure jwt
2 parents 176981b + f7ebe1b commit 4491147

13 files changed

+258
-408
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=7.0"
20+
"php": ">=7.0",
21+
"firebase/php-jwt": "~5.0"
2122
},
2223
"require-dev": {
2324
"phpunit/phpunit": "~6.0"

src/AbstractDataStream.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,44 @@ abstract class AbstractDataStream implements DataStream
1111
*/
1212
private $raw;
1313

14-
/**
15-
* @var \Hawkbit\DataStream\Hasher|null
16-
*/
17-
private $hasher;
1814
/**
1915
* @var \Hawkbit\DataStream\Compressor|null
2016
*/
2117
private $compressor;
18+
19+
/**
20+
* @var mixed
21+
*/
22+
private $data;
2223
/**
23-
* @var \Hawkbit\DataStream\Serializer|null
24+
* @var \Hawkbit\DataStream\JwtConfig|null
2425
*/
25-
private $serializer;
26+
private $jwtConfig;
2627

2728
/**
2829
* DataStream constructor.
2930
*
3031
* @param $data
31-
* @param \Hawkbit\DataStream\Serializer|null $serializer
32-
* @param \Hawkbit\DataStream\Hasher|null $hasher
32+
* @param \Hawkbit\DataStream\JwtConfig|null $jwtConfig
3333
* @param \Hawkbit\DataStream\Compressor|null $compressor
3434
*/
35-
public function __construct($data, Serializer $serializer = null, Hasher $hasher = null, Compressor $compressor =
36-
null)
35+
public function __construct($data, JwtConfig $jwtConfig = null, Compressor $compressor = null)
3736
{
3837
$this->raw = $data;
39-
$this->hasher = $hasher ?? new Adler32Hasher();
4038
$this->compressor = $compressor ?? new DeflateCompressor();
41-
$this->serializer = $serializer ?? new JsonSerializer();
39+
$this->jwtConfig = $jwtConfig ?? new JwtConfig();
40+
$this->data = $this->decorateData($data);
41+
}
42+
43+
/**
44+
* Decorate input data to desired result
45+
*
46+
* @param $data
47+
*
48+
* @return mixed
49+
*/
50+
protected function decorateData($data){
51+
return $data;
4252
}
4353

4454
/**
@@ -50,11 +60,11 @@ public function getRaw()
5060
}
5161

5262
/**
53-
* @return \Hawkbit\DataStream\Hasher|null
63+
* @return mixed
5464
*/
55-
public function getHasher()
65+
public function getData()
5666
{
57-
return $this->hasher;
67+
return $this->data;
5868
}
5969

6070
/**
@@ -66,18 +76,10 @@ public function getCompressor()
6676
}
6777

6878
/**
69-
* @return \Hawkbit\DataStream\Serializer|null
70-
*/
71-
public function getSerializer()
72-
{
73-
return $this->serializer;
74-
}
75-
76-
/**
77-
* @return string
79+
* @return \Hawkbit\DataStream\JwtConfig|null
7880
*/
79-
public function __toString(): string
81+
public function getJwtConfig()
8082
{
81-
return $this->getData();
83+
return $this->jwtConfig;
8284
}
8385
}

src/Adler32Hasher.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/DataStream.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace Hawkbit\DataStream;
55

66

7+
use Firebase\JWT\JWT;
8+
79
interface DataStream
810
{
911

@@ -15,12 +17,10 @@ interface DataStream
1517
* DataStream constructor.
1618
*
1719
* @param $data
18-
* @param \Hawkbit\DataStream\Serializer|null $serializer
19-
* @param \Hawkbit\DataStream\Hasher|null $hasher
20+
* @param \Hawkbit\DataStream\JwtConfig|null $jwtConfig
2021
* @param \Hawkbit\DataStream\Compressor|null $compressor
2122
*/
22-
public function __construct($data, Serializer $serializer = null, Hasher $hasher = null, Compressor $compressor =
23-
null);
23+
public function __construct($data, JwtConfig $jwtConfig = null, Compressor $compressor = null);
2424

2525
/**
2626
* get raw data
@@ -36,24 +36,5 @@ public function getRaw();
3636
*/
3737
public function getData();
3838

39-
/**
40-
* Get MD5 Hash fingerprint
41-
*
42-
* @return string
43-
*/
44-
public function getFingerprint(): string;
45-
46-
/**
47-
* Get expiration for data
48-
*
49-
* @return int
50-
*/
51-
public function getExpirationTime(): int;
52-
53-
/**
54-
* @return string
55-
*/
56-
public function __toString(): string;
57-
5839

5940
}

src/Hasher.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/InputStream.php

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,37 @@
44
namespace Hawkbit\DataStream;
55

66

7+
use Firebase\JWT\JWT;
8+
79
class 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
}

src/JsonSerializer.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)