Skip to content

Commit 3384dd3

Browse files
committed
Store secret JWT API token into a DB table
1 parent 28b1c0d commit 3384dd3

File tree

9 files changed

+84
-16
lines changed

9 files changed

+84
-16
lines changed

.env.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
APP_URL="http://localhost:8080"
33

44
# JSON Web Token
5-
JWT_KEY=""
65
JWT_TOKEN_EXPIRATION="86400" # in seconds
76
JWT_ALGO_ENCRYPTION="HS512"
87

src/Dal/FoodItemDal.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use PH7\ApiSimpleMenu\Entity\Item as ItemEntity;
66
use RedBeanPHP\R;
77

8-
class FoodItemDal
8+
final class FoodItemDal
99
{
1010
public const TABLE_NAME = 'fooditems'; // Cannot have underscore. Use one word
1111

@@ -46,8 +46,6 @@ public static function createDefaultItem(ItemEntity $itemEntity): int|string
4646
{
4747
$itemBan = R::dispense(self::TABLE_NAME);
4848

49-
// TODO Move this into Food Entity to build the item
50-
5149
$itemBan->item_uuid = $itemEntity->getItemUuid();
5250
$itemBan->name = $itemEntity->getName();
5351
$itemBan->price = $itemEntity->getPrice();

src/Dal/TokenKeyDal.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PH7\ApiSimpleMenu\Dal;
4+
5+
use RedBeanPHP\R;
6+
7+
final class TokenKeyDal
8+
{
9+
public const TABLE_NAME = 'secretkeys';
10+
11+
public static function saveSecretKey(string $jwtKey)
12+
{
13+
$tokenBean = R::dispense(self::TABLE_NAME);
14+
$tokenBean->secretKey = $jwtKey;
15+
16+
R::store($tokenBean);
17+
18+
// close connection with database
19+
R::close();
20+
}
21+
22+
public static function getSecretKey(): ?string
23+
{
24+
$tokenKeyBean = R::load(self::TABLE_NAME, 1);
25+
26+
return $tokenKeyBean?->secretKey;
27+
}
28+
}

src/Entity/Entitable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface Entitable
66
{
77
public function unserialize(?array $data): self;
88

9-
public function setSequentialId(int $sequentialId): void;
9+
public function setSequentialId(int $sequentialId): self;
1010

1111
public function getSequentialId(): int;
1212
}

src/Entity/Item.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,59 @@ class Item implements Entitable
1414

1515
private bool $available;
1616

17-
public function setSequentialId(int $sequentialId): void
17+
public function setSequentialId(int $sequentialId): self
1818
{
1919
$this->sequentialId = $sequentialId;
20+
21+
return $this;
2022
}
2123

2224
public function getSequentialId(): int
2325
{
2426
return $this->sequentialId;
2527
}
2628

27-
public function setItemUuid(string $itemUuid): void
29+
public function setItemUuid(string $itemUuid): self
2830
{
2931
$this->itemUuid = $itemUuid;
32+
33+
return $this;
3034
}
3135

3236
public function getItemUuid(): ?string
3337
{
3438
return $this->itemUuid;
3539
}
3640

37-
public function setName(string $name): void
41+
public function setName(string $name): self
3842
{
3943
$this->name = $name;
44+
45+
return $this;
4046
}
4147

4248
public function getName(): string
4349
{
4450
return $this->name;
4551
}
4652

47-
public function setPrice(float $price): void
53+
public function setPrice(float $price): self
4854
{
4955
$this->price = $price;
56+
57+
return $this;
5058
}
5159

5260
public function getPrice(): float
5361
{
5462
return $this->price;
5563
}
5664

57-
public function setAvailable(bool $available): void
65+
public function setAvailable(bool $available): self
5866
{
5967
$this->available = $available;
68+
69+
return $this;
6070
}
6171

6272
public function getAvailable(): bool

src/Route/user.routes.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PH7\ApiSimpleMenu\Route\Exception\NotFoundException;
55
use PH7\ApiSimpleMenu\Service\Exception\EmailExistsException;
6+
use PH7\ApiSimpleMenu\Service\SecretKey;
67
use PH7\ApiSimpleMenu\Service\User;
78
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
89

@@ -29,7 +30,10 @@ public function getResponse(): string
2930
// Ternary conditional operator operator
3031
$userId = $_REQUEST['id'] ?? ''; // using the null coalescing operator
3132

32-
$user = new User();
33+
// retrieve JWT secret key, and pass it to User Service' constructor
34+
$jwtToken = SecretKey::getJwtSecretKey();
35+
$user = new User($jwtToken);
36+
3337
try {
3438
// check first if HTTP method for the requested endpoint is valid
3539
$expectHttpMethod = match ($this) {

src/Service/FoodItem.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ public function retrieveAll(): array
3838
// if no items have been added yet, create the first one
3939
$itemUuid = Uuid::uuid4()->toString();
4040
$itemEntity = new ItemEntity();
41-
$itemEntity->setItemUuid($itemUuid);
42-
$itemEntity->setName('Burrito Cheese with French Fries');
43-
$itemEntity->setPrice(19.99);
44-
$itemEntity->setAvailable(true);
41+
42+
// chaining each method with the arrow ->
43+
$itemEntity
44+
->setItemUuid($itemUuid)
45+
->setName('Burrito Cheese with French Fries')
46+
->setPrice(19.99)
47+
->setAvailable(true);
4548

4649
FoodItemDal::createDefaultItem($itemEntity);
4750

src/Service/SecretKey.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace PH7\ApiSimpleMenu\Service;
4+
5+
use PH7\ApiSimpleMenu\Dal\TokenKeyDal;
6+
7+
class SecretKey
8+
{
9+
public static function getJwtSecretKey(): string
10+
{
11+
$jwtKey = TokenKeyDal::getSecretKey();
12+
13+
if (!$jwtKey) {
14+
$uniqueSecretKey = hash('sha512', strval(time()));
15+
TokenKeyDal::saveSecretKey($uniqueSecretKey);
16+
17+
return $uniqueSecretKey;
18+
}
19+
20+
return $jwtKey;
21+
}
22+
}

src/Service/User.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class User
1717
{
1818
public const DATE_TIME_FORMAT = 'Y-m-d H:i:s';
1919

20+
public function __construct(protected string $jwtKey)
21+
{
22+
}
23+
2024
public function login(mixed $data): array
2125
{
2226
$userValidation = new UserValidation($data);
@@ -38,7 +42,7 @@ public function login(mixed $data): array
3842
'name' => $userName
3943
]
4044
],
41-
$_ENV['JWT_KEY'],
45+
$this->jwtKey,
4246
$_ENV['JWT_ALGO_ENCRYPTION']
4347
);
4448

0 commit comments

Comments
 (0)