Completed
Push — master ( 920728...fe9abf )
by Tim
27s queued 12s
created

ExpiryWarning::setAuthState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\authX509\Controller;
4
5
use SimpleSAML\Auth;
6
use SimpleSAML\Configuration;
7
use SimpleSAML\Error;
8
use SimpleSAML\HTTP\RunnableResponse;
9
use SimpleSAML\Logger;
10
use SimpleSAML\Module;
11
use SimpleSAML\Module\authX509\Auth\Source\X509userCert;
12
use SimpleSAML\Session;
13
use SimpleSAML\XHTML\Template;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * Controller class for the authx509 module.
19
 *
20
 * This class serves the different views available in the module.
21
 *
22
 * @package simplesamlphp/simplesamlphp-module-authx509
23
 */
24
class ExpiryWarning
25
{
26
    /** @var \SimpleSAML\Configuration */
27
    protected $config;
28
29
    /** @var \SimpleSAML\Logger */
30
    protected $logger;
31
32
    /** @var \SimpleSAML\Session */
33
    protected $session;
34
35
    /**
36
     * @var \SimpleSAML\Auth\State|string
37
     * @psalm-var \SimpleSAML\Auth\State|class-string
38
     */
39
    protected $authState = Auth\State::class;
40
41
42
    /**
43
     * Controller constructor.
44
     *
45
     * It initializes the global configuration and session for the controllers implemented here.
46
     *
47
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
48
     * @param \SimpleSAML\Session $session The session to use by the controllers.
49
     *
50
     * @throws \Exception
51
     */
52
    public function __construct(
53
        Configuration $config,
54
        Session $session
55
    ) {
56
        $this->config = $config;
57
        $this->session = $session;
58
    }
59
60
61
    /**
62
     * Inject the \SimpleSAML\Logger dependency.
63
     *
64
     * @param \SimpleSAML\Logger $logger
65
     * @return void
66
     */
67
    public function setLogger(Logger $logger): void
68
    {
69
        $this->logger = $logger;
70
    }
71
72
73
    /**
74
     * Inject the \SimpleSAML\Auth\State dependency.
75
     *
76
     * @param \SimpleSAML\Auth\State $authState
77
     * @return void
78
     */
79
    public function setAuthState(Auth\State $authState): void
80
    {
81
        $this->authState = $authState;
82
    }
83
84
85
    /**
86
     * Show expiry warning.
87
     *
88
     * @param \Symfony\Component\HttpFoundation\Request $request
89
     * @return \Symfony\Component\HttpFoundation\Response
90
     * @throws \Exception
91
     */
92
    public function main(Request $request): Response
93
    {
94
        $this->logger::info('AuthX509 - Showing expiry warning to user');
95
96
        $id = $request->get('StateId', null);
97
        if ($id === null) {
98
            throw new Error\BadRequest('Missing required StateId query parameter.');
99
        }
100
101
        $state = $this->authState::loadState($id, 'warning:expire');
102
103
        if (is_null($state)) {
104
            throw new Error\NoState();
105
        } elseif ($request->get('proceed', null) !== null) {
106
            // The user has pressed the proceed-button
107
            return new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]);
108
        }
109
110
        $t = new Template($this->config, 'authX509:X509warning.twig');
111
        $t->data['target'] = Module::getModuleURL('authX509/expirywarning.php');
112
        $t->data['data'] = ['StateId' => $id];
113
        $t->data['daysleft'] = $state['daysleft'];
114
        $t->data['renewurl'] = $state['renewurl'];
115
        $t->data['errorcodes'] = Error\ErrorCodes::getAllErrorCodeMessages();
116
        return $t;
117
    }
118
}
119