Completed
Push — develop ( 2f6a53...9bf2e6 )
by Sean
04:08 queued 10s
created

JWTAuth::getClaimsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of jwt-auth
5
 *
6
 * (c) Sean Tymon <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tymon\JWTAuth;
13
14
use Illuminate\Http\Request;
15
use Tymon\JWTAuth\Support\CustomClaims;
16
use Tymon\JWTAuth\Contracts\Providers\Auth;
17
use Tymon\JWTAuth\Exceptions\JWTException;
18
use Tymon\JWTAuth\Http\Parser;
19
20
class JWTAuth extends JWT
21
{
22
    /**
23
     * @var \Tymon\JWTAuth\Manager
24
     */
25
    protected $manager;
26
27
    /**
28
     * @var \Tymon\JWTAuth\Contracts\Providers\Auth
29
     */
30
    protected $auth;
31
32
    /**
33
     * @var \Tymon\JWTAuth\Http\Parser
34
     */
35
    protected $parser;
36
37
    /**
38
     * @var \Tymon\JWTAuth\Token
39
     */
40
    protected $token;
41
42
    /**
43
     * @param \Tymon\JWTAuth\Manager                   $manager
44
     * @param \Tymon\JWTAuth\Contracts\Providers\Auth  $auth
45
     * @param \Tymon\JWTAuth\Http\Parser               $parser
46
     */
47 38
    public function __construct(Manager $manager, Auth $auth, Parser $parser)
48
    {
49 38
        parent::__construct($manager, $parser);
50 38
        $this->auth = $auth;
51 38
    }
52
53
    /**
54
     * Attempt to authenticate the user and return the token.
55
     *
56
     * @param array $credentials
57
     *
58
     * @return false|string
59
     */
60 4
    public function attempt(array $credentials)
61
    {
62 4
        if (! $this->auth->byCredentials($credentials)) {
63 2
            return false;
64
        }
65
66 2
        return $this->fromUser($this->user());
67
    }
68
69
    /**
70
     * Authenticate a user via a token.
71
     *
72
     * @return \Tymon\JWTAuth\Contracts\JWTSubject|false
73
     */
74 6
    public function authenticate()
75
    {
76 6
        $id = $this->getPayload()->get('sub');
77
78 4
        if (! $this->auth->byId($id)) {
79 2
            return false;
80
        }
81
82 2
        return $this->user();
83
    }
84
85
    /**
86
     * Alias for authenticate().
87
     *
88
     * @return \Tymon\JWTAuth\Contracts\JWTSubject|false
89
     */
90 2
    public function toUser()
91
    {
92 2
        return $this->authenticate();
93
    }
94
}
95