Auth::requestUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * This file is part of user_management
4
 * User: Sinan TURGUT <[email protected]>
5
 * Date: 24.06.2019
6
 * php version 7.2
7
 *
8
 * @category Assessment
9
 * @package  UserManagement
10
 * @author   Sinan TURGUT <[email protected]>
11
 * @license  See LICENSE file
12
 * @link     https://dev.sinanturgut.com.tr
13
 */
14
15
namespace UserManagement\Services\Auth;
16
17
use UserManagement\Models\User;
18
use DateTime;
19
use Firebase\JWT\JWT;
0 ignored issues
show
Bug introduced by
The type Firebase\JWT\JWT was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Illuminate\Database\Capsule\Manager;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Capsule\Manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Slim\Collection;
0 ignored issues
show
Bug introduced by
The type Slim\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Slim\Http\Request;
0 ignored issues
show
Bug introduced by
The type Slim\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
/**
25
 * Class Auth
26
 * @package UserManagement\Services\Auth
27
 */
28
class Auth
29
{
30
31
    /**
32
     *
33
     */
34
    const SUBJECT_IDENTIFIER = 'username';
35
36
    /**
37
     * @var Manager
38
     */
39
    private $db;
40
    /**
41
     * @var Collection
42
     */
43
    private $appConfig;
44
45
    /**
46
     * Auth constructor.
47
     * @param Manager $db
48
     * @param Collection $appConfig
49
     */
50
    public function __construct(Manager $db, Collection $appConfig)
51
    {
52
        $this->db = $db;
53
        $this->appConfig = $appConfig;
54
    }
55
56
    /**
57
     * @param User $user
58
     * @return string
59
     * @throws \Exception
60
     */
61
    public function generateToken(User $user)
62
    {
63
        $now = new DateTime();
64
        $future = new DateTime("now +".$this->appConfig['app']['session_expire']." minutes");
65
66
        $payload = [
67
            "iat" => $now->getTimeStamp(),
68
            "exp" => $future->getTimeStamp(),
69
            "jti" => base64_encode(random_bytes(16)),
70
            'iss' => $this->appConfig['app']['url'],
71
            "sub" => $user->{self::SUBJECT_IDENTIFIER},
72
        ];
73
74
        $secret = $this->appConfig['jwt']['secret'];
75
        $token = JWT::encode($payload, $secret, "HS256");
76
77
        return $token;
78
    }
79
80
    /**
81
     * @param $username
82
     * @param $password
83
     * @return bool
84
     */
85
    public function attemptLogin($username, $password)
86
    {
87
        if ( !$user = User::where('username', $username)->first()) {
88
            return false;
89
        }
90
91
        if (password_verify($password, $user->password)) {
92
            return $user;
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * @param Request $request
100
     * @return mixed
101
     */
102
    public function requestUser(Request $request)
103
    {
104
        if ($token = $request->getAttribute('token')) {
105
            return User::where(static::SUBJECT_IDENTIFIER, '=', $token->sub)->first();
106
        };
107
    }
108
109
}