Yubikey   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 73
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A main() 0 32 6
A setYubikey() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\authYubiKey\Controller;
6
7
use SimpleSAML\Configuration;
8
use SimpleSAML\Error;
9
use SimpleSAML\Module\authYubiKey\Auth\Source;
10
use SimpleSAML\Session;
11
use SimpleSAML\XHTML\Template;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Controller class for the authyubikey module.
16
 *
17
 * This class serves the different views available in the module.
18
 *
19
 * @package simplesamlphp/simplesamlphp-module-authyubikey
20
 */
21
class Yubikey
22
{
23
    /**
24
     * @var \SimpleSAML\Module\authYubiKey\Auth\Source\YubiKey|string
25
     * @psalm-var \SimpleSAML\Module\authYubiKey\Auth\Source\YubiKey|class-string
26
     */
27
    protected $yubikey = Source\YubiKey::class;
28
29
30
    /**
31
     * Controller constructor.
32
     *
33
     * It initializes the global configuration and session for the controllers implemented here.
34
     *
35
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
36
     * @param \SimpleSAML\Session $session The session to use by the controllers.
37
     *
38
     * @throws \Exception
39
     */
40
    public function __construct(
41
        protected Configuration $config,
42
        protected Session $session,
43
    ) {
44
    }
45
46
47
    /**
48
     * Inject the \SimpleSAML\Module\authYubiKey\Auth\Source\YubiKey dependency.
49
     *
50
     * @param \SimpleSAML\Module\authYubiKey\Auth\Source\YubiKey $yubikey
51
     */
52
    public function setYubikey(Source\YubiKey $yubikey): void
53
    {
54
        $this->yubikey = $yubikey;
55
    }
56
57
58
    /**
59
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
60
     * @return \SimpleSAML\XHTML\Template
61
     */
62
    public function main(Request $request): Template
63
    {
64
        if (!$request->query->has('AuthState')) {
65
            throw new Error\BadRequest('Missing AuthState parameter.');
66
        }
67
        $stateId = $request->query->all()['AuthState'];
68
69
        // attempt to log in
70
        $t = new Template($this->config, 'authYubiKey:yubikeylogin.twig');
71
72
        $errorCode = null;
73
        $otp = $request->request->has('otp') ? $request->request->all()['otp'] : null;
74
        if ($otp !== null) {
75
            // attempt to log in
76
77
            /** @psalm-var string $errorCode */
78
            $errorCode = $this->yubikey::handleLogin($stateId, $otp);
79
            $errorCodes = (new Error\ErrorCodes())->getAllMessages();
80
81
            if (array_key_exists($errorCode, $errorCodes['title'])) {
82
                $t->data['errorTitle'] = $errorCodes['title'][$errorCode];
83
            }
84
85
            if (array_key_exists($errorCode, $errorCodes['descr'])) {
86
                $t->data['errorDesc'] = $errorCodes['descr'][$errorCode];
87
            }
88
        }
89
90
        $t->data['errorCode'] = $errorCode;
91
        $t->data['stateParams'] = ['AuthState' => $stateId];
92
93
        return $t;
94
    }
95
}
96