File indexing completed on 2024-04-28 05:58:57

0001 <?php
0002 
0003 include_once APPLICATION_LIB . '/JWT.php';
0004 
0005 /**
0006  *  ocs-webserver
0007  *
0008  *  Copyright 2016 by pling GmbH.
0009  *
0010  *    This file is part of ocs-webserver.
0011  *
0012  *    This program is free software: you can redistribute it and/or modify
0013  *    it under the terms of the GNU Affero General Public License as
0014  *    published by the Free Software Foundation, either version 3 of the
0015  *    License, or (at your option) any later version.
0016  *
0017  *    This program is distributed in the hope that it will be useful,
0018  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0019  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0020  *    GNU Affero General Public License for more details.
0021  *
0022  *    You should have received a copy of the GNU Affero General Public License
0023  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0024  *
0025  * Created: 26.11.2018
0026  */
0027 class Local_LoginCookie
0028 {
0029 
0030     const secret = 'jrEeZKyGCCVJCa7Hsgwqv5wf21GREJ9j';
0031 
0032     public static function createJwt($data, $expire)
0033     {
0034         $payload = self::buildPayload($data, $expire);
0035 
0036         return JWT::encode($payload, self::secret, $algo = 'HS256');
0037     }
0038 
0039     private static function buildPayload($data, $expire)
0040     {
0041         $date = new DateTime();
0042         $interval = DateInterval::createFromDateString($expire);
0043         $payload['exp'] = $date->add($interval)->getTimestamp();
0044         $payload['data'] = $data;
0045 
0046         return $payload;
0047     }
0048 
0049     public static function readJwt($key)
0050     {
0051         $payload = JWT::decode($key, self::secret, true);
0052 
0053         if (self::isExpired($payload->exp)) {
0054             return false;
0055         }
0056 
0057         return $payload->data;
0058     }
0059 
0060     public static function isExpired($expiration_time)
0061     {
0062         $time = time();
0063 
0064         return $time < $expiration_time;
0065     }
0066 
0067 }