Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 7f4ce24

Browse files
committed
Storing entityIds instead of Names
* Because the SP / IdP can change the name, there is a problem that after changing the name, this SP / IdP is stored in statistics multiple times. * It is better to store SP/Idp identifier (EntityID and ClientID; This identifier should be unchangeable) and store tables for mapping identifier to Name.
1 parent e66cfd6 commit 7f4ce24

File tree

5 files changed

+95
-25
lines changed

5 files changed

+95
-25
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
5+
[Added]
6+
- Added mapping tables for mapping identifier to name
7+
8+
[Changed]
9+
- Storing entityIds instead of SpName/IdPName.
510

611
## [v1.2.1]
712
[Fixed]

config-templates/module_statisticsproxy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,21 @@
3838
*/
3939
'identityProvidersTableName' => 'identityProviders',
4040

41+
/*
42+
* Fill the table name for identityProvidersMap
43+
*/
44+
'identityProvidersMapTableName' => 'identityProvidersMap',
45+
4146
/*
4247
* Fill the table name for serviceProviders
4348
*/
4449
'serviceProvidersTableName' => 'serviceProviders',
4550

51+
/*
52+
* Fill the table name for serviceProviders
53+
*/
54+
'serviceProvidersMapTableName' => 'serviceProvidersMap',
55+
4656
/*
4757
* Fill true, if you want to use encryption, false if not.
4858
*/

