YubikeyLoginHandler::validateToken()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 8
nop 3
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\YubiAuth\Handlers;
4
5
use Firesphere\BootstrapMFA\Authenticators\BootstrapMFAAuthenticator;
6
use Firesphere\BootstrapMFA\Handlers\BootstrapMFALoginHandler;
7
use Firesphere\YubiAuth\Forms\YubikeyForm;
8
use Firesphere\YubiAuth\Forms\YubikeyLoginForm;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Control\HTTPResponse;
11
use SilverStripe\Forms\Form;
12
use SilverStripe\ORM\ValidationException;
13
use SilverStripe\ORM\ValidationResult;
14
use SilverStripe\Security\LoginForm;
15
use SilverStripe\Security\Member;
16
use SilverStripe\Security\MemberAuthenticator\MemberLoginForm;
17
use SilverStripe\Security\PasswordEncryptor_NotFoundException;
18
use SilverStripe\Security\Security;
19
20
/**
21
 * Class YubikeyLoginHandler
22
 */
23
class YubikeyLoginHandler extends BootstrapMFALoginHandler
24
{
25
    /**
26
     * @var array
27
     */
28
    private static $url_handlers = [
0 ignored issues
show
introduced by
The private property $url_handlers is not used, and could be removed.
Loading history...
29
        'yubikey-authentication' => 'secondFactor',
30
        'verify'                 => 'secondFactor'
31
    ];
32
33
    /**
34
     * @var array
35
     */
36
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
37
        'LoginForm',
38
        'dologin',
39
        'secondFactor',
40
        'yubikeyForm',
41
        'verify'
42
    ];
43
44
    /**
45
     * Return the MemberLoginForm form
46
     */
47
    public function LoginForm()
48
    {
49
        return YubikeyLoginForm::create(
50
            $this,
51
            get_class($this->authenticator),
52
            'LoginForm'
53
        );
54
    }
55
56
    /**
57
     * @param array $data
58
     * @param LoginForm|MemberLoginForm $form
59
     * @param HTTPRequest $request
60
     * @return HTTPResponse
61
     */
62
    public function doLogin($data, MemberLoginForm $form, HTTPRequest $request)
63
    {
64
        if ($member = $this->checkLogin($data, $request, $result)) {
65
            $session = $request->getSession();
66
            $session->set(BootstrapMFAAuthenticator::SESSION_KEY . '.MemberID', $member->ID);
67
            $session->set(BootstrapMFAAuthenticator::SESSION_KEY . '.Data', $data);
68
            if (!empty($data['BackURL'])) {
69
                $session->set(BootstrapMFAAuthenticator::SESSION_KEY . '.BackURL', $data['BackURL']);
70
            }
71
72
            return $this->redirect($this->link('yubikey-authentication'));
73
        }
74
75
        return $this->redirectBack();
76
    }
77
78
    /**
79
     * @return array|Form[]
80
     */
81
    public function secondFactor()
82
    {
83
        return ['Form' => $this->yubikeyForm()];
84
    }
85
86
    /**
87
     * @return YubikeyForm
88
     */
89
    public function yubikeyForm()
90
    {
91
        return YubikeyForm::create($this, 'yubikeyForm');
92
    }
93
94
    /**
95
     * @return YubikeyForm
96
     */
97
    public function MFAForm()
98
    {
99
        return $this->yubikeyForm();
100
    }
101
102
    /**
103
     * @param array $data
104
     * @param YubikeyForm $form
105
     * @param HTTPRequest $request
106
     * @return HTTPResponse
107
     * @throws ValidationException
108
     * @throws PasswordEncryptor_NotFoundException
109
     */
110
    public function validateToken($data, $form, $request)
111
    {
112
        $validationResult = ValidationResult::create();
113
        $session = $request->getSession();
114
115
        $memberData = $session->get(BootstrapMFAAuthenticator::SESSION_KEY . '.Data');
116
        $this->request['BackURL'] = !empty($memberData['BackURL']) ? $memberData['BackURL'] : '';
117
118
        $member = $this->authenticator->validateToken($data, $request, $validationResult);
119
120
        if (!$member instanceof Member) {
121
            $data['token'] = $data['yubiauth'];
122
            $member = parent::validate($data, $form, $request, $validationResult);
123
        }
124
125
        if ($member instanceof Member) {
126
            $memberData = $session->get(BootstrapMFAAuthenticator::SESSION_KEY . '.Data');
127
            $this->performLogin($member, $memberData, $request);
128
            Security::setCurrentUser($member);
129
            $session->clear(BootstrapMFAAuthenticator::SESSION_KEY);
130
131
            return $this->redirectAfterSuccessfulLogin();
132
        }
133
134
        $form->setSessionValidationResult($validationResult);
135
136
        return $this->redirect($this->link());
137
    }
138
}
139