aboutsummaryrefslogtreecommitdiff
path: root/app/class/Modelconnect.php
blob: 1201d36d8bdc0abcb29da8739ee8e05186d65d4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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);
        }
    }
}