Passed
Pull Request — master (#2)
by Tim
02:52
created

Yubikey::main()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 31
rs 9.4222
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
    /** @var \SimpleSAML\Configuration */
24
    protected Configuration $config;
25
26
    /** @var \SimpleSAML\Session */
27
    protected Session $session;
28
29
    /**
30
     * @var \SimpleSAML\Module\authYubikey\Auth\Source\YubiKey|string
31
     * @psalm-var \SimpleSAML\Module\authYubikey\Auth\Source\YubiKey|class-string
32
     */
33
    protected $yubikey = Source\YubiKey::class;
34
35
36
    /**
37
     * Controller constructor.
38
     *
39
     * It initializes the global configuration and session for the controllers implemented here.
40
     *
41
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
42
     * @param \SimpleSAML\Session $session The session to use by the controllers.
43
     *
44
     * @throws \Exception
45
     */
46
    public function __construct(
47
        Configuration $config,
48
        Session $session
49
    ) {
50
        $this->config = $config;
51
        $this->session = $session;
52
    }
53
54
55
    /**
56
     * Inject the \SimpleSAML\Module\authYubikey\Auth\Source\YubiKey dependency.
57
     *
58
     * @param \SimpleSAML\Module\authYubikey\Auth\Source\YubiKey $yubikey
59
     */
60
    public function setYubikey(Source\Yubikey $yubikey): void
61
    {
62
        $this->yubikey = $yubikey;
63
    }
64
65
66
    /**
67
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
68
     * @return \SimpleSAML\XHTML\Template
69
     */
70
    public function main(Request $request): Template
71
    {
72
        $stateId = $request->get('AuthState');
73
        if ($stateId === null) {
74
            throw new Error\BadRequest('Missing AuthState parameter.');
75
        }
76
77
        $t = new Template($this->config, 'authYubikey:yubikeylogin.twig');
78
79
        $errorCode = null;
80
        $otp = $request->get('otp');
81
        if ($otp !== null) {
82
            // attempt to log in
83
84
            /** @psalm-var string $errorCode */
85
            $errorCode = $this->yubikey::handleLogin($stateId, $otp);
86
            $errorCodes = Error\ErrorCodes::getAllErrorCodeMessages();
87
88
            if (array_key_exists($errorCode, $errorCodes['title'])) {
89
                $t->data['errorTitle'] = $errorCodes['title'][$errorCode];
90
            }
91
92
            if (array_key_exists($errorCode, $errorCodes['descr'])) {
93
                $t->data['errorDesc'] = $errorCodes['descr'][$errorCode];
94
            }
95
        }
96
97
        $t->data['errorCode'] = $errorCode;
98
        $t->data['stateParams'] = ['AuthState' => $stateId];
99
100
        return $t;
101
    }
102
}
103