Sfoglia il codice sorgente

Claim email da Spie e CNS disattivabile per utente

main
Ture La Manna 6 mesi fa
parent
commit
6d40398f53
  1. 1
      App/Config/site.php.example
  2. 1
      App/Core/Controller.php
  3. 6
      App/Core/Model.php
  4. 20
      App/Models/Cns.php
  5. 22
      App/Models/Token.php
  6. 14
      App/Models/User.php
  7. 33
      App/Models/UserTool.php
  8. 2
      App/bootstrap.php
  9. 12
      html/index.php
  10. 4
      html/spid/index.php
  11. 8
      setup/file/autenticazione.sql
  12. 2
      setup/file/update_001.sql
  13. 14
      tools/get_token
  14. 27
      tools/user_add
  15. 11
      tools/user_list

1
App/Config/site.php.example

@ -2,4 +2,5 @@
return [ return [
'key' => 'CHANGE_ME', 'key' => 'CHANGE_ME',
'url' => 'https://autenticazione.lavoripubblici.sicilia.it/',
]; ];

1
App/Core/Controller.php

@ -4,7 +4,6 @@ namespace MwgAuth\Core;
class Controller class Controller
{ {
private static $charset = 'UTF-8'; private static $charset = 'UTF-8';
private static $contentType = 'application/json'; private static $contentType = 'application/json';
private static $responseCode = 200; private static $responseCode = 200;

6
App/Core/Model.php

@ -78,7 +78,11 @@ class Model
foreach ($conditions as $field => $value) { foreach ($conditions as $field => $value) {
$where[] = "(`$field`=:$field)"; $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 protected static function insert(string $table, array $fields): bool

20
App/Models/Cns.php

@ -23,7 +23,11 @@ class Cns extends Model
$this->verificaCertificato(); $this->verificaCertificato();
// https://www.agid.gov.it/sites/default/files/repository_files/documentazione_trasparenza/strutturacertificatoautenticazionecns_v1.1_.pdf // https://www.agid.gov.it/sites/default/files/repository_files/documentazione_trasparenza/strutturacertificatoautenticazionecns_v1.1_.pdf
if (isset($this->certificato['subject']['serialNumber'])) { 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 { } else {
$this->codiceFiscale = explode('/', $this->certificato['subject']['commonName'])[0]; $this->codiceFiscale = explode('/', $this->certificato['subject']['commonName'])[0];
} }
@ -78,13 +82,15 @@ class Cns extends Model
private function verificaCertificato() private function verificaCertificato()
{ {
// if (!isset($this->certificato['subject']['commonName']) || if (
// !isset($this->certificato['subject']['surname']) || !isset($this->certificato['subject']['commonName'])
// !isset($this->certificato['subject']['givenName']) || || !isset($this->certificato['subject']['surname'])
// isset($this->certificato['subject']['serialNumber']) || || !isset($this->certificato['subject']['givenName'])
// !isset($this->certificato['extensions']['certificatePolicies'])) { || isset($this->certificato['subject']['serialNumber'])
|| !isset($this->certificato['extensions']['certificatePolicies'])
) {
$this->salvaCertificato(); $this->salvaCertificato();
// } }
} }
private function salvaCertificato() private function salvaCertificato()

22
App/Models/Token.php

@ -0,0 +1,22 @@
<?php
namespace MwgAuth\Models;
use MwgAuth\Core\Model;
class Token extends Model
{
public static function useCns(string $token): bool
{
$token = self::selectOne('tokens', ['auth'], ['token' => $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;
}
}

14
App/Models/User.php

@ -62,7 +62,7 @@ class User extends Model
public function getLoginData(string $token): ?array 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]; return empty($ret) ? null : $ret[0];
} }
@ -90,13 +90,19 @@ class User extends Model
return $this->userName; 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', [ return self::insert('logins', [
'token' => $token, 'token' => $token,
'codice_fiscale' => $codiceFiscale, 'codice_fiscale' => $codiceFiscale,
'nome' => $nome, 'nome' => $nome,
'cognome' => $cognome, 'cognome' => $cognome,
'email' => $email,
]); ]);
} }
@ -121,7 +127,7 @@ class User extends Model
return Config::getValue('site', 'key'); return Config::getValue('site', 'key');
} }
private static function hashPassword(string $password): string protected static function hashPassword(string $password): string
{ {
$siteKey = self::getSiteKey(); $siteKey = self::getSiteKey();
return hash('sha256', $siteKey . $password); return hash('sha256', $siteKey . $password);

33
App/Models/UserTool.php

@ -0,0 +1,33 @@
<?php
namespace MwgAuth\Models;
use MwgAuth\Core\Model;
class UserTool extends User
{
public static function add(string $username, string $password, string $remote, bool $useCns): bool
{
return self::insert('users', [
'username' => $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');
}
}

2
App/bootstrap.php

@ -2,5 +2,5 @@
namespace MwgAuth\Core; namespace MwgAuth\Core;
require_once(__DIR__.'/Core/AutoLoader.php'); require_once(__DIR__ . '/Core/AutoLoader.php');
AutoLoader::register(); AutoLoader::register();

12
html/index.php

@ -1,4 +1,12 @@
<!doctype html> <?php
use MwgAuth\Models\Token;
require_once(dirname(__DIR__) . '/App/bootstrap.php');
$useCns = Token::useCns($_GET['t'] ?? '');
?><!doctype html>
<html lang="it"> <html lang="it">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
@ -16,6 +24,7 @@
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<?php if ($useCns) : ?>
<div class="col-md-6 col-xs-12"> <div class="col-md-6 col-xs-12">
<h2>Accedi con CNS</h2> <h2>Accedi con CNS</h2>
<hr class="solid"> <hr class="solid">
@ -33,6 +42,7 @@
</button> </button>
</form> </form>
</div> </div>
<?php endif ?>
<div class="col-md-6 col-xs-12"> <div class="col-md-6 col-xs-12">
<h2>Accedi con SPID o CieID</h2> <h2>Accedi con SPID o CieID</h2>
<hr class="solid"> <hr class="solid">

4
html/spid/index.php

@ -8,11 +8,13 @@ $token = $_GET['t'] ?? null;
if ('' === ($token ?? '')) { if ('' === ($token ?? '')) {
include($_SERVER['DOCUMENT_ROOT'] . '/errors/generic.php'); include($_SERVER['DOCUMENT_ROOT'] . '/errors/generic.php');
} else { } else {
@file_put_contents("/var/www/html/cns/policy/claims-$token.txt", print_r($_SERVER, true));
User::setLoginData( User::setLoginData(
$token, $token,
$_SERVER['SPID_claim_fiscalNumber'], $_SERVER['SPID_claim_fiscalNumber'],
$_SERVER['SPID_claim_given_name'], $_SERVER['SPID_claim_given_name'],
$_SERVER['SPID_claim_family_name'] $_SERVER['SPID_claim_family_name'],
$_SERVER['SPID_claim_email'],
); );
$callback = User::getCallback($token); $callback = User::getCallback($token);
if (null === $callback) { if (null === $callback) {

8
setup/file/autenticazione.sql

@ -15,6 +15,7 @@ CREATE TABLE `logins` (
`codice_fiscale` varchar(255) DEFAULT NULL, `codice_fiscale` varchar(255) DEFAULT NULL,
`nome` varchar(255) DEFAULT NULL, `nome` varchar(255) DEFAULT NULL,
`cognome` varchar(255) DEFAULT NULL, `cognome` varchar(255) DEFAULT NULL,
`email` varchar(255) NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL,
`modified_at` timestamp NULL DEFAULT NULL, `modified_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`token`) PRIMARY KEY (`token`)
@ -46,17 +47,14 @@ CREATE TABLE `users` (
`username` varchar(255) NOT NULL, `username` varchar(255) NOT NULL,
`password` char(64) NOT NULL, `password` char(64) NOT NULL,
`remote` varchar(45) DEFAULT NULL, `remote` varchar(45) DEFAULT NULL,
`use_cns` BOOLEAN NOT NULL DEFAULT TRUE,
`created_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL, `modified_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL, `deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `users_username_remote_unique` (`username`,`remote`) UNIQUE KEY `users_username_remote_unique` (`username`,`remote`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO `users`(`id`,`username`,`password`,`remote`,`created_at`,`updated_at`,`deleted_at`) VALUES
(1,'sismica','268633d46fdf00502dba60facce2ba5eec6a5d86c843c19f3f3b1ef8c0342ada','192.168.15.*',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,NULL),
(2,'sismica','268633d46fdf00502dba60facce2ba5eec6a5d86c843c19f3f3b1ef8c0342ada','79.8.172.13',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,NULL);
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;

2
setup/file/update_001.sql

@ -0,0 +1,2 @@
ALTER TANLE `logins` ADD COLUMN `email` VARCHAR(255) NULL DEFAULT NULL AFTER `cognome`;
ALTER TANLE `users` ADD COLUMN `use_cns` BOOLEAN NOT NULL DEFAULT TRUE AFTER `remote`;

14
tools/get_token

@ -0,0 +1,14 @@
#!/usr/bin/env php
<?php
use MwgAuth\Core\Config;
use MwgAuth\Models\UserTool;
require_once(dirname(__DIR__) . '/App/bootstrap.php');
if ($argc < 2) {
echo "Usage: $argv[0] <username>\n";
exit(1);
}
$token = UserTool::token($argv[1]);
echo Config::getValue('site', 'url') . "?t=$token\n";

27
tools/user_add

@ -0,0 +1,27 @@
#!/usr/bin/env php
<?php
namespace Tools;
use MwgAuth\Models\UserTool;
require_once(dirname(__DIR__) . '/App/bootstrap.php');
$username = readline("Nome utente: ");
$password = readline("Password: ");
$remote = readline("Indirizzo remoto (default: *): ");
if (empty($remote)) {
$remote = '*';
}
$cns = readline("Usa CNS? (Y/n): ");
if (empty($cns)) {
$cns = 'y';
}
$cns = strtolower($cns) === 'y';
echo "Aggiungo utente...\n";
$result = UserTool::add($username, $password, $remote, $cns);
if ($result) {
echo "Utente aggiunto con successo.\n";
} else {
echo "Errore durante l'aggiunta dell'utente.\n";
}

11
tools/user_list

@ -0,0 +1,11 @@
#!/usr/bin/env php
<?php
use MwgAuth\Models\UserTool;
require_once(dirname(__DIR__) . '/App/bootstrap.php');
$users = UserTool::list();
foreach ($users as $user) {
echo "ID: {$user['id']}, Username: {$user['username']}, Remote: {$user['remote']}, Use CNS: {$user['use_cns']}\n";
}
Caricamento…
Annulla
Salva