88 lines
2.4 KiB
PHP
88 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Aiko;
|
|
|
|
class TokenSanitation
|
|
{
|
|
private $token='';
|
|
private $errors=array();
|
|
public function __construct($jwt)
|
|
{
|
|
// harus di set per client
|
|
$clientChipper='542346';
|
|
$this->token=$this->unWrapToken($jwt,$clientChipper);
|
|
}
|
|
|
|
public function getToken(){
|
|
return $this->token;
|
|
}
|
|
|
|
public function getErros(){
|
|
return $this->errors;
|
|
}
|
|
|
|
private function unWrapToken($jwt, $chipper)
|
|
{
|
|
try {
|
|
if (strlen($chipper) <> 6) {
|
|
throw new \ErrorException('chipper failed');
|
|
}
|
|
|
|
$headerPreffix = (int) substr($chipper, 0, 1);
|
|
$headerSuffix = (int) substr($chipper, 1, 1);
|
|
$payloadPreffix = (int) substr($chipper, 2, 1);
|
|
$payloadSuffix = (int) substr($chipper, 3, 1);
|
|
$signPreffix = (int) substr($chipper, 4, 1);
|
|
$signSuffix = (int) substr($chipper, 5, 1);
|
|
|
|
$jwtPart = explode('.', $jwt);
|
|
|
|
|
|
|
|
if (count($jwtPart) != 4) {
|
|
throw new \ErrorException('token part invalid');
|
|
}
|
|
|
|
|
|
$newString = $this->removePreSuf($jwtPart[0], $headerPreffix, $headerSuffix);
|
|
if ($newString == false) {
|
|
throw new \ErrorException('failed clean wrapper header');
|
|
}
|
|
$header = $newString;
|
|
|
|
$newString = $this->removePreSuf($jwtPart[1], $payloadPreffix, $payloadSuffix);
|
|
if ($newString == false) {
|
|
throw new \ErrorException('failed clean wrapper payload');
|
|
}
|
|
$payload = $newString;
|
|
|
|
$newString = $this->removePreSuf($jwtPart[2], $signPreffix, $signSuffix);
|
|
if ($newString == false) {
|
|
throw new \ErrorException('failed clean wrapper sign');
|
|
}
|
|
$sign = $newString;
|
|
|
|
return $header . '.' . $payload . '.' . $sign;
|
|
} catch (\ErrorException $e) {
|
|
array_push($this->errors,array($e->getMessage()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private function removePreSuf($string, $preffix, $suffix)
|
|
{
|
|
$jum = strlen(trim($string));
|
|
$totWrapper = ($preffix + $suffix);
|
|
$tot = $totWrapper + 10; // set minimum text
|
|
if ($jum > $tot) {
|
|
$total = $jum - $totWrapper;
|
|
$newString = substr($string, $preffix, $total);
|
|
return $newString;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|