PHP_SMART_HOME_V3/www/app/models/managers/AuthManager.php

46 lines
1.5 KiB
PHP
Raw Normal View History

2020-03-31 09:13:21 +00:00
<?php
2020-04-25 16:04:02 +00:00
class AuthManager {
2020-05-12 17:25:08 +00:00
public function getToken($username, $password){
$userManager = new UserManager();
if ($username != '' || $password != ''){
$userLogedIn = $userManager->loginNew($username, $password);
2020-04-25 16:04:02 +00:00
2020-05-12 17:25:08 +00:00
if ($userLogedIn != false){
// Create token header as a JSON string
$header = json_encode(['typ' => 'JWT', 'alg' => 'HS256']);
// Create token payload as a JSON string
$payload = json_encode(['user_id' => $userLogedIn]);
// Encode Header to Base64Url String
$base64UrlHeader = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($header));
// Encode Payload to Base64Url String
$base64UrlPayload = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($payload));
// Create Signature Hash
$signature = hash_hmac('sha256', $base64UrlHeader . "." . $base64UrlPayload, 'abC123!', true);
// Encode Signature to Base64Url String
$base64UrlSignature = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($signature));
// Create JWT
$jwt = $base64UrlHeader . "." . $base64UrlPayload . "." . $base64UrlSignature;
2020-04-25 16:04:02 +00:00
2020-05-12 17:25:08 +00:00
return $jwt;
}
}
return false;
}
public function deleteToken($token){
Db::command ('DELETE FROM tokens WHERE token=?', array ($token));
return true;
}
public function validateToken($token){
$tokens = Db::loadAll('SELECT * FROM tokens WHERE token = ? AND expire >= CURRENT_TIMESTAMP AND blocked = 0;', array($token));
if (count($tokens) == 1) {
2020-04-25 16:04:02 +00:00
return true;
2020-05-12 17:25:08 +00:00
} else if (count($tokens) == 0) {
return false;
};
return false;
}
2020-03-31 09:13:21 +00:00
}