File indexing completed on 2024-04-14 05:47:03

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: 10.10.2018
0026  */
0027 class Application_Model_Jwt
0028 {
0029 
0030     public static function encode($member_id)
0031     {
0032         $config = Zend_Registry::get('config')->settings->jwt;
0033         $member_data = self::getMemberData($member_id);
0034         $payload = self::buildPayload($member_data, $config);
0035 
0036         return JWT::encode($payload, $config->secret, $algo = 'HS256');
0037     }
0038 
0039     private static function getMemberData($member_id)
0040     {
0041         $model = new Default_Model_Member();
0042 
0043         return $model->fetchMemberData($member_id)->toArray();
0044     }
0045 
0046     private static function buildPayload($member_data, $config)
0047     {
0048         $date = new DateTime();
0049         $interval = DateInterval::createFromDateString($config->expire->cookie);
0050         $payload['exp'] = $date->add($interval)->getTimestamp();
0051         $payload['vt'] = 4; //type=cookie_ltat
0052         $payload['user'] = $member_data['external_id'];
0053         $payload['hash'] = crc32($member_data['username'] . $member_data['mail'] . $member_data['password']);
0054 
0055         return $payload;
0056     }
0057 
0058     public static function decode($jwt, $verify = true)
0059     {
0060         $config = Zend_Registry::get('config')->settings->jwt;
0061 
0062         return JWT::decode($jwt, $config->secret, $verify);
0063     }
0064 
0065     public static function encodeFromArray(array $payload)
0066     {
0067         $config = Zend_Registry::get('config')->settings->jwt;
0068 
0069         return JWT::encode($payload, $config->secret, $algo = 'HS256');
0070     }
0071 
0072 }