Completed
Push — develop ( 7bb86a...82de8f )
by Wisoot
02:16
created

src/JwtService.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace WWON\JwtGuard;
4
5
use Firebase\JWT\JWT;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use Illuminate\Support\Facades\Config;
8
use WWON\JwtGuard\Contract\TokenManager;
9
10
class JwtService
11
{
12
13
    /**
14
     * @var string
15
     */
16
    private $key;
17
18
    /**
19
     * @var TokenManager
20
     */
21
    protected $tokenManager;
22
23
    /**
24
     * JwtService constructor
25
     *
26
     * @param TokenManager $tokenManager
27
     */
28
    public function __construct(TokenManager $tokenManager)
29
    {
30
        $this->key = Config::get('jwt.secret');
31
        $this->tokenManager = $tokenManager;
32
    }
33
34
    /**
35
     * getTokenForUser method
36
     *
37
     * @param Authenticatable $user
38
     * @return null|string
39
     */
40
    public function getTokenForUser(Authenticatable $user)
41
    {
42
        $claim = new Claim([
43
            'sub' => $user->getAuthIdentifier()
44
        ]);
45
46
        return $this->getTokenForClaim($claim);
47
    }
48
49
    /**
50
     * getUserIdFromToken method
51
     *
52
     * @param $token
53
     * @return mixed|null
54
     */
55
    public function getUserIdFromToken($token)
56
    {
57
        $claim = $this->getClaimFromToken($token);
58
59
        if (!empty($claim) && $this->tokenManager->check($claim)) {
60
            return $claim->sub;
61
        }
62
63
        return null;
64
    }
65
66
    /**
67
     * refreshToken method
68
     *
69
     * @param $token
70
     * @return null|string
71
     */
72
    public function refreshToken($token)
73
    {
74
        $claim = $this->getClaimFromToken($token);
75
76
        if (empty($claim)) {
77
            return null;
78
        }
79
80
        $this->tokenManager->remove($claim);
81
82
        $newClaim = new Claim([
83
            'sub' => $claim->sub
84
        ]);
85
86
        return $this->getTokenForClaim($newClaim);
87
    }
88
89
    /**
90
     * getTokenForUser method
91
     *
92
     * @param Claim $claim
93
     * @return null|string
94
     */
95
    protected function getTokenForClaim(Claim $claim)
96
    {
97
        $token = JWT::encode($claim->toArray(), $this->key);
98
        $this->tokenManager->add($claim);
99
100
        return $token;
101
    }
102
103
    /**
104
     * getClaimFromToken method
105
     *
106
     * @param $token
107
     * @return null|Claim
108
     */
109
    protected function getClaimFromToken($token)
110
    {
111
        try {
112
            $payload = JWT::decode($token, $this->key);
113
114
            return new Claim((array) $payload);
115
116
        } catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
117
118
        return null;
119
    }
120
121
}