Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
56 righe
2.2 KiB
56 righe
2.2 KiB
<?php |
|
|
|
namespace ProcurePlus { |
|
|
|
|
|
class In { |
|
/** |
|
* Autentica una richiesta proveniente da ProcurePlus. Ritorna true in caso di successo altrimenti risponde con un JSON consono |
|
* |
|
* @return boolean |
|
*/ |
|
public static function authenticate() : bool { |
|
global $config; |
|
|
|
// Otteniamo dominio/piattaforma |
|
$domain = getCurrentDomain(); |
|
$platform = $config['id-installazione']; |
|
|
|
// Verifichiamo ci sia il JWT |
|
$headers = getallheaders(); |
|
$auth = $headers["Authorization"] ?? null; |
|
if(!$auth || !str_starts_with($auth, "Bearer ")) { |
|
return HTTP::respondJson(['error' => 'Unauthorized - JWT Authentication requied'], 401); |
|
} |
|
$token = explode(" ", $auth)[1]; |
|
$jwt = \JOSE_JWT::decode($token); |
|
|
|
// Verifichiamo che la richiesta sia destinata alla nostra piattaforma e dominio |
|
if($domain != ($jwt->header["domain"] ?? "") || $platform != ($jwt->header["platform"] ?? "") ) { |
|
return HTTP::respondJson(['error' => 'Unauthorized - Invalid JWT for current platform and domain'], 401); |
|
} |
|
|
|
// Otteniamo la chiave pubblica per verificare la firma |
|
try { |
|
$verificationKey = KeyStore::get("sign", $domain, "public"); |
|
} catch(\Exception $ex) { |
|
return HTTP::respondJson(['error' => 'Unauthorized - This platform/domain combo might not be federated'] ); |
|
} |
|
// Verifichiamo la firma |
|
try { |
|
$jws = $jwt->verify($verificationKey, "RS512"); |
|
} catch(\JOSE_Exception_VerificationFailed $ex) { |
|
return HTTP::respondJson(['error' => "Unauthorized - Invalid signature"], 401); |
|
} |
|
|
|
// Il JWT è firmato correttamente, ora verifichiamo che siano firmati anche i contenuti della richiesta |
|
$bodyHash = sha1(file_get_contents("php://input")); |
|
if($bodyHash !== ($jws->claims["bodyHash"] ?? "")) { |
|
return HTTP::respondJson(['error' => 'Unauthorized - Tampered body'], 401); |
|
} |
|
|
|
// Richiesta autenticata |
|
return true; |
|
} |
|
} |
|
}
|
|
|