<?php
/**
* SBrook\JWS\Jws
*/
namespace SBrook\JWS;
use SBrook\JWS\Exception\JwsException;
/**
* Class Jws
* @package SBrook\JWS
* @throws JwsException:
* Encode:
* 10. Header should be an array
* 11. Payload should be a non empty string
* 12. Unknown signature algorithm in header
* Decode:
* 20. JWS should be a non empty string
* 21. Invalid JWS header
* 22. Error while decoding JWS header
* 23. Error while decoding JWS payload
*/
abstract class Jws {
/**
* Create JWS from payload and optional header and sign it.
* @param $payload - Payload.
* @param $header - Header data.
*/
abstract public function sign($payload, $header);
/**
* Verify JWS signature.
* @param $jws - JWS.
*/
abstract public function verify($jws);
/**
* Check validity of signature algorithm.
* @param string $algorithm - Algorithm name.
* @return bool - TRUE on valid algorithm, FALSE on invalid.
*/
abstract protected function isValidAlgorithm(string $algorithm): bool;
/**
* Get JWS header.
* @param string $jws - JWS.
* @return array - Decoded JWS header.
* @throws JwsException
*/
public function getHeader($jws) {
throw new JwsException("Error while decoding JWS header", 22);
} else {
return $header;
}
} else {
throw new JwsException("JWS should be a non empty string", 20);
}
}
/**
* Get JWS payload.
* @param string $jws - JWS.
* @return string - Decoded JWS payload.
* @throws JwsException
*/
public function getPayload($jws) {
if ($payload) {
return $payload;
} else {
throw new JwsException("Error while decoding JWS payload", 23);
}
} else {
throw new JwsException("JWS should be a non empty string", 20);
}
}
/**
* Validate and prepare data to sign JWS.
* @param string $defaultAlgo - Default signature algorithm name.
* @param string $payload - Payload.
* @param array $header - Header data.
* @return array - Required data to sign JWS.
* @throws JwsException
*/
protected function prepareSign
($defaultAlgo, $payload, $header): array {
// Remove header parameters with empty string values:
foreach ($header as $key => $value) {
}
}
// If not specified, set default signature algorithm:
$header["alg"] = $defaultAlgo;
}
// Don't trust anyone:
if ($this->isValidAlgorithm($header["alg"])) {
return [
"alg" => $header["alg"],
];
} else {
throw new JwsException("Unknown signature algorithm in header", 12);
}
} else {
throw new JwsException("Payload should be a non empty string", 11);
}
} else {
throw new JwsException("Header should be an array", 10);
}
}
/**
* Validate and prepare data to verify JWS.
* @param string $jws - JWS.
* @return array - Required data to verify JWS.
* @throws JwsException
*/
protected function prepareVerify
($jws): array {
return [
"h" => $h,
"p" => $p
];
} else {
throw new JwsException("Invalid JWS header", 21);
}
} else {
throw new JwsException("JWS should be a non empty string", 20);
}
}
}