-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuthSystem.php
More file actions
126 lines (107 loc) · 2.59 KB
/
Copy pathAuthSystem.php
File metadata and controls
126 lines (107 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Represents an auth system that is external to DeskPRO
*/
class AuthSystem
{
private $users;
public function __construct()
{
session_start();
$_SESSION['authenticated'] = isset($_SESSION['authenticated']) ? $_SESSION['authenticated'] : false;
$_SESSION['user'] = isset($_SESSION['user']) ? $_SESSION['user'] : null;
$this->users = array(
array(
'id' => '1',
'user' => 'cp',
'pass' => 'user1',
'name' => 'CP',
'email' => 'cp@cp.com'
),
array(
'id' => '2',
'user' => 'cn',
'pass' => 'user2',
'name' => 'CN',
'email' => 'cn@cn.com'
),
array(
'id' => '3',
'user' => 'ct',
'pass' => 'user3',
'name' => 'CT',
'email' => 'ct@ct.com'
),
array(
'id' => '4',
'user' => 'maxim',
'pass' => 'user4',
'name' => 'MAXIM',
'email' => 'max@im.com'
),
array(
'id' => '5',
'user' => 'user',
'pass' => 'user',
'name' => 'Some User',
'email' => 'user@user.com'
),
array(
'id' => '6',
'user' => 'admin',
'pass' => 'admin',
'name' => 'Admin User',
'email' => 'admin@admin.com'
)
);
}
public function login($username, $password)
{
foreach ($this->users as $user_data) {
if ($user_data['user'] == $username && $user_data['pass'] == $password) {
$this->authenticate($user_data);
return;
}
}
$this->logout();
}
protected function authenticate(array $user_data)
{
$_SESSION['authenticated'] = true;
$_SESSION['user'] = $user_data;
}
public function logout()
{
$_SESSION['authenticated'] = false;
$_SESSION['user'] = null;
}
public function isAuthenticated()
{
return $_SESSION['authenticated'];
}
public function getUser()
{
return $_SESSION['user'];
}
public function getToken($CONFIG)
{
$user = $this->getUser();
/***********************************************************************
* Generate a valid JWT token with a secret that we share with DeskPRO
*/
$now = time();
$exp = $now + (60) * 5; // 5 minutes
$token = array(
"jti" => md5($now . rand()),
"iat" => $now, // iat is recommended
"exp" => $exp, // exp is recommended
"id" => $user['id'],
"email" => $user['email'],
"name" => $user['name'],
// these are also accepted:
//"first_name" => $user['first_name'],
//"last_name" => $user['last_name'],
);
return \JWT::encode($token, $CONFIG['secret']);
}
}