• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

valkyrjaio / valkyrja / 15659546660

15 Jun 2025 04:39AM UTC coverage: 47.202% (-0.4%) from 47.589%
15659546660

push

github

MelechMizrachi
Update Config.

5331 of 11294 relevant lines covered (47.2%)

16.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Valkyrja/Auth/Repository/JwtRepository.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <melechmizrachi@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Valkyrja\Auth\Repository;
15

16
use Valkyrja\Auth\Adapter\Contract\Adapter;
17
use Valkyrja\Auth\Config;
18
use Valkyrja\Auth\Entity\Contract\User;
19
use Valkyrja\Auth\Exception\TokenizationException;
20
use Valkyrja\Auth\Model\Contract\AuthenticatedUsers;
21
use Valkyrja\Auth\Repository\Contract\JWTRepository as Contract;
22
use Valkyrja\Exception\InvalidArgumentException;
23
use Valkyrja\Jwt\Contract\Jwt as JwtManager;
24
use Valkyrja\Jwt\Driver\Contract\Driver as Jwt;
25
use Valkyrja\Session\Contract\Session;
26

27
/**
28
 * Class JwtRepository.
29
 *
30
 * @author Melech Mizrachi
31
 */
32
class JwtRepository extends TokenizedRepository implements Contract
33
{
34
    /**
35
     * The Jwt.
36
     *
37
     * @var Jwt
38
     */
39
    protected Jwt $jwt;
40

41
    /**
42
     * JWTRepository constructor.
43
     *
44
     * @param class-string<User> $user The user class
45
     */
46
    public function __construct(Adapter $adapter, JwtManager $jwt, Session $session, Config $config, string $user)
47
    {
48
        parent::__construct($adapter, $session, $config, $user);
×
49

50
        $this->jwt = $jwt->use();
×
51
    }
52

53
    /**
54
     * @inheritDoc
55
     */
56
    protected function tokenizeUsers(AuthenticatedUsers $users): string
57
    {
58
        return $this->jwt->encode($users->asArray());
×
59
    }
60

61
    /**
62
     * @inheritDoc
63
     */
64
    protected function unTokenizeUsers(string $token): AuthenticatedUsers
65
    {
66
        $decodedToken = $this->jwt->decode($token);
×
67

68
        if (! is_string(array_key_first($decodedToken))) {
×
69
            throw new InvalidArgumentException('Provided token is invalid');
×
70
        }
71

72
        /** @var array<string, mixed> $decodedToken */
73

74
        return $this->usersModel::fromArray($decodedToken);
×
75
    }
76

77
    /**
78
     * @inheritDoc
79
     */
80
    protected function getRequiredFields(): array
81
    {
82
        if ($this->config->shouldAlwaysAuthenticate) {
×
83
            throw new TokenizationException(
×
84
                'alwaysAuthenticate setting is turned on in config. '
×
85
                . 'This will result in exposed password and other sensitive user fields in an unsecured JWT. '
×
86
                . 'Please use the ' . JwtCryptRepository::class . ' Repository instead.'
×
87
            );
×
88
        }
89

90
        return parent::getRequiredFields();
×
91
    }
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc