diff options
Diffstat (limited to 'app/class/Modelconnect.php')
-rw-r--r-- | app/class/Modelconnect.php | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/app/class/Modelconnect.php b/app/class/Modelconnect.php new file mode 100644 index 0000000..1201d36 --- /dev/null +++ b/app/class/Modelconnect.php @@ -0,0 +1,45 @@ +<?php + +namespace Wcms; + +use Firebase\JWT\JWT; +use RuntimeException; +use Exception; + +class Modelconnect extends Model +{ + + /** + * @param string $userid + * @param string $wsession + * @param int $conservation + * @throws RuntimeException if secret key is not set or cant send cookie + */ + public function createauthcookie(string $userid, string $wsession, int $conservation) + { + $datas = [ + "userid" => $userid, + "wsession" => $wsession + ]; + if (empty(Config::secretkey())) { + throw new RuntimeException("Secret Key not set"); + } + $jwt = JWT::encode($datas, Config::secretkey()); + $cookie = setcookie('authtoken', $jwt, time() + $conservation * 24 * 3600, "", "", false, true); + if (!$cookie) { + throw new RuntimeException("Cant be send"); + } + } + + /** + * Check cookie using JWT + * @throws Exception + */ + public function checkcookie() + { + if (!empty($_COOKIE['authtoken'])) { + $datas = JWT::decode($_COOKIE['authtoken'], Config::secretkey(), ['HS256']); + return get_object_vars($datas); + } + } +} |