2022-03-19 00:00:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace brain\data;
|
|
|
|
|
|
|
|
use ReallySimpleJWT\Token;
|
|
|
|
use function _\find;
|
|
|
|
|
|
|
|
class Auth
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
public static function sessionStatus()
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
if (isset($_SESSION['member'])) {
|
2022-03-19 00:00:51 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//return $this->secret;
|
|
|
|
}
|
|
|
|
public static function status()
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
$result = '';
|
2022-03-19 00:00:51 +01:00
|
|
|
if (Session::active()) {
|
|
|
|
$result = true;
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
public static function login($who)
|
|
|
|
{
|
|
|
|
//grab member list
|
|
|
|
$folks = (new Settings())->getFolks();
|
2022-05-17 02:41:15 +02:00
|
|
|
$found = find($folks, ['handle' => $who['handle']]);
|
2022-03-19 00:00:51 +01:00
|
|
|
|
|
|
|
if ($found) {
|
|
|
|
//name is found, verify password
|
2022-05-17 02:41:15 +02:00
|
|
|
if (password_verify($who['password'], $found['password'])) {
|
2022-03-19 00:00:51 +01:00
|
|
|
$member = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'handle' => $found['handle'],
|
|
|
|
'email' => $found['email'],
|
|
|
|
'role' => $found['role'],
|
|
|
|
'avatar' => $found['avi'],
|
|
|
|
'key' => $found['key'],
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$token = Token::create(
|
2022-05-17 02:41:15 +02:00
|
|
|
$found['key'],
|
|
|
|
$found['secret'],
|
2022-03-19 00:00:51 +01:00
|
|
|
time() + 3600,
|
2022-05-17 02:41:15 +02:00
|
|
|
'localhost'
|
2022-03-19 00:00:51 +01:00
|
|
|
); //expires in an hour
|
|
|
|
|
|
|
|
$form_token = md5(uniqid(microtime(), true));
|
|
|
|
Session::start();
|
2022-05-17 02:41:15 +02:00
|
|
|
Session::set('member', $member);
|
|
|
|
Session::set('token', $token);
|
|
|
|
Session::set('form_token', $form_token);
|
2022-03-19 00:00:51 +01:00
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
$result = 'good_login';
|
2022-03-19 00:00:51 +01:00
|
|
|
} else {
|
2022-05-17 02:41:15 +02:00
|
|
|
$result = 'bad_pass';
|
2022-03-19 00:00:51 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//if name is not found
|
2022-05-17 02:41:15 +02:00
|
|
|
$result = 'no_name';
|
2022-03-19 00:00:51 +01:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
public static function findSecret($data)
|
|
|
|
{
|
|
|
|
$result = [];
|
2022-05-17 02:41:15 +02:00
|
|
|
$folks = (new Settings())->getFolks();
|
2022-03-19 00:00:51 +01:00
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
if (!empty($data['email']) &&
|
|
|
|
filter_var($data['email'], FILTER_VALIDATE_EMAIL)
|
2022-03-19 00:00:51 +01:00
|
|
|
) {
|
2022-05-17 02:41:15 +02:00
|
|
|
$found = find($folks, ['email' => $data['email']]);
|
2022-03-19 00:00:51 +01:00
|
|
|
if ($found) {
|
|
|
|
//if email is cool, check mail relay status
|
|
|
|
//if set up, send secret there, if not just return it
|
2022-05-17 02:41:15 +02:00
|
|
|
$config = new Settings();
|
2022-03-19 00:00:51 +01:00
|
|
|
$settings = $config->getSettings();
|
2022-05-17 02:41:15 +02:00
|
|
|
$email = $settings['email']['active'];
|
|
|
|
if ($email != 'option-none') {
|
|
|
|
$data['mail_task'] = 'SEND_SECRET';
|
|
|
|
$data['secret'] = $found['secret'];
|
|
|
|
$result = Mailer::sendmail($data);
|
2022-03-19 00:00:51 +01:00
|
|
|
} else {
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => 'Valid email, but no email set up!',
|
|
|
|
'type' => 'secretFound',
|
|
|
|
'secret' => $found['secret'],
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => 'No valid email, no goodies, pleighboi',
|
|
|
|
'type' => 'secretNotFound',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => 'Aye, this address is not right, slick.',
|
|
|
|
'type' => 'secretNotFound',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
public static function makeNewPassword($data)
|
|
|
|
{
|
|
|
|
//check if passwordsmatch
|
2022-05-17 02:41:15 +02:00
|
|
|
if ($data['newPass'] == $data['newPassConfirm']) {
|
2022-03-19 00:00:51 +01:00
|
|
|
//verify secret
|
|
|
|
$folks = (new Settings())->getFolks();
|
2022-05-17 02:41:15 +02:00
|
|
|
$found = find($folks, ['secret' => $data['secret']]);
|
2022-03-19 00:00:51 +01:00
|
|
|
if ($found) {
|
|
|
|
//create new pass and secret key, then update file
|
2022-05-17 02:41:15 +02:00
|
|
|
$hash = password_hash($data['newPass'], PASSWORD_DEFAULT);
|
2022-03-19 00:00:51 +01:00
|
|
|
$freshSecret = StringTools::randomString(12);
|
2022-05-17 02:41:15 +02:00
|
|
|
Member::updateData('password', $hash, $data['secret']);
|
|
|
|
Member::updateData('secret', $freshSecret, $data['secret']);
|
2022-03-19 00:00:51 +01:00
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => 'Password Updated. Very nice!',
|
|
|
|
'type' => 'passCreated',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => 'Secret key is invalid. Try to retrieve it again',
|
|
|
|
'type' => 'passNotCreated',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => "Passwords don't match. Try it again.",
|
|
|
|
'type' => 'passNotCreated',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
public static function logout()
|
|
|
|
{
|
|
|
|
Session::kill();
|
|
|
|
}
|
|
|
|
}
|