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

Commit c5b5b41

Browse files
author
Dominik Frantisek Bucik
committed
fix: 🐛 Fix possible exceptions and refactor code
Refactored code to include more logging. Also fixed some possible exception places.
1 parent 7e001ac commit c5b5b41

File tree

4 files changed

+200
-100
lines changed

4 files changed

+200
-100
lines changed

lib/Auth/Process/Statistics.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
namespace SimpleSAML\Module\proxystatistics\Auth\Process;
66

77
use DateTime;
8+
use Exception;
89
use SimpleSAML\Auth\ProcessingFilter;
10+
use SimpleSAML\Logger;
911
use SimpleSAML\Module\proxystatistics\DatabaseCommand;
1012

1113
class Statistics extends ProcessingFilter
1214
{
15+
private const STAGE = 'proxystatistics:Statistics';
16+
17+
private const DEBUG_PREFIX = self::STAGE . ' - ';
18+
1319
public function __construct($config, $reserved)
1420
{
1521
parent::__construct($config, $reserved);
@@ -19,6 +25,12 @@ public function process(&$request)
1925
{
2026
$dateTime = new DateTime();
2127
$dbCmd = new DatabaseCommand();
22-
$dbCmd->insertLogin($request, $dateTime);
28+
try {
29+
$dbCmd->insertLogin($request, $dateTime);
30+
} catch (Exception $ex) {
31+
Logger::error(
32+
self::DEBUG_PREFIX . 'Caught exception while inserting login into statistics: ' . $ex->getMessage()
33+
);
34+
}
2335
}
2436
}

lib/Config.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Config
2020

2121
public const MODE_PROXY = 'PROXY';
2222

23+
private const KNOWN_MODES = ['PROXY', 'IDP', 'SP', 'MULTI_IDP'];
24+
2325
private const STORE = 'store';
2426

2527
private const MODE = 'mode';
@@ -40,22 +42,33 @@ class Config
4042

4143
private $sourceIdpEntityIdAttribute;
4244

45+
private $tables;
46+
47+
private $keepPerUser;
48+
49+
private $requiredAuthSource;
50+
51+
private $idAttribute;
52+
4353
private static $instance;
4454

4555
private function __construct()
4656
{
4757
$this->config = Configuration::getConfig(self::CONFIG_FILE_NAME);
4858
$this->store = $this->config->getConfigItem(self::STORE, null);
49-
$this->tables = $this->config->getArray('tables', []);
50-
$this->mode = $this->config->getValueValidate(self::MODE, ['PROXY', 'IDP', 'SP', 'MULTI_IDP'], 'PROXY');
59+
$this->tables = $this->config->getArray('tables', []);;
5160
$this->sourceIdpEntityIdAttribute = $this->config->getString(self::SOURCE_IDP_ENTITY_ID_ATTRIBUTE, '');
61+
$this->mode = $this->config->getValueValidate(self::MODE, self::KNOWN_MODES, self::MODE_PROXY);
62+
$this->keepPerUser = $this->config->getIntegerRange(self::KEEP_PER_USER, 31, 1827, 31);
63+
$this->requiredAuthSource = $this->config->getString(self::REQUIRE_AUTH_SOURCE, '');
64+
$this->idAttribute = $this->config->getString(self::USER_ID_ATTRIBUTE, 'uid');
5265
}
5366

5467
private function __clone()
5568
{
5669
}
5770

58-
public static function getInstance()
71+
public static function getInstance(): self
5972
{
6073
if (null === self::$instance) {
6174
self::$instance = new self();
@@ -81,17 +94,19 @@ public function getStore()
8194

8295
public function getIdAttribute()
8396
{
84-
return $this->config->getString(self::USER_ID_ATTRIBUTE, 'uid');
97+
return $this->idAttribute;
8598
}
8699

87100
public function getSourceIdpEntityIdAttribute()
88101
{
89102
return $this->sourceIdpEntityIdAttribute;
90103
}
91104

92-
public function getSideInfo($side)
105+
public function getSideInfo(string $side)
93106
{
94-
assert(in_array($side, [self::SIDES], true));
107+
if (!in_array($side, self::SIDES, true)) {
108+
throw new \Exception('Unrecognized side parameter value passed \'' . $side . '\'.');
109+
}
95110

96111
return array_merge([
97112
'name' => '',
@@ -101,11 +116,11 @@ public function getSideInfo($side)
101116

102117
public function getRequiredAuthSource()
103118
{
104-
return $this->config->getString(self::REQUIRE_AUTH_SOURCE, '');
119+
return$this->requiredAuthSource;
105120
}
106121

107122
public function getKeepPerUser()
108123
{
109-
return $this->config->getIntegerRange(self::KEEP_PER_USER, 31, 1827, 31);
124+
return $this->keepPerUser;
110125
}
111126
}

0 commit comments

Comments
 (0)