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+ }
0 commit comments