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

valkyrjaio / valkyrja / 16039388639

03 Jul 2025 01:23AM UTC coverage: 39.501% (+0.5%) from 38.991%
16039388639

push

github

MelechMizrachi
PHP CS Fixer: Fixing styling issues.

4052 of 10258 relevant lines covered (39.5%)

4.96 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\Session\Contract\Session;
25

26
use function is_string;
27

28
/**
29
 * Class JwtRepository.
30
 *
31
 * @author Melech Mizrachi
32
 */
33
class JwtRepository extends TokenizedRepository implements Contract
34
{
35
    /**
36
     * JWTRepository constructor.
37
     *
38
     * @param class-string<User> $user The user class
39
     */
40
    public function __construct(
41
        Adapter $adapter,
42
        protected JwtManager $jwt,
43
        Session $session,
44
        Config $config,
45
        string $user
46
    ) {
47
        parent::__construct($adapter, $session, $config, $user);
×
48
    }
49

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

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

65
        if (! is_string(array_key_first($decodedToken))) {
×
66
            throw new InvalidArgumentException('Provided token is invalid');
×
67
        }
68

69
        /** @var array<string, mixed> $decodedToken */
70

71
        return $this->usersModel::fromArray($decodedToken);
×
72
    }
73

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

87
        return parent::getRequiredFields();
×
88
    }
89
}
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