-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauth.php
More file actions
129 lines (111 loc) · 2.96 KB
/
auth.php
File metadata and controls
129 lines (111 loc) · 2.96 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
127
128
129
<?php
/** This file is part of load.link (https://github.com/deuiore/load.link).
* View the LICENSE file for full license information.
**/
class Auth
{
const LOGIN_PATH = 'login';
const LOGOUT_PATH = 'logout';
const COOKIE_EXPIRATION = 31536000;
protected $isAuthorized;
protected $token;
public function __construct()
{
$this->isAuthorized = FALSE;
}
public function isAuthorized()
{
return $this->isAuthorized;
}
public static function checkPassword($password)
{
$login = Config::get()->getSection('login');
if (hash_hmac('sha512',
$password, $login['salt']) == $login['password'])
{
return TRUE;
}
return FALSE;
}
public function authorizeFromToken($token)
{
if (DB::get()->getSession($token))
{
$this->isAuthorized = TRUE;
$this->token = $token;
return TRUE;
}
return FALSE;
}
public function authorizeFromCookies()
{
if (isset($_COOKIE['token']))
{
if (DB::get()->getSession($_COOKIE['token']))
{
$this->token = $_COOKIE['token'];
$this->isAuthorized = TRUE;
return TRUE;
}
}
return FALSE;
}
public function authorizeFromBearerAuthHeader()
{
if (isset($_SERVER['HTTP_AUTHORIZATION']))
{
if (DB::get()->getSession(substr($_SERVER["HTTP_AUTHORIZATION"], 7)))
{
$this->token = $_SERVER['HTTP_AUTHORIZATION'];
$this->isAuthorized = TRUE;
return TRUE;
}
}
return FALSE;
}
public function authorizeFromLogin($username, $password)
{
if ($username == Config::get()->getValue('login', 'username')
&& self::checkPassword($password))
{
$this->token = DB::get()->addSession();
$this->isAuthorized = TRUE;
return TRUE;
}
return FALSE;
}
public function unauthorize()
{
DB::get()->delSession($this->token);
unset($this->token);
}
public function unauthorizeAll()
{
DB::get()->delAllSessions();
unset($this->token);
}
public function setCookie()
{
setcookie('token', $this->token, time() + self::COOKIE_EXPIRATION,
Config::get()->getValue('routing', 'baseurl'));
}
public function unsetCookie()
{
unset($_COOKIE['token']);
setcookie('token', '', time() - 42000,
Config::get()->getValue('routing', 'baseurl'));
}
public function getToken()
{
return $this->token;
}
static function passwordToHashAndSalt($password)
{
$salt = uniqid();
$hash = hash_hmac('sha512', $password, $salt);
return array(
'hash' => $hash,
'salt' => $salt
);
}
}