add support for cucked urls

This commit is contained in:
lolcat committed 2026-11-30 17:19:39 -05:00
1 parent e03c582f74
commit ffb1fc00f3
8 files changed
+223 -318

No files matched your search

+85
View File
@@ -334,4 +334,89 @@ class backend{
return false;
}
//
// SIGNATURES
//
public function hmac_sign(int $purpose, string $payload){
if(config::HMAC_SECRET === null){
return "";
}
$header = pack("CN", $purpose, intdiv(time(), 60));
$hash = hash_hmac("sha256", $header . $payload, config::HMAC_SECRET, true);
return rtrim(strtr(base64_encode(
$header . $hash
), "+/", "-_"), "=");
}
public function hmac_verify(int $expected_purpose, string $payload, string $signature){
if(config::HMAC_SECRET === null){
return [
"valid" => true
];
}
$decoded = base64_decode(strtr($signature, "-_", "+/"));
if(strlen($decoded) !== 37){
return [
"valid" => false,
"reason" => "Invalid signature size"
];
}
// extract bullshit
$purpose = unpack("C", $decoded)[1];
$timestamp = unpack("N", substr($decoded, 1, 4))[1];
$signature = substr($decoded, 5);
// check purpose
if($purpose !== $expected_purpose){
return [
"valid" => false,
"reason" => "This signature is used for something else"
];
}
// check expiration
$now = intdiv(time(), 60);
if(
$timestamp > $now ||
$now > $timestamp + config::HMAC_TTL
){
return [
"valid" => false,
"reason" => "Signature expired"
];
}
// check hmac
$r_header = pack("CN", $expected_purpose, $timestamp);
$r_hash = hash_hmac("sha256", $r_header . $payload, config::HMAC_SECRET, true);
if(!hash_equals($signature, $r_hash)){
return [
"valid" => false,
"reason" => "Signature mismatch"
];
}
return [
"valid" => true,
"timestamp" => $timestamp,
"signature" => $signature
];
}
}