This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomObj.php
90 lines (75 loc) · 2.58 KB
/
RandomObj.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
<?php
class Response
{
private $success;
private $message;
private function __construct($success, $message) {
$this->success = $success;
$this->message = $message;
}
public static function asSuccess($message = "") {
return new Response(true, $message);
}
public static function asFailure($message = "") {
return new Response(false, $message);
}
public function isSuccess() {
return $this->success;
}
public function getMessage() {
return $this->message;
}
}
class RandomObj
{
/*
* This function is used for capturing username and password, and saving it to a data store
* For later security validation
*/
public function register($username, $password): Response {
if (!isset($username) || $username === '') {
return Response::asFailure("Username is empty");
}
if (!isset($password) || $password === '') {
return Response::asFailure("Password is empty");
}
if (!$this->isValidUsername($username)) {
return Response::asFailure("Username is invalid");
}
$passwordValidation = $this->isPasswordValid($password);
if (!$passwordValidation->isSuccess()) {
return Response::asFailure($passwordValidation->getMessage());
}
//SAVE USER
$user = $this->saveUser($username, $password);
//RETURN USER DATA
return Response::asSuccess($user);
}
private function isValidUsername($username) {
//Email validation - Chat about solution or implement on
}
private function isPasswordValid($password) {
// Password validation
return Response::asSuccess();
}
private function saveUser($username, $password) {
$user = json_encode(array('username' => $username, 'password' => $this->getPasswordNonReadable($password)));
file_put_contents("users.txt", $user);
return $user;
}
private function getPasswordNonReadable($data) {
return $data; //Implement / add password obfuscation
}
public function login($username, $password) {
$users = explode('\n', file_get_contents("users.txt"));
foreach($users as $user) {
if ($user != "") {
$JSONData = json_decode($user, true);
if ($username == $JSONData['username'] && $password == $JSONData['password']) {
return Response::asSuccess("User authenticated");
}
}
}
return Response::asFailure("User not authenticated");
}
}