<?php
namespace Extension;
 include_once __DIR__ . DIRECTORY_SEPARATOR . 'php-jwt-master/src/BeforeValidException.php';
 include_once __DIR__ . DIRECTORY_SEPARATOR . 'php-jwt-master/src/ExpiredException.php';
 include_once __DIR__ . DIRECTORY_SEPARATOR . 'php-jwt-master/src/SignatureInvalidException.php';
 include_once __DIR__ . DIRECTORY_SEPARATOR . 'php-jwt-master/src/JWT.php';
 use \Firebase\JWT\JWT;
 use \Firebase\JWT\SignatureInvalidException;
 use \Firebase\JWT\ExpiredException;
 use \DomainException;
 use TCMS\AuthExtension;
 use TCMS\Extension;
 class ApiAuthDefault extends AuthExtension {
 private $secretKey = "sadfweru2094@sdvcs01ssd!1d346@57gfb";
 private $userInfo = null;
 public function __construct() {
 $this->loginUrl = url('/getToken');
 \Model::add('GroupUserModel', \VnChocola\Model\GroupUserModel::class, true);
 \Model::add('UserModel');
 \Model::add('BranchsModel', \VnChocola\Model\BranchsModel::class, true);
 }
 public function login(string $username, string $password, ?array $option = null) {
 $userModel = \Model::load('UserModel');
 if(!$userModel) {
 http_response_code(500);
 exit();
 }
 $password = $this->encodePass($password);
 $user = $userModel->select('id', 'name')->where('username', $username)->where('password', $password)->first();
 if($user) {
 $issueTime = time() - 10;
 $notBeforeTime = $issueTime;
 $expireTime = $notBeforeTime + 86500;
 $jwtData = array( "iss" => "http://example.org", "aud" => "http://example.com", "iat" => $issueTime, "nbf" => $notBeforeTime, "exp" => $expireTime, "data" => [ 'id' => $user->id, 'name' => $user->name ] );
 try {
 $token = JWT::encode($jwtData, $this->secretKey);
 return [ 'token' => $token, 'id' => $user->id, 'name' => $user->name ];
 }
 catch (Extension $e) {
 }
 catch (SignatureInvalidException $e) {
 }
 catch (ExpiredException $e) {
 }
 catch (DomainException $e) {
 }
 }
 return null;
 }
 public function decodeToken($token) {
 try{
 $tokenParts = explode(".", $token);
 $tokenHeader = base64_decode($tokenParts[0]);
 $tokenPayload = base64_decode($tokenParts[1]);
 $jwtHeader = json_decode($tokenHeader);
 $jwtPayload = json_decode($tokenPayload);
 if(!isset($jwtPayload->data)) throw new \TCMS\TException('Token không hợp lệ!');
 return $jwtPayload->data;
 }
catch(\Exception $ex) {
 throw new \TCMS\TException('Token không hợp lệ!');
 }
 }
 public function logout() {
 }
 public function auth() {
 if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
 try {
 $t = substr($_SERVER['HTTP_AUTHORIZATION'], 7);
 $decoded = JWT::decode($t, $this->secretKey, array('HS256'));
 $this->userInfo = $decoded->data;
 return $this->userInfo;
 }
 catch (SignatureInvalidException $e) {
 throw new \TCMS\TException($e->getMessage(), 504);
 }
 catch (DomainException $e) {
 throw new \TCMS\TException($e->getMessage(), 504);
 }
 catch (ExpiredException $e) {
 throw new \TCMS\TException($e->getMessage(), 504);
 }
 }
 return false;
 }
 public function getName() {
 return $this->userInfo->name;
 }
 private $groupRoles = null;
 private $userCustomUser = null;
 public function hasRole($role) {
 if(!$this->userInfo->id) return false;
 if(!$this->userCustomUser) {
 $userModel = \Model::load('UserModel');
 $u = $userModel->find($this->userInfo->id);
 if(!$u) throw new \TCMS\TException("Không tìm thấy thông tin người dùng đang đăng nhập", 500);
 require_once(APP_PATH . 'models' . DIRECTORY_SEPARATOR . 'CustomUserModel.php');
 $this->userCustomUser = new \CustomUserModel();
 $this->userCustomUser->copy($u);
 }
 if(!$this->userCustomUser) return false;
 if(!$this->groupRoles) {
 $model = \Model::load('GroupUserModel');
 $group = $model->select('roles')->where('id', $this->userCustomUser->groupId)->first();
 if(!$group) return false;
 $this->groupRoles = $group->roles;
 }
 return strpos($this->groupRoles, ','. $role .',') !== false;
 }
 public function getUser() {
 if($this->userCustomUser) return $this->userCustomUser;
 if(!$this->userInfo->id) return false;
 $userModel = \Model::load('UserModel');
 $user = $userModel->find($this->userInfo->id);
 if(!$user) throw new \TCMS\TException("Không tìm thấy thông tin người dùng đang đăng nhập", 500);
 require_once(APP_PATH . 'models' . DIRECTORY_SEPARATOR . 'CustomUserModel.php');
 $this->userCustomUser = new \CustomUserModel();
 $this->userCustomUser->copy($user);
 return $this->userCustomUser;
 }
 public function encodePass($password) {
 $secretKey = 'a@d12s01@2sdc!mpc213212ss1tvthoa';
 $iv = substr($secretKey, 0,16);
 $encrypted = openssl_encrypt($password, 'AES-256-CBC', $secretKey,0,$iv);
 $hmac = hash_hmac('md5', $encrypted, $secretKey);
 return $encrypted;
 }
 public function updateUser($user) {
 }
 public function createUser($user) {
 }
 }
 ?>