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

JwtService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, WWON\JwtGuard\TokenManager.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
}