Passed
Pull Request — master (#5)
by Tim
02:14
created

Yubikey::main()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 33
rs 8.9777
cc 6
nc 12
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\yubikey\Controller;
6
7
use InvalidArgumentException;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Error;
11
use SimpleSAML\HTTP\RunnableResponse;
12
use SimpleSAML\Module\yubikey\Auth\Process\OTP;
13
use SimpleSAML\Session;
14
use SimpleSAML\XHTML\Template;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * Controller class for the yubikey module.
19
 *
20
 * This class serves the different views available in the module.
21
 *
22
 * @package simplesamlphp/simplesamlphp-module-yubikey
23
 */
24
class Yubikey
25
{
26
    /** @var \SimpleSAML\Configuration */
27
    protected Configuration $config;
28
29
    /** @var \SimpleSAML\Session */
30
    protected Session $session;
31
32
    /**
33
     * @var \SimpleSAML\Auth\State|string
34
     * @psalm-var \SimpleSAML\Auth\State|class-string
35
     */
36
    protected $authState = Auth\State::class;
37
38
39
    /**
40
     * Controller constructor.
41
     *
42
     * It initializes the global configuration and session for the controllers implemented here.
43
     *
44
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
45
     * @param \SimpleSAML\Session $session The session to use by the controllers.
46
     *
47
     * @throws \Exception
48
     */
49
    public function __construct(
50
        Configuration $config,
51
        Session $session
52
    ) {
53
        $this->config = $config;
54
        $this->session = $session;
55
    }
56
57
58
    /**
59
     * Inject the \SimpleSAML\Auth\State dependency.
60
     *
61
     * @param \SimpleSAML\Auth\State $authState
62
     */
63
    public function setAuthState(Auth\State $authState): void
64
    {
65
        $this->authState = $authState;
66
    }
67
68
69
    /**
70
     * This page asks the user to authenticate using a Yubikey.
71
     *
72
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
73
     * @return \SimpleSAML\XHTML\Template|\SimpleSAML\HTTP\RunnableResponse
74
     */
75
    public function main(Request $request): Template
76
    {
77
        $stateId = $request->get('StateId');
78
        if ($stateId === null) {
79
            throw new Error\BadRequest('Missing AuthState parameter.');
80
        }
81
82
        /** @var array $state */
83
        $state = $this->authState::loadState($stateId, 'yubikey:otp:init');
84
85
        $error = false;
86
87
        $otp = $request->get('otp');
88
        if ($otp !== null) {
89
            // we were given an OTP
90
            try {
91
                if (OTP::authenticate($state, $otp)) {
92
                    $this->authState::saveState($state, 'yubikey:otp:init');
93
                    return new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new SimpleSAML\HT...ssing'), array($state)) returns the type SimpleSAML\HTTP\RunnableResponse which is incompatible with the type-hinted return SimpleSAML\XHTML\Template.
Loading history...
94
                } else {
95
                    $error = 'The YubiKey used is invalid. Make sure to use the YubiKey associated with your account.';
96
                }
97
            } catch (InvalidArgumentException $e) {
98
                $error = $e->getMessage();
99
            }
100
        }
101
102
        $t = new Template($this->config, 'yubikey:otp.twig');
103
        $t->data['params'] = ['StateId' => $stateId];
104
        $t->data['error'] = $error || false;
105
        $t->data['autofocus'] = 'otp';
106
107
        return $t;
108
    }
109
}
110