Issues (20)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

app/Models/SocialAuth.php (5 issues)

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 App\Models;
4
5
use App\Exceptions\InvalidInputException;
6
use Illuminate\Database\Eloquent\Model;
7
8
class SocialAuth extends Model
9
{
10
    const SOCIAL_TYPE_WEIBO = 1;
11
    const SOCIAL_TYPE_WEIXIN = 2;
12
    const SOCIAL_TYPE_QQ = 3;
13
14
    const SOCIAL_WEIBO = 'weibo';
15
    const SOCIAL_WEIXIN = 'weixin';
16
    const SOCIAL_QQ = 'qq';
17
18
    /**
19
     * The accessors to append to the model's array form.
20
     *
21
     * @var array
22
     */
23
    protected $appends = ['social'];
24
25
    /**
26
     * The attributes that should be visible in arrays.
27
     *
28
     * @var array
29
     */
30
    protected $visible = [
31
        'social', 'access_token', 'refresh_token', 'uid', 'expires_at',
32
    ];
33
34
    /**
35
     * The attributes that should be mutated to dates.
36
     *
37
     * @var array
38
     */
39
    protected $dates = ['expires_at'];
40
41
    /**
42
     * Get the `social` attribute.
43
     *
44
     * @return string
45
     */
46
    public function getSocialAttribute()
47
    {
48
        return static::socialFromSocialType($this->social_type);
49
    }
50
51
    /**
52
     * Scope a query with social type or name.
53
     *
54
     * @param  string|int  $social
55
     * @return \Illuminate\Database\Eloquent\Builder
56
     */
57
    public function scopeSocial($query, $social)
58
    {
59
        return $query->where('social_type', static::toSocialType($social));
60
    }
61
62
    /**
63
     * Get all socials.
64
     *
65
     * @return array
66
     */
67
    public static function allSocials()
68
    {
69
        return [
70
            static::SOCIAL_WEIBO,
71
            static::SOCIAL_WEIXIN,
72
            static::SOCIAL_QQ,
73
        ];
74
    }
75
76
    /**
77
     * Convert social type to social name.
78
     *
79
     * @param  int  $type
80
     * @return string|false
81
     */
82 View Code Duplication
    public static function socialFromSocialType($type)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        switch ($type) {
85
            case static::SOCIAL_TYPE_WEIBO:
86
                return static::SOCIAL_WEIBO;
87
            case static::SOCIAL_TYPE_WEIXIN:
88
                return static::SOCIAL_WEIXIN;
89
            case static::SOCIAL_TYPE_QQ:
90
                return static::SOCIAL_QQ;
91
            default:
92
                return false;
93
        }
94
    }
95
96
    /**
97
     * Convert social name to social type.
98
     *
99
     * @param  string  $social
100
     * @return int|false
101
     */
102 View Code Duplication
    public static function socialTypeFromSocial($social)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        switch ($social) {
105
            case static::SOCIAL_WEIBO:
106
                return static::SOCIAL_TYPE_WEIBO;
107
            case static::SOCIAL_WEIXIN:
108
                return static::SOCIAL_TYPE_WEIXIN;
109
            case static::SOCIAL_QQ:
110
                return static::SOCIAL_TYPE_QQ;
111
            default:
112
                return false;
113
        }
114
    }
115
116
    /**
117
     * Convert string or int to social type.
118
     *
119
     * @param  mixed  $value
120
     * @return int|false
121
     */
122
    public static function toSocialType($value)
123
    {
124
        return is_string($value) ? static::socialTypeFromSocial($value) : (int) $value;
125
    }
126
127
    /**
128
     * Find records via userId and social.
129
     *
130
     * @param  mixed  $userId
131
     * @param  string|int  $social
132
     * @return \Illuminate\Database\Eloquent\Collection|static
133
     */
134
    public static function findByUser($userId, $social = null)
135
    {
136 View Code Duplication
        if (is_object($userId)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
            $userId = $userId->id;
138
        } elseif (is_array($userId)) {
139
            $userId = $userId['id'];
140
        }
141
142
        $query = static::where('user_id', $userId);
143
144
        if (! is_null($social)) {
145
            return $query->social($social)->first();
146
        }
147
148
        return $query->get()->keyBy('social');
149
    }
150
151
    /**
152
     * Find record via credentials and social user.
153
     *
154
     * @param  string|int  $social
155
     * @param  array  $credentials
156
     * @return static|null
157
     *
158
     * @throws \App\Exceptions\InvalidInputException
159
     */
160
    public static function findByCredentials($social, $credentials)
