gestione gare pubbliche
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.
 
 
 
 
 

102 righe
3.7 KiB

<?php
namespace ProcurePlus {
use Exception;
/**
* Gestisce le richieste di federazione verso oidc-gateway
* http://repos.studioamica.local/amica-tools/oidc-gateway/
*/
class KeyStore
{
/**
* Genera un keypair nel path specificato
*
* @param string $path
* @return void
*/
public static function generateKeyPair(string $name, string $domain, int $bits = 4096, int $type = OPENSSL_KEYTYPE_RSA)
{
// Generiamo la chiave privata
$key = openssl_pkey_new([
"private_key_bits" => $bits,
"private_key_type" => $type,
]);
// Generiamo i pem
$rsa_pub = openssl_pkey_get_details($key)['key'];
openssl_pkey_export($key, $rsa_pem);
self::set($name, $domain, "public", $rsa_pub);
self::set($name, $domain, "private", $rsa_pem);
}
public static function getKeyPath(string $kid, string $domain)
{
global $config;
return realFolderPathOrCreate(
$config["procurePlus"]["keyPath"] . DIRECTORY_SEPARATOR .
$config["id-installazione"] . DIRECTORY_SEPARATOR .
$domain
) . DIRECTORY_SEPARATOR . $kid;
}
/**
* Elimina una chiave dal keystore
*
* @param string $name Nome della chiave
* @param string $domain Dominio della chiave
* @param string $kind Tipo di chiave (public/private)
*/
public static function delete(string $name, string $domain, string $kind)
{
// Cerchiamo il file
$extension = ["public" => ".rsa.pub", "private" => ".rsa.pem"][$kind] ?? null;
if($extension === null) throw new Exception("Tipo di chiave '$kind' non valido");
$path = self::getKeyPath($name, $domain) . $extension;
// Creiamo un backup se esiste già
if (file_exists($path)) {
unlink($path);
}
}
/**
* Aggiunge una chiave al keystore
*
* @param string $name Nome della chiave
* @param string $domain Dominio della chiave
* @param string $kind Tipo di chiave (public/private)
* @param string $data Contenuto della chiave
*/
public static function set(string $name, string $domain, string $kind, string $data)
{
// Cerchiamo il file
$extension = ["public" => ".rsa.pub", "private" => ".rsa.pem"][$kind] ?? null;
if($extension === null) throw new Exception("Tipo di chiave '$kind' non valido");
$path = self::getKeyPath($name, $domain) . $extension;
// Creiamo un backup se esiste già
if (file_exists($path)) {
file_put_contents($path . ".bkp." . uniqid(date("Ymd")), file_get_contents($path));
}
file_put_contents($path, $data);
}
/**
* Ottiene una chiave dal keystore
*
* @param string $name Nome della chiave
* @param string $domain Dominio della chiave
* @param string $kind Tipo di chiave (public/private)
* @return string Contenuto della chiave
*/
public static function get(string $name, string $domain, string $kind)
{
$extension = ["public" => ".rsa.pub", "private" => ".rsa.pem"][$kind] ?? null;
if($extension === null) throw new Exception("Tipo di chiave '$kind' non valido");
$path = self::getKeyPath($name, $domain) . $extension;
if (!file_exists($path)) throw new Exception("Impossibile trovare la chiave {$path}");
return file_get_contents($path);
}
}
}