Skip to content

Commit f7ebe1b

Browse files
author
aganttor
committed
Add and implement jwt config
1 parent 002336d commit f7ebe1b

File tree

5 files changed

+200
-45
lines changed

5 files changed

+200
-45
lines changed

src/AbstractDataStream.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,23 @@ abstract class AbstractDataStream implements DataStream
2020
* @var mixed
2121
*/
2222
private $data;
23+
/**
24+
* @var \Hawkbit\DataStream\JwtConfig|null
25+
*/
26+
private $jwtConfig;
2327

2428
/**
2529
* DataStream constructor.
2630
*
2731
* @param $data
32+
* @param \Hawkbit\DataStream\JwtConfig|null $jwtConfig
2833
* @param \Hawkbit\DataStream\Compressor|null $compressor
2934
*/
30-
public function __construct($data, Compressor $compressor = null)
35+
public function __construct($data, JwtConfig $jwtConfig = null, Compressor $compressor = null)
3136
{
3237
$this->raw = $data;
3338
$this->compressor = $compressor ?? new DeflateCompressor();
39+
$this->jwtConfig = $jwtConfig ?? new JwtConfig();
3440
$this->data = $this->decorateData($data);
3541
}
3642

@@ -68,4 +74,12 @@ public function getCompressor()
6874
{
6975
return $this->compressor;
7076
}
77+
78+
/**
79+
* @return \Hawkbit\DataStream\JwtConfig|null
80+
*/
81+
public function getJwtConfig()
82+
{
83+
return $this->jwtConfig;
84+
}
7185
}

src/DataStream.php

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

66

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

1012
const DEFAULT_INPUT = InputStream::class;
1113
const DEFAULT_OUTPUT = OutputStream::class;
1214
const MESSAGE_ESCAPE_STRING = "\0";
13-
const DEFAULT_SECRET = 'datastream';
14-
const DEFAULT_ISSUER = 'datastream';
15-
const DEFAULT_ALG = 'HS512';
1615

1716
/**
1817
* DataStream constructor.
1918
*
2019
* @param $data
20+
* @param \Hawkbit\DataStream\JwtConfig|null $jwtConfig
2121
* @param \Hawkbit\DataStream\Compressor|null $compressor
2222
*/
23-
public function __construct($data, Compressor $compressor = null);
23+
public function __construct($data, JwtConfig $jwtConfig = null, Compressor $compressor = null);
2424

2525
/**
2626
* get raw data

src/InputStream.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ class InputStream extends AbstractDataStream implements DataStream
1919
protected function decorateData($data)
2020
{
2121

22+
// load jwt config
23+
$config = $this->getJwtConfig();
24+
2225
// compressed jwt
2326
$compressed = base64_decode($data);
2427

2528
// get inflated jwt
2629
$jwt = $this->getCompressor()->uncompress($compressed);
30+
$secret = $config->getSecret();
31+
$alg = $config->getAlg();
2732

2833
// decode data
29-
$payload = JWT::decode($jwt, base64_encode(static::DEFAULT_SECRET), [static::DEFAULT_ALG]);
34+
$payload = JWT::decode($jwt, $secret, [$alg]);
3035

3136
// return payload data
3237
// workaround to get always assoc arrays instead of objects

src/JwtConfig.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
4+
namespace Hawkbit\DataStream;
5+
6+
7+
final class JwtConfig
8+
{
9+
10+
const DEFAULT_SECRET = 'datastream';
11+
const DEFAULT_ISSUER = 'datastream';
12+
const DEFAULT_ALG = 'HS512';
13+
const DEFAULT_NOT_BEFORE = 0;
14+
const DEFAULT_EXPIRE_AT = 60;
15+
16+
/**
17+
* @var string
18+
*/
19+
private $issuer = self::DEFAULT_ISSUER;
20+
21+
/**
22+
* @var string
23+
*/
24+
private $secret = self::DEFAULT_SECRET;
25+
26+
/**
27+
* @var string
28+
*/
29+
private $alg = self::DEFAULT_ALG;
30+
/**
31+
* @var int
32+
*/
33+
private $notBefore = self::DEFAULT_NOT_BEFORE;
34+
/**
35+
* @var int
36+
*/
37+
private $expireAt = self::DEFAULT_EXPIRE_AT;
38+
39+
/**
40+
* @return string
41+
*/
42+
public function getIssuer(): string
43+
{
44+
return $this->issuer;
45+
}
46+
47+
/**
48+
* @param string $issuer
49+
*
50+
* @return JwtConfig
51+
*/
52+
public function setIssuer(string $issuer): JwtConfig
53+
{
54+
$this->issuer = $issuer;
55+
return $this;
56+
}
57+
58+
/**
59+
* Extract the key, which is coming from the config file.
60+
*
61+
* Best suggestion is the key to be a binary string and
62+
* store it in encoded in a config file.
63+
*
64+
* Can be generated with base64_encode(openssl_random_pseudo_bytes(64));
65+
*
66+
* keep it secure! You'll need the exact key to verify the
67+
* token later.
68+
*
69+
* @return string
70+
*/
71+
public function getSecret(): string
72+
{
73+
return base64_encode($this->secret);
74+
}
75+
76+
/**
77+
* @param string $secret
78+
*
79+
* @return JwtConfig
80+
*/
81+
public function setSecret(string $secret): JwtConfig
82+
{
83+
$this->secret = $secret;
84+
return $this;
85+
}
86+
87+
/**
88+
* Algorithm used to sign the token
89+
*
90+
* @see https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40#section-3
91+
*
92+
* @return string
93+
*/
94+
public function getAlg(): string
95+
{
96+
return $this->alg;
97+
}
98+
99+
/**
100+
* @param string $alg
101+
*
102+
* @return JwtConfig
103+
*/
104+
public function setAlg(string $alg): JwtConfig
105+
{
106+
$this->alg = $alg;
107+
return $this;
108+
}
109+
110+
/**
111+
* @return int
112+
*/
113+
public function getNotBefore(): int
114+
{
115+
return $this->getIssuedAt() + $this->notBefore;
116+
}
117+
118+
/**
119+
* @param int $notBefore
120+
*
121+
* @return JwtConfig
122+
*/
123+
public function setNotBefore(int $notBefore): JwtConfig
124+
{
125+
$this->notBefore = $notBefore;
126+
return $this;
127+
}
128+
129+
/**
130+
* @return int
131+
*/
132+
public function getExpireAt(): int
133+
{
134+
return $this->getIssuedAt() + $this->expireAt;
135+
}
136+
137+
/**
138+
* @param int $expireAt
139+
*
140+
* @return JwtConfig
141+
*/
142+
public function setExpireAt(int $expireAt): JwtConfig
143+
{
144+
$this->expireAt = $expireAt;
145+
return $this;
146+
}
147+
148+
/**
149+
* @return int
150+
*/
151+
public function getIssuedAt(): int
152+
{
153+
return time();
154+
}
155+
156+
/**
157+
* @return string
158+
*/
159+
public function getTokenId(): string
160+
{
161+
return base64_encode(random_bytes(32));
162+
}
163+
164+
}

