-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.php
executable file
·185 lines (142 loc) · 3.65 KB
/
Auth.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Chestnut\Auth;
use Chestnut\Contract\Support\Container as ContainerContract;
use Firebase\JWT\JWT;
/**
* @author Liyang Zhang <zhangliyang@zhangliyang.name>
*/
class Auth {
protected $app;
protected $isLogin = false;
protected $user;
protected $permissions;
protected $model;
public function __construct(ContainerContract $app) {
$this->app = $app;
$this->model = $app->config->get('auth.model', 'Model\Auth');
}
public function login($account, $password, $remember) {
if ($user = $this->getModel()->where('user_name', $account)
->orWhere('email', $account)
->orWhere('phone', $account)
->one()) {
if (password_verify($password, $user->password)) {
$this->boot($user);
return AuthStatic::ACCOUNT_LOGIN_ACCESS;
} else {
return AuthStatic::WRONG_PASSWORD;
}
} else {
return AuthStatic::ACCOUNT_NOT_FOUND;
}
}
public function logout() {
cookie_remove('jwt');
return true;
}
public function boot($user) {
if (!is_object($user)) {
$user = $this->getModel()->one($user);
}
if (!$user) {
return false;
}
$this->user = $user;
$this->setPermissions($user->getPermissions());
return $this->setLogin(true);
}
public function check($token = null) {
if (is_null($token)) {
return false;
}
if ($token = $this->decodeJsonWebToken($token)) {
if (time() > $token->expire) {
cookie_remove('jwt');
return false;
}
if ($token->aud !== $this->app->config->get('app.domain')) {
return false;
}
return $this->boot($token->iss);
} else {
return false;
}
}
public function create($user) {
if ($this->getModel()->where('user_name', $user['phone'])
->orWhere('email', $user['phone'])
->orWhere('phone', $user['phone'])
->count()) {
return AuthStatic::ACCOUNT_HAS_BEEN_REGISTERED;
}
unset($user['repassword']);
$user['password'] = $this->convertPassword($user['password']);
$id = $this->getModel()->create($user);
$this->boot($id);
return AuthStatic::ACCOUNT_REGISTER_SUSSECC;
}
public function getModel() {
return (new $this->model);
}
public function getUser() {
return $this->user;
}
public function setPermissions($permissions) {
$this->permissions = array_filter($permissions);
}
public function setWithRelations($with) {
$this->with = $with;
}
public function hasPermission($permission) {
if (in_array('all', $this->permissions)) {
return true;
}
if (!in_array(explode('.', $permission)[0], ['admin', 'upload', 'all', 'show'])) {
return true;
}
return in_array($permission, $this->permissions);
}
public function getFirstPermission() {
if (reset($this->permissions) == 'all') {
return 'admin.dashboard';
}
return reset($this->permissions);
}
public function getAccount() {
if (!isset($this->user)) {
return 'onekeymall';
}
return $this->user->phone;
}
public function getId() {
if (!isset($this->user)) {
return 0;
}
return $this->user->id;
}
public function setLogin($isLogin) {
if ($this->isLogin) {
return true;
}
return $this->isLogin = $isLogin;
}
public function generateJsonWebToken() {
$key = $this->app->config->get('app.app_key');
$token = [
'iss' => $this->getId(),
'aud' => config('app.domain'),
'expire' => time() + 604800,
];
return JWT::encode($token, $key);
}
public function __call($method, $params) {
return call_user_func_array([$this->user, $method], $params);
}
private function convertPassword($password) {
return password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
}
private function decodeJsonWebToken($token) {
$key = $this->app->config->get('app.app_key');
return JWT::decode($token, $key, ['HS256']);
}
}