161
    {
162
        if (is_array($credentials) &&
163
            false !== ($social_type = static::toSocialType($social))
164
        ) {
165
            extract($credentials);
166
167
            $query = static::social($social);
168
169
            if (static::SOCIAL_TYPE_WEIBO === $social_type) {
170 View Code Duplication
                if (! empty($access_token) && ! empty($uid)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
                    return $query->where(function ($query) use ($access_token, $uid) {
172
                        $query->where('access_token', $access_token)
173
                            ->orWhere('uid', $uid);
174
                    })->first();
175
                }
176
            } elseif (static::SOCIAL_TYPE_WEIXIN === $social_type) {
177
                if (! empty($access_token) && ! empty($openid) && ! empty($unionid)) {
178
                    return $query->where(function ($query) use ($access_token, $openid, $unionid) {
179
                        $query->where('access_token', $access_token)
180
                           ->orWhere('uid', $openid)
181
                           ->orWhere('vendor', $unionid);
182
                    })->first();
183
                }
184 View Code Duplication
            } elseif (static::SOCIAL_TYPE_QQ === $social_type) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
                if (! empty($access_token) && ! empty($openid)) {
186
                    return $query->where(function ($query) use ($access_token, $openid) {
187
                        $query->where('access_token', $access_token)
188
                            ->orWhere('uid', $openid);
189
                    })->first();
190
                }
191
            }
192
        }
193
194
        throw new InvalidInputException('授权数据错误!');
195
    }
196
197
    /**
198
     * Create a SocialAuth record.
199
     *
200
     * @param  string|int  $social
201
     * @param  int $userId
202
     * @param  array $credentials
203
     * @return static
204
     */
205
    public static function createByCredentials($social, $userId, $credentials, $save = false)
206
    {
207
        $auth = new static;
208
        $auth->social_type = static::toSocialType($social);
209
        $auth->user_id = $userId;
210
211
        $auth->updateByCredentials($credentials, false);
212
213
        if ($save) {
214
            $auth->save();
215
        }
216
217
        return $auth;
218
    }
219
220
    /**
221
     * Update credentials info.
222
     *
223
     * @param  array  $credentials
224
     * @param  bool  $save
225
     * @return $this
226
     */
227
    public function updateByCredentials($credentials, $save = false)
228
    {
229
        $this->access_token = $credentials['access_token'];
230
        $this->refresh_token = array_get($credentials, 'refresh_token');
231
232
        if ($this->social_type == static::SOCIAL_TYPE_WEIBO) {
233
            $this->uid = str_limit2($credentials['uid'], 40);
234
        } elseif ($this->social_type == static::SOCIAL_TYPE_WEIXIN) {
235
            $this->uid = str_limit2($credentials['openid'], 40);
236
            $this->vendor = str_limit2($credentials['unionid'], 200);
237
        } elseif ($this->social_type == static::SOCIAL_TYPE_QQ) {
238
            $this->uid = str_limit2($credentials['openid'], 40);
239
        }
240
241
        if ($expires_in = array_get($credentials, 'expires_in')) {
242
            $this->expires_at = $this->freshTimestamp()->addSeconds($expires_in);
243
        } else {
244
            $this->expires_at = null;
245
        }
246
247
        if ($save) {
248
            $this->save();
249
        }
250
251
        return $this;
252
    }
253
254
    /**
255
     * Get user avatar URL from social user array.
256
     *
257
     * @param  string|int  $social
258
     * @param  array  $user
259
     * @return string|null
260
     */
261
    public static function getAvatarFromSocialUser($social, $user)
262
    {
263
        $type = static::toSocialType($social);
264
265
        if ($type == static::SOCIAL_TYPE_WEIBO) {
266
            return array_get($user, 'avatar_hd');
267
        } elseif ($type == static::SOCIAL_TYPE_WEIXIN) {
268
            return array_get($user, 'headimgurl');
269
        } elseif ($type == static::SOCIAL_TYPE_QQ) {
270
            return array_get($user, 'figureurl_qq_2');
271
        }
272
    }
273
274
    /**
275
     * Get user nickname from social user array.
276
     *
277
     * @param  string|int  $social
278
     * @param  array $user
279
     * @return string|null
280
     */
281
    public static function getUsernameFromSocialUser($social, $user)
282
    {
283
        $type = static::toSocialType($social);
284
285
        if ($type == static::SOCIAL_TYPE_WEIBO) {
286
            $username = array_get($user, 'name');
287
        } elseif ($type == static::SOCIAL_TYPE_WEIXIN) {
288
            $username = array_get($user, 'nickname');
289
        } elseif ($type == static::SOCIAL_TYPE_QQ) {
290
            $username = array_get($user, 'nickname');
291
        }
292
293
        if (isset($username)) {
294
            $username = mb_trim($username);
295
296
            return ! empty($username) ? $username : null;
297
        }
298
    }
299
}
300