src/OutputStream.php

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,63 +18,35 @@ class OutputStream extends AbstractDataStream
1818
*/
1919
protected function decorateData($data)
2020
{
21-
// build message
22-
$tokenId = base64_encode(random_bytes(32));
23-
$issuedAt = time();
21+
// load config
22+
$config = $this->getJwtConfig();
2423

25-
// gte $issuedAt
26-
$notBefore = $issuedAt;
27-
28-
// Adding 60 seconds
29-
$expire = $notBefore + 120;
30-
31-
// Retrieve the server name from config file
32-
$issuer = static::DEFAULT_ISSUER;
33-
34-
/*
35-
* Create the token as an array
36-
*/
24+
// Create the token as an array
3725
$payload = [
38-
'iat' => $issuedAt,
26+
'iat' => $config->getIssuedAt(),
3927
// Issued at: time when the token was generated
40-
'jti' => $tokenId,
28+
'jti' => $config->getTokenId(),
4129
// Json Token Id: an unique identifier for the token
42-
'iss' => $issuer,
30+
'iss' => $config->getIssuer(),
4331
// Issuer
44-
'nbf' => $notBefore,
32+
'nbf' => $config->getNotBefore(),
4533
// Not before
46-
'exp' => $expire,
34+
'exp' => $config->getExpireAt(),
4735
// Expire
4836
'data' => $data
4937
];
5038

51-
/*
52-
* Extract the key, which is coming from the config file.
53-
*
54-
* Best suggestion is the key to be a binary string and
55-
* store it in encoded in a config file.
56-
*
57-
* Can be generated with base64_encode(openssl_random_pseudo_bytes(64));
58-
*
59-
* keep it secure! You'll need the exact key to verify the
60-
* token later.
61-
*/
62-
$secretKey = base64_encode(static::DEFAULT_SECRET);
63-
6439
/*
6540
* Encode the array to a JWT string.
6641
* Second parameter is the key to encode the token.
6742
*
6843
* The output string can be validated at http://jwt.io/
6944
*/
70-
$jwt = JWT::encode($payload, // Data to be encoded in the JWT
71-
$secretKey, // The signing key
72-
static::DEFAULT_ALG // Algorithm used to sign the token, see https://tools.ietf
73-
//.org/html/draft-ietf-jose-json-web-algorithms-40#section-3
45+
$jwt = JWT::encode($payload,
46+
$config->getSecret(),
47+
$config->getAlg()
7448
);
7549

76-
var_dump($jwt);
77-
7850
// compress jwt
7951
$compressed = $this->getCompressor()->compress($jwt);
8052

0 commit comments

Comments
 (0)