-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthenticate.php
129 lines (112 loc) · 3.7 KB
/
authenticate.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
/*require_once("/utils/dbconnect.php");
class authenticate {
private $_db;
public function __construct($database=getdb()) {
$this->_db = $database;
}
private function randomString($length = 50) {
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
$string = "";
for ($i = 0; $i < $length; $i++) {
$string = $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
private function hashData($data) {
return hash_hmac('sha512', $data, $this->_siteKey);
}
public function isAdmin($userID) {
$admin_query = "SELECT admin FROM user WHERE user_id = $userID";
$admin = $db->query($admin_query);
if($admin == 1) {
return true;
} else {
return false;
}
}
public function createUser($email, $password, $is_admin = 0) {
$salt = $this->randomString();
$password = $salt . $password;
$password = $this->hashData($password);
$verification_code = $this->randomString();
// TO-DO: "password_digest" needs to be changed to "salt" in the user table.
$insert_query = "INSERT INTO user (email, password, password_digest, verification_code) values($email, $password, $salt, $verification_code)";
$user_created = $db->query($insert_query);
if($user_created) {
if($this->sendVerificationEmail($email, $verification_code) == 1) {
return true;
}
}
return false;
}
public function sendVerificationEmail($email, $verification_code) {
$subject = "NuTRUtion Account Verfication";
$message = $verification_code;
if(mail($email, $subject, $message) == 1) {
return true;
} else {
return false;
}
}
public function login($email, $password) {
$id_query = "SELECT user_id FROM user WHERE email = $email";
$id = $db->query($id_query);
$digest_query = "SELECT password_digest FROM user WHERE user_id = $id";
$digest = query($exists_query);
$attempt_password = $salt . $password;
$this->hashData($password);
$password_query = "SELECT password FROM user WHERE user_id = $id";
$saved_password = $db->query($password_query);
if($attempt_password == $saved_password) {
$random = $this->randomString();
$token = $_SERVER['HTTP_USER_AGENT'] . $random;
$this->hashData($token);
session_start();
$_SESSION['token'] = $token;
$_SESSION['user_id'] = $id;
$delete_old_sessions_query = "DELETE FROM logged_in_user WHERE user_id = $id";
$db->query($delete_old_sessions_query);
$ses_id = session_id();
$add_new_session_query = "INSERT INTO logged_in_user(user_id, session_id, token) values($id, $ses_id, $token)";
if($db->query($add_new_session_query) == true) {
return 0;
}
return 1;
} else {
return 1;
}
}
public function checkSession() {
$user_id = $_SESSION['user_id'];
$token_query = "SELECT token FROM user WHERE username = $user_id";
$session_id_query = "SELECT session_id FROM user WHERE username = $user_id";
$token = $db->query($token_query);
$session_id = $db->query($session_id_query);
if($token == $_SESSION['token'] && $session_id == session_id()) {
$this->refreshSession($user_id);
return true;
}
return false;
}
private function refreshSession($user_id) {
session_regenerate_id();
$random = $this->randomString();
$token = $_SERVER['HTTP_USER_AGENT'] . $random;
$token = $this->hashData($token);
$_SESSION['token'] = $token;
$session_id = session_id();
$update_session_query = "UPDATE logged_in_user SET token = $token, session_id = $session_id WHERE user_id = $user_id";
$refreshed = $db->query($update_session_query);
return ($refreshed);
}
public function logout() {
$user_id = $_SESSION['user_id'];
$delete_query = "DELETE FROM logged_in_user WHERE user_id = $user_id";
$db->query($delete_query);
session_unset();
session_destroy();
header('Location: index.php');
}
}
?>
*/