diff --git a/App/Config/site.php.example b/App/Config/site.php.example index 7208a70..ea9eada 100644 --- a/App/Config/site.php.example +++ b/App/Config/site.php.example @@ -2,4 +2,5 @@ return [ 'key' => 'CHANGE_ME', + 'url' => 'https://autenticazione.lavoripubblici.sicilia.it/', ]; diff --git a/App/Core/Controller.php b/App/Core/Controller.php index d5fcc1d..0c75f2b 100644 --- a/App/Core/Controller.php +++ b/App/Core/Controller.php @@ -4,7 +4,6 @@ namespace MwgAuth\Core; class Controller { - private static $charset = 'UTF-8'; private static $contentType = 'application/json'; private static $responseCode = 200; diff --git a/App/Core/Model.php b/App/Core/Model.php index 6e088c1..980f936 100644 --- a/App/Core/Model.php +++ b/App/Core/Model.php @@ -78,7 +78,11 @@ class Model foreach ($conditions as $field => $value) { $where[] = "(`$field`=:$field)"; } - return "SELECT `" . implode('`,`', $fields) . "` FROM `$table` WHERE " . implode(' AND ', $where); + $sql = "SELECT `" . implode('`,`', $fields) . "` FROM `$table`"; + if (count($where)) { + $sql .= ' WHERE ' . implode(' AND ', $where); + } + return $sql; } protected static function insert(string $table, array $fields): bool diff --git a/App/Models/Cns.php b/App/Models/Cns.php index b228359..37d27a0 100644 --- a/App/Models/Cns.php +++ b/App/Models/Cns.php @@ -23,7 +23,11 @@ class Cns extends Model $this->verificaCertificato(); // https://www.agid.gov.it/sites/default/files/repository_files/documentazione_trasparenza/strutturacertificatoautenticazionecns_v1.1_.pdf if (isset($this->certificato['subject']['serialNumber'])) { - $this->codiceFiscale = preg_replace('/^[A-Za-z]{2}:/', '', $this->certificato['subject']['serialNumber']); + $this->codiceFiscale = preg_replace( + '/^[A-Za-z]{2}:/', + '', + $this->certificato['subject']['serialNumber'] + ); } else { $this->codiceFiscale = explode('/', $this->certificato['subject']['commonName'])[0]; } @@ -78,13 +82,15 @@ class Cns extends Model private function verificaCertificato() { - // if (!isset($this->certificato['subject']['commonName']) || - // !isset($this->certificato['subject']['surname']) || - // !isset($this->certificato['subject']['givenName']) || - // isset($this->certificato['subject']['serialNumber']) || - // !isset($this->certificato['extensions']['certificatePolicies'])) { - $this->salvaCertificato(); - // } + if ( + !isset($this->certificato['subject']['commonName']) + || !isset($this->certificato['subject']['surname']) + || !isset($this->certificato['subject']['givenName']) + || isset($this->certificato['subject']['serialNumber']) + || !isset($this->certificato['extensions']['certificatePolicies']) + ) { + $this->salvaCertificato(); + } } private function salvaCertificato() diff --git a/App/Models/Token.php b/App/Models/Token.php new file mode 100644 index 0000000..08ee0d1 --- /dev/null +++ b/App/Models/Token.php @@ -0,0 +1,22 @@ + $token]); + if (null === $token) { + return true; + } + $user_id = self::selectOne('sessions', ['user_id'], ['auth' => $token['auth']]); + if (null === $user_id) { + return true; + } + $useCns = self::selectOne('users', ['use_cns'], ['id' => $user_id['user_id']]); + return $useCns['use_cns'] == 1; + } +} diff --git a/App/Models/User.php b/App/Models/User.php index 01b9074..05243b0 100644 --- a/App/Models/User.php +++ b/App/Models/User.php @@ -62,7 +62,7 @@ class User extends Model public function getLoginData(string $token): ?array { - $ret = $this->select('logins', ['codice_fiscale', 'nome', 'cognome'], ['token' => $token]); + $ret = $this->select('logins', ['codice_fiscale', 'nome', 'cognome', 'email'], ['token' => $token]); return empty($ret) ? null : $ret[0]; } @@ -90,13 +90,19 @@ class User extends Model return $this->userName; } - public static function setLoginData(string $token, string $codiceFiscale, string $nome, string $cognome): bool - { + public static function setLoginData( + string $token, + string $codiceFiscale, + string $nome, + string $cognome, + ?string $email = null + ): bool { return self::insert('logins', [ 'token' => $token, 'codice_fiscale' => $codiceFiscale, 'nome' => $nome, 'cognome' => $cognome, + 'email' => $email, ]); } @@ -121,7 +127,7 @@ class User extends Model return Config::getValue('site', 'key'); } - private static function hashPassword(string $password): string + protected static function hashPassword(string $password): string { $siteKey = self::getSiteKey(); return hash('sha256', $siteKey . $password); diff --git a/App/Models/UserTool.php b/App/Models/UserTool.php new file mode 100644 index 0000000..04ac206 --- /dev/null +++ b/App/Models/UserTool.php @@ -0,0 +1,33 @@ + $username, + 'password' => self::hashPassword($password), + 'remote' => $remote, + 'use_cns' => $useCns, + ]); + } + + public static function list(): array + { + return self::select('users', ['id', 'username', 'remote', 'use_cns'], []); + } + + public static function token(string $username): ?string + { + $rows = self::select('users', ['id'], ['username' => $username]); + if (empty($rows)) { + return null; + } + $user = new User($rows[0]['id'], $username); + return $user->getToken('http://localhost/callback'); + } +} diff --git a/App/bootstrap.php b/App/bootstrap.php index ff1ced4..40040ee 100644 --- a/App/bootstrap.php +++ b/App/bootstrap.php @@ -2,5 +2,5 @@ namespace MwgAuth\Core; -require_once(__DIR__.'/Core/AutoLoader.php'); +require_once(__DIR__ . '/Core/AutoLoader.php'); AutoLoader::register(); diff --git a/html/index.php b/html/index.php index 2a3b9ca..8044393 100644 --- a/html/index.php +++ b/html/index.php @@ -1,4 +1,12 @@ - +
@@ -16,23 +24,25 @@- La Carta Nazionale dei Servizi o CNS è una smart card o una chiavetta USB che contiene un - "certificato digitale" di autenticazione personale, utile per accedere ai servizi online della - Regione Siciliana. -
- -+ La Carta Nazionale dei Servizi o CNS è una smart card o una chiavetta USB che contiene un + "certificato digitale" di autenticazione personale, utile per accedere ai servizi online della + Regione Siciliana. +
+ +