Skip to content

Commit 002336d

Browse files
author
aganttor
committed
Remove core hashing and serialisation logic with more secure jwt
1 parent 176981b commit 002336d

12 files changed

+108
-413
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: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,38 @@ 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+
2219
/**
23-
* @var \Hawkbit\DataStream\Serializer|null
20+
* @var mixed
2421
*/
25-
private $serializer;
22+
private $data;
2623

2724
/**
2825
* DataStream constructor.
2926
*
3027
* @param $data
31-
* @param \Hawkbit\DataStream\Serializer|null $serializer
32-
* @param \Hawkbit\DataStream\Hasher|null $hasher
3328
* @param \Hawkbit\DataStream\Compressor|null $compressor
3429
*/
35-
public function __construct($data, Serializer $serializer = null, Hasher $hasher = null, Compressor $compressor =
36-
null)
30+
public function __construct($data, Compressor $compressor = null)
3731
{
3832
$this->raw = $data;
39-
$this->hasher = $hasher ?? new Adler32Hasher();
4033
$this->compressor = $compressor ?? new DeflateCompressor();
41-
$this->serializer = $serializer ?? new JsonSerializer();
34+
$this->data = $this->decorateData($data);
35+
}
36+
37+
/**
38+
* Decorate input data to desired result
39+
*
40+
* @param $data
41+
*
42+
* @return mixed
43+
*/
44+
protected function decorateData($data){
45+
return $data;
4246
}
4347

4448
/**
@@ -50,11 +54,11 @@ public function getRaw()
5054
}
5155

5256
/**
53-
* @return \Hawkbit\DataStream\Hasher|null
57+
* @return mixed
5458
*/
55-
public function getHasher()
59+
public function getData()
5660
{
57-
return $this->hasher;
61+
return $this->data;
5862
}
5963

6064
/**
@@ -64,20 +68,4 @@ public function getCompressor()
6468
{
6569
return $this->compressor;
6670
}
67-
68-
/**
69-
* @return \Hawkbit\DataStream\Serializer|null
70-
*/
71-
public function getSerializer()
72-
{
73-
return $this->serializer;
74-
}
75-
76-
/**
77-
* @return string
78-
*/
79-
public function __toString(): string
80-
{
81-
return $this->getData();
82-
}
8371
}

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
@@ -10,17 +10,17 @@ interface DataStream
1010
const DEFAULT_INPUT = InputStream::class;
1111
const DEFAULT_OUTPUT = OutputStream::class;
1212
const MESSAGE_ESCAPE_STRING = "\0";
13+
const DEFAULT_SECRET = 'datastream';
14+
const DEFAULT_ISSUER = 'datastream';
15+
const DEFAULT_ALG = 'HS512';
1316

1417
/**
1518
* DataStream constructor.
1619
*
1720
* @param $data
18-
* @param \Hawkbit\DataStream\Serializer|null $serializer
19-
* @param \Hawkbit\DataStream\Hasher|null $hasher
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, 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: 13 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,32 @@
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()
46-
{
47-
return $this->data;
48-
}
49-
50-
/**
51-
* Get MD5 Hash fingerprint
52-
*
53-
* @return string
54-
*/
55-
public function getFingerprint(): string
19+
protected function decorateData($data)
5620
{
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);
78-
79-
$data = explode(DataStream::MESSAGE_ESCAPE_STRING, $bin, 3);
8021

81-
$this->fingerPrint = $data[0];
82-
$this->expirationTime = (int)$data[1];
83-
$payload = $data[2];
22+
// compressed jwt
23+
$compressed = base64_decode($data);
8424

85-
if ($this->getHasher()->hash($payload) !== $this->getFingerprint())
86-
{
87-
throw new \RuntimeException('Data are not equal!');
88-
}
25+
// get inflated jwt
26+
$jwt = $this->getCompressor()->uncompress($compressed);
8927

90-
if (time() > $this->getExpirationTime())
91-
{
92-
throw new \RuntimeException('Data transfer expired!');
93-
}
28+
// decode data
29+
$payload = JWT::decode($jwt, base64_encode(static::DEFAULT_SECRET), [static::DEFAULT_ALG]);
9430

95-
// transform to json
96-
return $this->getSerializer()->unserialize($payload);
31+
// return payload data
32+
// workaround to get always assoc arrays instead of objects
33+
return json_decode(json_encode($payload->data), true);
9734
}
9835
}

src/JsonSerializer.php

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

0 commit comments

Comments
 (0)