Completed
Pull Request — develop (#350)
by
unknown
64:24 queued 01:51
created

Auth::onceBasic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php namespace Tymon\JWTAuth;
2
3
use Illuminate\Contracts\Auth\Authenticatable;
4
use Illuminate\Contracts\Auth\Guard;
5
use Illuminate\Http\Request;
6
use Koodzo\Models\Db\User;
7
use Tymon\JWTAuth\JWTAuth;
8
9
class Auth implements Guard
10
{
11
    protected $auth;
12
13
    public function __construct(JWTAuth $auth)
14
    {
15
        $this->auth = $auth;
16
    }
17
18
    /**
19
     * Determine if the current user is authenticated.
20
     *
21
     * @return bool
22
     */
23
    public function check()
24
    {
25
        try
26
        {
27
            return ($token = $this->auth->parseToken()) && ($user = $this->auth->authenticate($token->getToken()));
0 ignored issues
show
Unused Code introduced by
The call to JWTAuth::authenticate() has too many arguments starting with $token->getToken().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
28
        }
29
        catch (\Exception $e)
30
        {
31
            return false;
32
        }
33
    }
34
35
    /**
36
     * Determine if the current user is a guest.
37
     *
38
     * @return bool
39
     */
40
    public function guest()
41
    {
42
        return !$this->check();
43
    }
44
45
    /**
46
     * Get the currently authenticated user's ID.
47
     *
48
     * @return int|null
49
     */
50
    public function id()
51
    {
52
        return $this->auth->parseToken()->getPayload()->get('sub');
53
    }
54
55
    /**
56
     * Get the currently authenticated user.
57
     *
58
     * @param array $attr Attributes to retrieve.
59
     *
60
     * @return \Koodzo\Models\Db\User|null
61
     */
62
    public function user($attr = ['*'])
63
    {
64
        $id = $this->auth->parseToken()->getPayload()->get('sub');
65
66
        return User::whereId($id)->first($attr);
67
    }
68
69
    /**
70
     * Log a user into the application without sessions or cookies.
71
     *
72
     * @param  array $credentials
73
     * @return bool
74
     */
75
    public function once(array $credentials = [])
76
    {
77
        // Skip
78
    }
79
80
    /**
81
     * Attempt to authenticate a user using the given credentials.
82
     *
83
     * @param  array $credentials
84
     * @param  bool $remember
85
     * @param  bool $login
86
     * @return false|string
87
     */
88
    public function attempt(array $credentials = [], $remember = false, $login = true)
89
    {
90
        return $this->auth->attempt($credentials);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->auth->attempt($credentials); of type false|string adds the type string to the return on line 90 which is incompatible with the return type declared by the interface Illuminate\Contracts\Auth\Guard::attempt of type boolean.
Loading history...
91
    }
92
93
    /**
94
     * Attempt to authenticate using HTTP Basic Auth.
95
     *
96
     * @param  string $field
97
     * @return \Symfony\Component\HttpFoundation\Response|null
98
     */
99
    public function basic($field = 'email')
100
    {
101
        // Skip
102
    }
103
104
    /**
105
     * Perform a stateless HTTP Basic login attempt.
106
     *
107
     * @param  string $field
108
     * @return \Symfony\Component\HttpFoundation\Response|null
109
     */
110
    public function onceBasic($field = 'email')
111
    {
112
        // Skip
113
    }
114
115
    /**
116
     * Validate a user's credentials.
117
     *
118
     * @param  array $credentials
119
     * @return bool
120
     */
121
    public function validate(array $credentials = [])
122
    {
123
        return !!$this->auth->attempt($credentials);
124
    }
125
126
    /**
127
     * Log a user into the application.
128
     *
129
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
130
     * @param  bool $remember
131
     * @return string
132
     */
133
    public function login(Authenticatable $user, $remember = false)
134
    {
135
        return $this->auth->fromUser($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Illuminate\Contracts\Auth\Authenticatable>, but the function expects a object<Tymon\JWTAuth\Contracts\JWTSubject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136
    }
137
138
    /**
139
     * Log the given user ID into the application.
140
     *
141
     * @param  mixed $id
142
     * @param  bool $remember
143
     * @return \Illuminate\Contracts\Auth\Authenticatable
144
     */
145
    public function loginUsingId($id, $remember = false)
146
    {
147
        $user = User::whereId($id)->first();
148
149
        return $this->auth->fromUser($user);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->auth->fromUser($user); (string) is incompatible with the return type declared by the interface Illuminate\Contracts\Auth\Guard::loginUsingId of type Illuminate\Contracts\Auth\Authenticatable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
150
    }
151
152
    /**
153
     * Determine if the user was authenticated via "remember me" cookie.
154
     *
155
     * @return bool
156
     */
157
    public function viaRemember()
158
    {
159
        // Skip
160
    }
161
162
    /**
163
     * Log the user out of the application.
164
     *
165
     * @return void
166
     */
167
    public function logout()
168
    {
169
        $this->auth->invalidate($this->auth->getToken());
0 ignored issues
show
Bug introduced by
It seems like $this->auth->getToken() targeting Tymon\JWTAuth\JWTAuth::getToken() can also be of type object<Tymon\JWTAuth\Token>; however, Tymon\JWTAuth\JWTAuth::invalidate() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
170
    }
171
}
172