Security service

pull/8/head
sundowndev 2018-07-12 18:04:53 +02:00
parent 5707282614
commit 40197e0102
1 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,81 @@
<?php
namespace App\Service;
/**
* Class Security
* @package App\Service
*/
class Security
{
/**
* @var Session
*/
private $session;
/**
* Security constructor.
*/
public function __construct()
{
$this->session = new Session();
}
/**
* @return string
*/
public function generateToken()
{
$token = md5(uniqid(rand(), TRUE));
return $token;
}
/**
* @param $cookie
* @return bool
*/
public function isLogged($cookie)
{
if (is_null($this->session->getSession($cookie))) {
return false;
} else {
return true;
}
}
/**
* @param $csrf
* @param $cookie
* @return bool
*/
public function isValidCsrf($csrf, $cookie)
{
if (is_null($session = $this->session->getSession($cookie))) {
return false;
}
return $session['csrf'] === $csrf;
}
/**
* @param $password
* @return bool|string
*/
public function passwordHash($password)
{
return password_hash($password, PASSWORD_BCRYPT, [
'cost' => 12
]);
}
/**
* @param $password
* @param $hash
* @return bool
*/
public function passwordVerify($password, $hash)
{
return \password_verify($password, $hash);
}
}