-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthenticationManager.php
136 lines (106 loc) · 3.27 KB
/
AuthenticationManager.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
<?php
include_once("Model/AES256.php");
if (isset($_POST['login'])) {
session_start();
echo login($_POST['login']);
}
if (isset($_POST['logoff'])) {
session_start();
echo logoff($_POST['logoff']);
}
if (isset($_POST['register'])) {
session_start();
echo register($_POST['register']);
}
function login($login){
$aes256 = '';
if($login["email"] =="" || $login["email"] =="*REQUIRED" || $login["password"] =="" ||
$login["password"] =="*REQUIRED"){
return json_encode("nulls");
}
$login = htmlEntitiesCheck($login); //ASSIGNMENT 3 SECURITY CHECK
require_once("global-connect.inc.php");
$query ="SELECT * FROM UserAccount WHERE Email = '". $login['email'] ."'"; //HTML PASSED FOR XXS ATTACK
$stmt = oci_parse($db, $query);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
return false;
}
oci_execute($stmt);
while(oci_fetch_array($stmt)) {
$aes256 = new AES256;
$aes256->setPassword(oci_result($stmt,"PASSWORD"));
$aes256-> setCryptoKey(oci_result($stmt,"CRYPTOKEY"));
}
// Close the connection
oci_close($db);
if($aes256 != ""){
if($aes256 -> decrypt($aes256) == $login["password"]){
setcookie("user", $login['email'], time()+7200);
return json_encode("success");
}
else{
return json_encode("passFAIL");
}
}
else{
return json_encode("no account exist");
}
}
function logoff(){
setcookie("user", "", time() - 7200);
return json_encode("success");
}
function register($register){
if($register["email"] =="" || $register["email"] =="*REQUIRED" || $register["password"] =="" ||
$register["password"] =="*REQUIRED"){
return json_encode("nulls");
}
$count = 0;
require_once("global-connect.inc.php");
$query ="SELECT * FROM UserAccount WHERE Email = '".$register['email']."'";
$stmt = oci_parse($db, $query);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
oci_execute($stmt);
while(oci_fetch_array($stmt)) {
$count++;
}
if($count == 0){
$aes256 = new AES256;
$aes256 = $aes256-> encrypt($register['password']);
$query ="INSERT INTO UserAccount VALUES ('". $register['email'] ."', '" . $aes256-> getPassword() . "', '" . $aes256->getCryptoKey() ."' , '". $aes256-> getSalt() . "')";
$stmt = oci_parse($db, $query);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
oci_execute($stmt);
oci_close($db);
return json_encode("success");
}
else{
return json_encode("exist");
}
}
function htmlEntitiesCheck($input){
$input['email'] = htmlspecialchars_decode($input['email'],ENT_QUOTES);
$input['email'] = htmlentities($input['email']);
$input['password'] = htmlspecialchars_decode($input['password'], ENT_QUOTES);
$input['password'] = htmlentities($input['password']);
return $input;
}
function validateLogin($login){
if(!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $form["email"]) || $form["email"] == ""){
$login["email"] = false; //https://emailregex.com
return false;
}
if(!preg_match('/^[.*] {1, 256}$/', $login["password"]) || $login["password"] == ""){
$login["password"] = false;
}
return true;
}
?>