config-templates/tables.sql

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Statistics for IdPs
1+
--Statistics for IdPs
22
CREATE TABLE identityProviders (
33
year INT NOT NULL,
44
month INT NOT NULL,
@@ -12,7 +12,7 @@ CREATE TABLE identityProviders (
1212
PRIMARY KEY (year, month, day, sourceIdp)
1313
);
1414

15-
#Statistics for services
15+
--Statistics for services
1616
CREATE TABLE serviceProviders(
1717
year INT NOT NULL,
1818
month INT NOT NULL,
@@ -24,4 +24,18 @@ CREATE TABLE serviceProviders(
2424
INDEX (year,month),
2525
INDEX (year,month,day),
2626
PRIMARY KEY (year, month, day, service)
27+
);
28+
29+
--Tables for mapping identifier to name
30+
CREATE TABLE identityProvidersMap(
31+
entityId VARCHAR(255) NOT NULL,
32+
name VARCHAR(255) NOT NULL,
33+
PRIMARY KEY (entityId)
34+
);
35+
36+
DROP TABLE serviceProvidersMap;
37+
CREATE TABLE serviceProvidersMap(
38+
identifier VARCHAR(255) NOT NULL,
39+
name VARCHAR(255) NOT NULL,
40+
PRIMARY KEY (identifier)
2741
);

lib/Auth/Process/DatabaseCommand.php

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,47 @@ public static function insertLogin(&$request, &$date)
1313
$conn = $databaseConnector->getConnection();
1414
assert($conn != NULL);
1515
$identityProvidersTableName = $databaseConnector->getIdentityProvidersTableName();
16+
$identityProvidersMapTableName = $databaseConnector->getIdentityProvidersMapTableName();
1617
$serviceProvidersTableName = $databaseConnector->getServiceProvidersTableName();
17-
$sourceIdp = $request['Attributes']['sourceIdPName'][0];
18-
$service = $request['Destination']['name']['en'];
18+
$serviceProvidersMapTableName = $databaseConnector->getServiceProvidersMapTableName();
19+
$idpEntityID = $request['saml:sp:IdP'];
20+
$idpName = $request['Attributes']['sourceIdPName'][0];
21+
$spEntityId = $request['Destination']['entityid'];
22+
$spName = $request['Destination']['name']['en'];
1923
$year = $date->format('Y');
2024
$month = $date->format('m');
2125
$day = $date->format('d');
2226

23-
$stmt = $conn->prepare("INSERT INTO ".$identityProvidersTableName."(year, month, day, sourceIdp, count) VALUES (?, ?, ?, ?, '1') ON DUPLICATE KEY UPDATE count = count + 1");
24-
$stmt->bind_param("iiis", $year, $month, $day, $sourceIdp);
25-
if ($stmt->execute() === FALSE) {
26-
SimpleSAML\Logger::error("The login log wasn't inserted into the database.");
27-
}
27+
if (is_null($idpEntityID) || empty($idpEntityID) || is_null($spEntityId) || empty($spEntityId)) {
28+
SimpleSAML\Logger::error("Some from attribute: 'idpEntityId', 'idpName', 'spEntityId' and 'spName' is null or empty and login log wasn't inserted into the database.");
29+
} else {
30+
$stmt = $conn->prepare("INSERT INTO ".$identityProvidersTableName."(year, month, day, sourceIdp, count) VALUES (?, ?, ?, ?, '1') ON DUPLICATE KEY UPDATE count = count + 1");
31+
$stmt->bind_param("iiis", $year, $month, $day, $idpEntityID);
32+
if ($stmt->execute() === FALSE) {
33+
SimpleSAML\Logger::error("The login log wasn't inserted into table: " . $identityProvidersTableName . ".");
34+
}
35+
36+
$stmt = $conn->prepare("INSERT INTO ".$serviceProvidersTableName."(year, month, day, service, count) VALUES (?, ?, ?, ?, '1') ON DUPLICATE KEY UPDATE count = count + 1");
37+
$stmt->bind_param("iiis", $year, $month, $day, $spEntityId);
38+
if ($stmt->execute() === FALSE) {
39+
SimpleSAML\Logger::error("The login log wasn't inserted into into table: " . $serviceProvidersTableName . ".");
40+
}
41+
42+
if (is_null($idpName) || empty($idpName)) {
43+
$stmt->prepare("INSERT INTO " . $identityProvidersMapTableName . "(entityId, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = ?");
44+
$stmt->bind_param("sss", $idpEntityID, $idpName, $idpName);
45+
$stmt->execute();
46+
}
2847

29-
$stmt = $conn->prepare("INSERT INTO ".$serviceProvidersTableName."(year, month, day, service, count) VALUES (?, ?, ?, ?, '1') ON DUPLICATE KEY UPDATE count = count + 1");
30-
$stmt->bind_param("iiis", $year, $month, $day, $service);
31-
if ($stmt->execute() === FALSE) {
32-
SimpleSAML\Logger::error("The login log wasn't inserted into the database.");
48+
if (is_null($spName) || empty($spName)) {
49+
$stmt->prepare("INSERT INTO " . $serviceProvidersMapTableName . "(identifier, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = ?");
50+
$stmt->bind_param("sss", $spEntityId, $spName, $spName);
51+
$stmt->execute();
52+
}
3353
}
3454

55+
SimpleSAML\Logger::error("The login log was successfully stored in database");
56+
3557
$conn->close();
3658
}
3759

@@ -56,12 +78,13 @@ public static function getLoginCountPerDeyPerService()
5678
$databaseConnector = new DatabaseConnector();
5779
$conn = $databaseConnector->getConnection();
5880
assert($conn != NULL);
59-
$table_name = $databaseConnector->getIdentityProvidersTableName();
60-
$stmt = $conn->prepare("SELECT year, month, sourceIdp, SUM(count) AS count FROM ".$table_name. " GROUP BY year, month, sourceIdp HAVING sourceIdp != '' ORDER BY year DESC, month DESC, count DESC");
81+
$identityProvidersTableName = $databaseConnector->getIdentityProvidersTableName();
82+
$identityProvidersMapTableName = $databaseConnector->getIdentityProvidersMapTableName();
83+
$stmt = $conn->prepare("SELECT year, month, IFNULL(name,sourceIdp) AS idPName, SUM(count) AS count FROM ".$identityProvidersTableName. " LEFT OUTER JOIN " . $identityProvidersMapTableName . " ON sourceIdp = entityId GROUP BY year, month, sourceIdp HAVING sourceIdp != '' ORDER BY year DESC, month DESC, count DESC");
6184
$stmt->execute();
6285
$result = $stmt->get_result();
6386
while($row = $result->fetch_assoc()) {
64-
echo "[new Date(".$row["year"].",".($row["month"] - 1 )."),'".$row["sourceIdp"]."', {v:".$row["count"]."}],";
87+
echo "[new Date(".$row["year"].",".($row["month"] - 1 )."),'".$row["idPName"]."', {v:".$row["count"]."}],";
6588
}
6689
$conn->close();
6790
}
@@ -71,12 +94,13 @@ public static function getAccessToServicesPerMonth()
7194
$databaseConnector = new DatabaseConnector();
7295
$conn = $databaseConnector->getConnection();
7396
assert($conn != NULL);
74-
$table_name = $databaseConnector->getServiceProvidersTableName();
75-
$stmt = $conn->prepare("SELECT year, month, service, SUM(count) AS count FROM ".$table_name." GROUP BY year DESC, month DESC, service HAVING service != '' ORDER BY year DESC, month DESC, count DESC");
97+
$serviceProvidersTableName = $databaseConnector->getServiceProvidersTableName();
98+
$serviceProvidersMapTableName = $databaseConnector->getServiceProvidersMapTableName();
99+
$stmt = $conn->prepare("SELECT year, month, IFNULL(name,service) AS spName, SUM(count) AS count FROM ".$serviceProvidersTableName." LEFT OUTER JOIN " . $serviceProvidersMapTableName . " ON service = identifier GROUP BY year DESC, month DESC, service HAVING service != '' ORDER BY year DESC, month DESC, count DESC");
76100
$stmt->execute();
77101
$result = $stmt->get_result();
78102
while($row = $result->fetch_assoc()) {
79-
echo "[new Date(".$row["year"].",".($row["month"] - 1 )."),'".$row["service"]."', {v:".$row["count"]."}],"; }
103+
echo "[new Date(".$row["year"].",".($row["month"] - 1 )."),'".$row["spName"]."', {v:".$row["count"]."}],"; }
80104
$conn->close();
81105
}
82106

