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.
134 righe
4.4 KiB
134 righe
4.4 KiB
<?php |
|
namespace ProcurePlus { |
|
/** |
|
* Gestisce le richieste di federazione verso oidc-gateway |
|
* http://repos.studioamica.local/amica-tools/oidc-gateway/ |
|
*/ |
|
class Federate |
|
{ |
|
/** |
|
* Genera, se necessario, la chiave di federazione interna, collegata al gateway, usata per firmare le richieste di federazione |
|
* dopodichè la mostra per poterla inserire nel gateway |
|
* |
|
* @param boolean $format Se true viene ritornata con DOM stilizzato |
|
* @return string|array |
|
*/ |
|
public static function getFederationKey($format = true) |
|
{ |
|
global $config; |
|
// Chiave di federazione |
|
$federationKeyPath = KeyStore::getKeyPath("federation", "global"); |
|
|
|
// Se non esiste, andiamo a crearla |
|
if (!file_exists($federationKeyPath . ".rsa.pem") || !file_exists($federationKeyPath . ".rsa.pub")) { |
|
KeyStore::generateKeyPair("federation", "global"); |
|
} |
|
|
|
// Carichiamo la chiave |
|
$federationPublicKey = KeyStore::get("federation", "global", "public"); |
|
|
|
if (!$format) { |
|
return $federationPublicKey; |
|
} |
|
$idInstallazione = $config['id-installazione']; |
|
$publicKey = str_replace("\n", "\\n", $federationPublicKey); |
|
return <<<HTML |
|
<style> |
|
body { |
|
background: #222; |
|
color: #ddd; |
|
font-family: sans-serif; |
|
} |
|
|
|
a { |
|
color: #ddd !important; |
|
text-decoration: none; |
|
} |
|
|
|
img { |
|
padding: 1em; |
|
background: #ccc; |
|
border-radius: 10px; |
|
} |
|
|
|
h1 { |
|
font-weight: 600; |
|
} |
|
|
|
h2 { |
|
font-weight: 100; |
|
} |
|
|
|
pre { |
|
user-select: all; |
|
} |
|
</style> |
|
<h1>ProcurePlus integration</h1> |
|
<h2>Inserisci questa chiave pubblica nell'istanza di ProcurePlus</h2> |
|
<pre><?= $federationPublicKey ?></pre> |
|
<h2>Query pronta</h2> |
|
<pre> |
|
db.tuttogare_keys.insertOne( |
|
{ |
|
'kid' : 'federation', |
|
'platform' : '{$idInstallazione}', |
|
'public_key' : '{$publicKey}', |
|
} |
|
); |
|
</pre> |
|
HTML; |
|
} |
|
/** |
|
* Nota bene: richiede che la configurazione del tenant sia configurata |
|
* Questo codice fa quanto segue: |
|
* - Genera se necessario le chiavi di core e log per il tenant attuale |
|
* - Effettua una richiesta di federazione a ProcurePlus |
|
* |
|
* @param boolean $force_generation Se true, forza la rigenerazione delle chiavi |
|
* @return boolean |
|
*/ |
|
public static function federate() |
|
{ |
|
|
|
// Verifichiamo che la chiave di federazione esista |
|
$federationKeyPath = KeyStore::getKeyPath("federation", "global"); |
|
if (!file_exists($federationKeyPath . ".rsa.pem") || !file_exists($federationKeyPath . ".rsa.pub")) { |
|
throw new \Exception("Non hai ancora creato una chiave di federazione per questa piattaforma."); |
|
} |
|
|
|
// Otteniamo il dominio corrente |
|
$domain = getCurrentDomain(); |
|
|
|
// Creiamo il keypair di federazione ma conserviamo solo la chiave privata su filesystem. |
|
KeyStore::generateKeyPair("sign", $domain); |
|
// La chiave pubblica rimarrà solo in memoria per essere inviata |
|
$publicKey = KeyStore::get("sign", $domain, "public"); |
|
KeyStore::delete("sign", $domain, "public"); |
|
|
|
// Firmiamo la richiesta con la chiave di federazione |
|
$federationPrivateKey = KeyStore::get("federation", "global", "private"); |
|
|
|
// Effettuiamo la richiesta a oidc-gateway |
|
$raw_response = HTTP::signedRequest( |
|
"federation-request", // Richiesta di federazione |
|
[ |
|
"domain" => $domain, // Il dominio da federare viene specificato nel payload |
|
"public_key" => $publicKey |
|
], |
|
"global", // La richiesta invece parte come globale di piattaforma |
|
$federationPrivateKey // Firmata appunto con la chiave di piattaforma |
|
); |
|
if (valid_json($raw_response, $response, true)) { |
|
if (!isset($response["public_key"])) { |
|
throw new \Exception($raw_response); |
|
} |
|
KeyStore::set("sign", $domain, "public", $response["public_key"]); |
|
|
|
return true; |
|
} else { |
|
return false; |
|
} |
|
} |
|
} |
|
|
|
}
|
|
|