StatisticsWithAttribute   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setIdentifier() 0 10 3
A __construct() 0 19 4
B process() 0 28 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\statistics\Auth\Process;
6
7
use SimpleSAML\{Auth, Logger};
8
use SimpleSAML\Assert\Assert;
9
10
use function array_key_exists;
11
use function boolval;
12
use function is_null;
13
14
/**
15
 * Log a line in the STAT log with one attribute.
16
 *
17
 * @package SimpleSAMLphp
18
 */
19
class StatisticsWithAttribute extends Auth\ProcessingFilter
20
{
21
    /**
22
     * The attribute to log
23
     * @var string|null
24
     */
25
    private ?string $attribute = null;
26
27
    /**
28
     * @var string
29
     */
30
    private string $typeTag = 'saml20-idp-SSO';
31
32
    /**
33
     * @var bool
34
     */
35
    private bool $skipPassive = false;
36
37
38
    /**
39
     * Initialize this filter.
40
     *
41
     * @param array &$config  Configuration information about this filter.
42
     * @param mixed $reserved  For future use.
43
     */
44
    public function __construct(array &$config, $reserved)
45
    {
46
        parent::__construct($config, $reserved);
47
48
        if (array_key_exists('attributename', $config)) {
49
            Assert::stringNotEmpty(
50
                $config['attributename'],
51
                'Invalid attribute name given to core:StatisticsWithAttribute filter.',
52
            );
53
            $this->attribute = $config['attributename'];
54
        }
55
56
        if (array_key_exists('type', $config)) {
57
            Assert::stringNotEmpty($config['type'], 'Invalid typeTag given to core:StatisticsWithAttribute filter.');
58
            $this->typeTag = $config['type'];
59
        }
60
61
        if (array_key_exists('skipPassive', $config)) {
62
            $this->skipPassive = boolval($config['skipPassive']);
63
        }
64
    }
65
66
67
    /**
68
     * Log line.
69
     *
70
     * @param array &$state  The current state.
71
     */
72
    public function process(array &$state): void
73
    {
74
        Assert::keyExists($state, 'Attributes');
75
76
        $logAttribute = 'NA';
77
        $isPassive = '';
78
79
        if (array_key_exists('isPassive', $state) && $state['isPassive'] === true) {
80
            if ($this->skipPassive === true) {
81
                // We have a passive request. Skip logging statistics
82
                return;
83
            }
84
            $isPassive = 'passive-';
85
        }
86
87
        if (!is_null($this->attribute) && array_key_exists($this->attribute, $state['Attributes'])) {
88
            $logAttribute = $state['Attributes'][$this->attribute][0];
89
        }
90
91
        $source = $this->setIdentifier('Source', $state);
92
        $dest = $this->setIdentifier('Destination', $state);
93
94
        if (!array_key_exists('PreviousSSOTimestamp', $state)) {
95
            // The user hasn't authenticated with this SP earlier in this session
96
            Logger::stats($isPassive . $this->typeTag . '-first ' . $dest . ' ' . $source . ' ' . $logAttribute);
97
        }
98
99
        Logger::stats($isPassive . $this->typeTag . ' ' . $dest . ' ' . $source . ' ' . $logAttribute);
100
    }
101
102
103
    /**
104
     * @param string &$direction  Either 'Source' or 'Destination'.
105
     * @param array $state  The current state.
106
     *
107
     * @return string
108
     */
109
    private function setIdentifier(string $direction, array $state): string
110
    {
111
        if (array_key_exists($direction, $state)) {
112
            if (isset($state[$direction]['core:statistics-id'])) {
113
                return $state[$direction]['core:statistics-id'];
114
            } else {
115
                return $state[$direction]['entityid'];
116
            }
117
        }
118
        return 'NA';
119
    }
120
}
121