@@ -128,12 +152,13 @@ public static function getAccessCountPerService()
128152
$databaseConnector = new DatabaseConnector();
129153
$conn = $databaseConnector->getConnection();
130154
assert($conn != NULL);
131-
$table_name = $databaseConnector->getServiceProvidersTableName();
132-
$stmt = $conn->prepare("SELECT service, SUM(count) AS count FROM ".$table_name." GROUP BY service HAVING service != ''");
155+
$serviceProvidersTableName = $databaseConnector->getServiceProvidersTableName();
156+
$serviceProvidersMapTableName = $databaseConnector->getServiceProvidersMapTableName();
157+
$stmt = $conn->prepare("SELECT IFNULL(name,service) AS spName, SUM(count) AS count FROM ".$serviceProvidersTableName." LEFT OUTER JOIN " . $serviceProvidersMapTableName . " ON service = identifier GROUP BY service HAVING service != ''");
133158
$stmt->execute();
134159
$result = $stmt->get_result();
135160
while($row = $result->fetch_assoc()) {
136-
echo "['".$row["service"]."', ".$row["count"]."],";
161+
echo "['".$row["spName"]."', ".$row["count"]."],";
137162
}
138163
$conn->close();
139164
}
@@ -143,12 +168,13 @@ public static function getLoginCountPerIdp()
143168
$databaseConnector = new DatabaseConnector();
144169
$conn = $databaseConnector->getConnection();
145170
assert($conn != NULL);
146-
$table_name = $databaseConnector->getIdentityProvidersTableName();
147-
$stmt = $conn->prepare("SELECT sourceIdp, SUM(count) AS count FROM ".$table_name." GROUP BY sourceIdp HAVING sourceIdp != ''");
171+
$identityProvidersTableName = $databaseConnector->getIdentityProvidersTableName();
172+
$identityProvidersMapTableName = $databaseConnector->getIdentityProvidersMapTableName();
173+
$stmt = $conn->prepare("SELECT IFNULL(name,sourceIdp) AS idPName, SUM(count) AS count FROM ".$identityProvidersTableName. " LEFT OUTER JOIN " . $identityProvidersMapTableName . " ON sourceIdp = entityId GROUP BY sourceIdp HAVING sourceIdp != ''");
148174
$stmt->execute();
149175
$result = $stmt->get_result();
150176
while($row = $result->fetch_assoc()) {
151-
echo "['".$row["sourceIdp"]."', ".$row["count"]."],";
177+
echo "['".$row["idPName"]."', ".$row["count"]."],";
152178
}
153179
$conn->close();
154180
}

lib/Auth/Process/DatabaseConnector.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class databaseConnector
1111
private $password;
1212
private $databaseName;
1313
private $identityProvidersTableName;
14+
private $identityProvidersMapTableName;
1415
private $serviceProvidersTableName;
16+
private $serviceProvidersMapTableName;
1517
private $encryption;
1618
private $sslCA;
1719
private $sslCert;
@@ -25,7 +27,9 @@ class databaseConnector
2527
const PASSWORD = 'password';
2628
const DATABASE = 'databaseName';
2729
const IDP_TABLE_NAME = 'identityProvidersTableName';
30+
const IDP_MAP_TABLE_NAME = 'identityProvidersMapTableName';
2831
const SP_TABLE_NAME = 'serviceProvidersTableName' ;
32+
const SP_MAP_TABLE_NAME = 'serviceProvidersMapTableName';
2933
const ENCRYPTION = 'encryption';
3034
const SSL_CA = 'ssl_ca';
3135
const SSL_CERT = 'ssl_cert_path';
@@ -43,7 +47,9 @@ public function __construct ()
4347
$this->password = $conf->getString(self::PASSWORD);
4448
$this->databaseName = $conf->getString(self::DATABASE);
4549
$this->identityProvidersTableName = $conf->getString(self::IDP_TABLE_NAME);
50+
$this->identityProvidersMapTableName = $conf->getString(self::IDP_MAP_TABLE_NAME);
4651
$this->serviceProvidersTableName = $conf->getString(self::SP_TABLE_NAME);
52+
$this->serviceProvidersMapTableName = $conf->getString(self::SP_MAP_TABLE_NAME);
4753
$this->encryption = $conf->getBoolean(self::ENCRYPTION);
4854
$this->sslCA = $conf->getString(self::SSL_CA);
4955
$this->sslCert = $conf->getString(self::SSL_CERT);
@@ -80,11 +86,20 @@ public function getIdentityProvidersTableName()
8086

8187
}
8288

89+
public function getIdentityProvidersMapTableName()
90+
{
91+
return $this->identityProvidersMapTableName;
92+
}
93+
8394
public function getServiceProvidersTableName()
8495
{
8596
return $this->serviceProvidersTableName;
8697

8798
}
8899

100+
public function getServiceProvidersMapTableName()
101+
{
102+
return $this->serviceProvidersMapTableName;
103+
}
89104

90105
}

0 commit comments

Comments
 (0)