Yubikey::setOtp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
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
    /**
27
     * @var \SimpleSAML\Auth\State|class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment \SimpleSAML\Auth\State|class-string at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in \SimpleSAML\Auth\State|class-string.
Loading history...
28
     */
29
    protected $authState = Auth\State::class;
30
31
    /**
32
     * @var \SimpleSAML\Module\yubikey\Auth\Process\OTP|class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment \SimpleSAML\Module\yubik...rocess\OTP|class-string at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in \SimpleSAML\Module\yubikey\Auth\Process\OTP|class-string.
Loading history...
33
     */
34
    protected $otp = OTP::class;
35
36
37
    /**
38
     * Controller constructor.
39
     *
40
     * It initializes the global configuration and session for the controllers implemented here.
41
     *
42
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
43
     * @param \SimpleSAML\Session $session The session to use by the controllers.
44
     *
45
     * @throws \Exception
46
     */
47
    public function __construct(
48
        protected Configuration $config,
49
        protected Session $session,
50
    ) {
51
    }
52
53
54
    /**
55
     * Inject the \SimpleSAML\Auth\State dependency.
56
     *
57
     * @param \SimpleSAML\Auth\State $authState
58
     */
59
    public function setAuthState(Auth\State $authState): void
60
    {
61
        $this->authState = $authState;
62
    }
63
64
65
    /**
66
     * Inject the \SimpleSAML\Module\yubikey\Auth\Process\OTP dependency.
67
     *
68
     * @param \SimpleSAML\Module\yubikey\Auth\Process\OTP $otp
69
     */
70
    public function setOtp(OTP $otp): void
71
    {
72
        $this->otp = $otp;
73
    }
74
75
76
    /**
77
     * This page asks the user to authenticate using a Yubikey.
78
     *
79
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
80
     * @return \SimpleSAML\XHTML\Template|\SimpleSAML\HTTP\RunnableResponse
81
     */
82
    public function main(Request $request)
83
    {
84
        $stateId = $request->query->get('AuthState');
85
        if ($stateId === null) {
86
            throw new Error\BadRequest('Missing AuthState parameter.');
87
        }
88
89
        $state = $this->authState::loadState($stateId, 'yubikey:otp:init');
90
91
        $error = false;
92
93
        $otp = $request->request->get('otp');
94
        if ($otp !== null) {
95
            // we were given an OTP
96
            try {
97
                if ($this->otp::authenticate($state, $otp)) {
98
                    $this->authState::saveState($state, 'yubikey:otp:init');
99
                    return new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]);
100
                } else {
101
                    $error = 'The YubiKey used is invalid. Make sure to use the YubiKey associated with your account.';
102
                }
103
            } catch (InvalidArgumentException $e) {
104
                $error = $e->getMessage();
105
            }
106
        }
107
108
        $t = new Template($this->config, 'yubikey:otp.twig');
109
        $t->data['AuthState'] = $stateId;
110
        $t->data['error'] = $error ?: false;
111
        $t->data['autofocus'] = 'otp';
112
113
        return $t;
114
    }
115
}
116