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|string |
28
|
|
|
* @psalm-var \SimpleSAML\Auth\State|class-string |
29
|
|
|
*/ |
30
|
|
|
protected $authState = Auth\State::class; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \SimpleSAML\Module\yubikey\Auth\Process\OTP|string |
34
|
|
|
* @psalm-var \SimpleSAML\Module\yubikey\Auth\Process\OTP|class-string |
35
|
|
|
*/ |
36
|
|
|
protected $otp = OTP::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
|
|
|
protected Configuration $config, |
51
|
|
|
protected Session $session, |
52
|
|
|
) { |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Inject the \SimpleSAML\Auth\State dependency. |
58
|
|
|
* |
59
|
|
|
* @param \SimpleSAML\Auth\State $authState |
60
|
|
|
*/ |
61
|
|
|
public function setAuthState(Auth\State $authState): void |
62
|
|
|
{ |
63
|
|
|
$this->authState = $authState; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Inject the \SimpleSAML\Module\yubikey\Auth\Process\OTP dependency. |
69
|
|
|
* |
70
|
|
|
* @param \SimpleSAML\Module\yubikey\Auth\Process\OTP $otp |
71
|
|
|
*/ |
72
|
|
|
public function setOtp(OTP $otp): void |
73
|
|
|
{ |
74
|
|
|
$this->otp = $otp; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* This page asks the user to authenticate using a Yubikey. |
80
|
|
|
* |
81
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request The current request. |
82
|
|
|
* @return \SimpleSAML\XHTML\Template|\SimpleSAML\HTTP\RunnableResponse |
83
|
|
|
*/ |
84
|
|
|
public function main(Request $request) |
85
|
|
|
{ |
86
|
|
|
$stateId = $request->query->get('AuthState'); |
87
|
|
|
if ($stateId === null) { |
88
|
|
|
throw new Error\BadRequest('Missing AuthState parameter.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$state = $this->authState::loadState($stateId, 'yubikey:otp:init'); |
92
|
|
|
|
93
|
|
|
$error = false; |
94
|
|
|
|
95
|
|
|
$otp = $request->request->get('otp'); |
96
|
|
|
if ($otp !== null) { |
97
|
|
|
// we were given an OTP |
98
|
|
|
try { |
99
|
|
|
if ($this->otp::authenticate($state, $otp)) { |
|
|
|
|
100
|
|
|
$this->authState::saveState($state, 'yubikey:otp:init'); |
101
|
|
|
return new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]); |
102
|
|
|
} else { |
103
|
|
|
$error = 'The YubiKey used is invalid. Make sure to use the YubiKey associated with your account.'; |
104
|
|
|
} |
105
|
|
|
} catch (InvalidArgumentException $e) { |
106
|
|
|
$error = $e->getMessage(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$t = new Template($this->config, 'yubikey:otp.twig'); |
111
|
|
|
$t->data['AuthState'] = $stateId; |
112
|
|
|
$t->data['error'] = $error ?: false; |
113
|
|
|
$t->data['autofocus'] = 'otp'; |
114
|
|
|
|
115
|
|
|
return $t; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|