diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2020-05-15 20:05:26 +0200 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2020-05-15 20:05:26 +0200 |
commit | ded1b2a19ee238543d561b6f26312458d2a43974 (patch) | |
tree | b5569c95266ad7482298a7feffb6a3be24a19844 /app/class/Modelconnect.php | |
parent | 3f69df3b93510f2704f3af9d54b9bf3b34d0e6bb (diff) | |
download | wcms-ded1b2a19ee238543d561b6f26312458d2a43974.tar.gz wcms-ded1b2a19ee238543d561b6f26312458d2a43974.zip |
new cookie session system
composer require JWT
user stores sessions
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); + } + } +} |