ExpiryWarning::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 2
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\authX509\Auth\Process;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Logger;
10
use SimpleSAML\Module;
11
use SimpleSAML\Utils;
12
13
/**
14
 * Filter which shows a warning if the user's client certificate is about to expire.
15
 *
16
 ** <code>
17
 * // show about2xpire warning if client certificate is about to expire
18
 * 10 => array(
19
 *     'class' => 'authX509:ExpiryWarning',
20
 *     'warndaysbefore' => 30,
21
 * ),
22
 * </code>
23
 *
24
 * @package SimpleSAMLphp
25
 */
26
27
class ExpiryWarning extends Auth\ProcessingFilter
28
{
29
    /** @var int */
30
    private int $warndaysbefore = 30;
31
32
    /** @var string|null */
33
    private ?string $renewurl = null;
34
35
    /**
36
     * Initialize this filter.
37
     *
38
     * @param array $config  Configuration information about this filter.
39
     * @param mixed $reserved  For future use.
40
     */
41
    public function __construct(array $config, $reserved)
42
    {
43
        parent::__construct($config, $reserved);
44
45
        if (array_key_exists('warndaysbefore', $config)) {
46
            Assert::integerish(
47
                $config['warndaysbefore'],
48
                'Invalid value for \'warndaysbefore\'-option to authX509::ExpiryWarning filter.',
49
            );
50
            $this->warndaysbefore = (int)$config['warndaysbefore'];
51
        }
52
53
        if (array_key_exists('renewurl', $config)) {
54
            $this->renewurl = $config['renewurl'];
55
            Assert::string(
56
                $this->renewurl,
57
                'Invalid value for \'renewurl\'-option to authX509::ExpiryWarning filter.',
58
            );
59
        }
60
    }
61
62
    /**
63
     * Process an authentication response.
64
     *
65
     * This function saves the state, and if necessary redirects the user to the page where the user
66
     * is informed about the expiry date of his/her certificate.
67
     *
68
     * @param array $state  The state of the response.
69
     */
70
    public function process(array &$state): void
71
    {
72
        if (isset($state['isPassive']) && $state['isPassive'] === true) {
73
            // We have a passive request. Skip the warning
74
            return;
75
        }
76
77
        if (
78
            !isset($_SERVER['SSL_CLIENT_CERT']) ||
79
            ($_SERVER['SSL_CLIENT_CERT'] == '')
80
        ) {
81
            return;
82
        }
83
84
        $client_cert = $_SERVER['SSL_CLIENT_CERT'];
85
        $client_cert_data = openssl_x509_parse($client_cert);
86
        if ($client_cert_data == false) {
87
            Logger::error('authX509: invalid cert');
88
            return;
89
        }
90
        $validTo = $client_cert_data['validTo_time_t'];
91
        $now = time();
92
        $daysleft = (int) (($validTo - $now) / 86400); //24*60*60
93
        if ($daysleft > $this->warndaysbefore) {
94
            // We have a certificate that will be valid for some time. Skip the warning
95
            return;
96
        }
97
98
        Logger::warning('authX509: user certificate expires in ' . $daysleft . ' days');
99
        $state['daysleft'] = $daysleft;
100
        $state['renewurl'] = $this->renewurl;
101
102
        // Save state and redirect
103
        $id = Auth\State::saveState($state, 'warning:expire');
104
        $url = Module::getModuleURL('authX509/expirywarning.php');
105
        $httpUtils = new Utils\HTTP();
106
        $httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
107
    }
108
}
109