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

little-apps / LittleJWT / 26266035191

22 May 2026 03:05AM UTC coverage: 83.376% (-0.2%) from 83.566%
26266035191

push

github

little-apps
Capitalize workflow name in CI config

Update .github/workflows/run-tests.yml to change the workflow name from 'run-tests' to 'Run Tests' for improved readability and consistency in the GitHub Actions UI.

1314 of 1576 relevant lines covered (83.38%)

300.92 hits per line

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

95.65
/src/Guards/Adapters/FingerprintAdapter.php
1
<?php
2

3
namespace LittleApps\LittleJWT\Guards\Adapters;
4

5
use Illuminate\Contracts\Auth\Authenticatable;
6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Support\Facades\Response as ResponseFactory;
9
use Illuminate\Support\Str;
10
use LittleApps\LittleJWT\JWT\JsonWebToken;
11
use LittleApps\LittleJWT\Validation\Validatables;
12

13
class FingerprintAdapter extends AbstractAdapter
14
{
15
    use Concerns\BuildsJwt;
16
    use Concerns\HasRequest;
17

18
    /**
19
     * The hash algorithm to use for the fingerprint.
20
     *
21
     * @var string
22
     */
23
    public const HASH_ALGORITHM = 'sha256';
24

25
    /**
26
     * Base adapter to add fingerprint for.
27
     */
28
    protected readonly AbstractAdapter $baseAdapter;
29

30
    /**
31
     * Intializes fingerprint adapter.
32
     *
33
     * @param  Container  $container  Application container.
34
     * @param  GenericAdapter  $adapter  Adapter to add fingerprint for.
35
     */
36
    public function __construct(Container $container, GenericAdapter $adapter)
37
    {
38
        parent::__construct($container);
154✔
39

40
        $this->baseAdapter = $adapter;
154✔
41
    }
42

43
    /**
44
     * Creates a JWT with a fingerprint hash.
45
     *
46
     * @return JsonWebToken
47
     */
48
    public function createJwtWithFingerprint(Authenticatable $user, string $fingerprintHash)
49
    {
50
        return $this->buildJwtForUser($user, [
21✔
51
            $this->getFingerprintClaimName() => $fingerprintHash,
21✔
52
        ]);
21✔
53
    }
54

55
    /**
56
     * Creates a JWT response for an Authenticatable instance.
57
     *
58
     * @param  Authenticatable  $user  The user to generate the JWT for.
59
     * @return JsonResponse Returns response with JWT
60
     */
61
    public function createJwtResponse(Authenticatable $user)
62
    {
63
        $fingerprint = $this->createFingerprint();
7✔
64

65
        $jwt = $this->createJwtWithFingerprint($user, $this->hashFingerprint($fingerprint));
7✔
66

67
        return
7✔
68
            ResponseFactory::withJwt($jwt)
7✔
69
                ->withCookie($this->getFingerprintCookieName(), $fingerprint, $this->getFingerprintCookieTtl());
7✔
70
    }
71

72
    /**
73
     * Gets the name for the fingerprint claim.
74
     *
75
     * @return string
76
     */
77
    public function getFingerprintClaimName()
78
    {
79
        return 'fgpt';
21✔
80
    }
81

82
    /**
83
     * Gets the name of the cookie that holds the fingeprint.
84
     *
85
     * @return string
86
     */
87
    public function getFingerprintCookieName()
88
    {
89
        return $this->config['cookie'] ?? 'fingerprint';
35✔
90
    }
91

92
    /**
93
     * Gets the cookies time to live.
94
     *
95
     * @return int Time to live (in minutes). 0 means forever.
96
     */
97
    public function getFingerprintCookieTtl()
98
    {
99
        return $this->config['ttl'] ?? 0;
7✔
100
    }
101

102
    /**
103
     * Gets the value of the fingerprint cookie.
104
     *
105
     * @return string|null Fingerprint value or null if cookie doesn't exist.
106
     */
107
    public function getFingerprintCookieValue()
108
    {
109
        return $this->request->cookie($this->getFingerprintCookieName());
28✔
110
    }
111

112
    /**
113
     * Generates a value for the fingerprint cookie.
114
     *
115
     * @return string
116
     */
117
    public function createFingerprint()
118
    {
119
        return (string) Str::uuid();
21✔
120
    }
121

122
    /**
123
     * Hashes a fingerprint value for use in the JWT.
124
     *
125
     * @return string
126
     */
127
    public function hashFingerprint(string $fingerprint)
128
    {
129
        return hash(static::HASH_ALGORITHM, $fingerprint);
35✔
130
    }
131

132
    /**
133
     * Gets a callback that receives  a Validator to specify the JWT validations.
134
     *
135
     * @return callable
136
     */
137
    protected function getValidatorCallback()
138
    {
139
        $fingerprintHash = $this->hashFingerprint($this->getFingerprintCookieValue() ?? '');
28✔
140

141
        $validatable = new Validatables\StackValidatable([
28✔
142
            $this->baseAdapter->getValidatorCallback(),
28✔
143
            new Validatables\FingerprintValidatable($fingerprintHash),
28✔
144
        ]);
28✔
145

146
        return $validatable;
28✔
147
    }
148

149
    /**
150
     * {@inheritDoc}
151
     */
152
    protected function getConfig(): array
153
    {
154
        return config('littlejwt.guard.adapters.fingerprint', []);
×
155
    }
156
}
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