GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

U2fRegistrationChallenge::process()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 119
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 77
nc 24
nop 2
dl 0
loc 119
rs 7.8795
c 2
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LM\AuthAbstractor\Challenge;
6
7
use Firehed\U2F\ClientErrorException;
8
use LM\AuthAbstractor\Enum\Persistence\Operation;
9
use LM\AuthAbstractor\Factory\U2fRegistrationFactory;
10
use LM\AuthAbstractor\Model\AuthenticationProcess;
11
use LM\AuthAbstractor\Model\IAuthenticationProcess;
12
use LM\AuthAbstractor\Model\IU2fRegistration;
13
use LM\AuthAbstractor\Model\PersistOperation;
14
use LM\AuthAbstractor\Implementation\ChallengeResponse;
15
use LM\AuthAbstractor\Model\U2fRegistrationRequest;
16
use LM\AuthAbstractor\U2f\U2fRegistrationManager;
17
use Firehed\U2F\InvalidDataException;
18
use LM\Common\Enum\Scalar;
19
use LM\Common\Model\ArrayObject;
20
use LM\AuthAbstractor\Model\IChallengeResponse;
21
use LM\Common\Model\IntegerObject;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
26
use Symfony\Component\Form\FormError;
27
use Symfony\Component\Form\FormFactoryInterface;
28
use Twig_Environment;
29
30
/**
31
 * A challenge for asking the user to register a new U2F device.
32
 */
33
class U2fRegistrationChallenge implements IChallenge
34
{
35
    /** @var FormFactoryInterface */
36
    private $formFactory;
37
38
    /** @var HttpFoundationFactory */
39
    private $httpFoundationFactory;
40
41
    /** @var Twig_Environment */
42
    private $twig;
43
44
    /** @var U2fRegistrationManager */
45
    private $u2fRegistrationManager;
46
47
    /**
48
     * @internal
49
     */
50
    public function __construct(
51
        FormFactoryInterface $formFactory,
52
        HttpFoundationFactory $httpFoundationFactory,
53
        U2fRegistrationFactory $u2fRegistrationFactory,
54
        U2fRegistrationManager $u2fRegistrationManager,
55
        Twig_Environment $twig
56
    ) {
57
        $this->formFactory = $formFactory;
58
        $this->httpFoundationFactory = $httpFoundationFactory;
59
        $this->twig = $twig;
60
        $this->u2fRegistrationFactory = $u2fRegistrationFactory;
0 ignored issues
show
Bug Best Practice introduced by
The property u2fRegistrationFactory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
61
        $this->u2fRegistrationManager = $u2fRegistrationManager;
62
    }
63
64
    /**
65
     * @internal
66
     * @todo Maybe it should convert u2fRegistrations to ArrayObject, and then
67
     * U2fRegistrationManager would also take an ArrayObject as parameter.
68
     * @todo Handle invalid responses.
69
     * @todo security: An important part of the state of the application is
70
     * frozen during the authentication process. This includes the U2F owned by
71
     * the current user (if any), among other things. However, security
72
     * decisions are based on potentially obsolete frozen versions of
73
     * parameters.
74
     * @todo Rename u2fDeviceResponse to u2fTokenResponse
75
     */
76
    public function process(
77
        IAuthenticationProcess $process,
78
        ?ServerRequestInterface $httpRequest
79
    ): IChallengeResponse {
80
        $u2fRegistrations = $process
0 ignored issues
show
Deprecated Code introduced by
The function LM\AuthAbstractor\Model\...nProcess::getTypedMap() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

80
        $u2fRegistrations = /** @scrutinizer ignore-deprecated */ $process
Loading history...
81
            ->getTypedMap()
82
            ->get('u2f_registrations', Scalar::_ARRAY)
83
        ;
84
        $nU2fRegistrations = $process
0 ignored issues
show
Deprecated Code introduced by
The function LM\AuthAbstractor\Model\...nProcess::getTypedMap() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

84
        $nU2fRegistrations = /** @scrutinizer ignore-deprecated */ $process
Loading history...
85
            ->getTypedMap()
86
            ->get('n_u2f_registrations', IntegerObject::class)
87
            ->toInteger()
88
        ;
89
90
        $form = $this
91
            ->formFactory
92
            ->createBuilder()
93
            ->add('u2fDeviceResponse', HiddenType::class)
94
            ->getForm()
95
        ;
96
97
        if (null !== $httpRequest) {
98
            $form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest));
99
        }
100
101
        $typedMap = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $typedMap is dead and can be removed.
Loading history...
102
        if ($form->isSubmitted() && $form->isValid()) {
103
            try {
104
                $currentU2fRegistrationRequest = $process
0 ignored issues
show
Deprecated Code introduced by
The function LM\AuthAbstractor\Model\...nProcess::getTypedMap() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

104
                $currentU2fRegistrationRequest = /** @scrutinizer ignore-deprecated */ $process
Loading history...
105
                    ->getTypedMap()
106
                    ->get('current_u2f_registration_request', U2fRegistrationRequest::class)
107
                ;
108
                // ob_start(); // tmp
109
                // var_dump($form['u2fDeviceResponse']->getData());
110
                // file_put_contents('/var/www/html/tmp.txt', ob_get_clean(), FILE_APPEND);
111
                $u2fRegistration = $this
112
                    ->u2fRegistrationManager
113
                    ->getU2fRegistrationFromResponse(
114
                        $form['u2fDeviceResponse']->getData(),
115
                        $currentU2fRegistrationRequest->getRequest()
116
                    )
117
                ;
118
                // ob_start(); // tmp
119
                // var_dump($u2fRegistration);
120
                // file_put_contents('/var/www/html/tmp.txt', ob_get_clean(), FILE_APPEND);
121
122
                $u2fRegistrations[] = $u2fRegistration;
123
124
                $typedMap = $process
0 ignored issues
show
Deprecated Code introduced by
The function LM\AuthAbstractor\Model\...nProcess::getTypedMap() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

124
                $typedMap = /** @scrutinizer ignore-deprecated */ $process
Loading history...
125
                    ->getTypedMap()
126
                    ->set(
127
                        'persist_operations',
128
                        $process
0 ignored issues
show
Deprecated Code introduced by
The function LM\AuthAbstractor\Model\...nProcess::getTypedMap() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

128
                        /** @scrutinizer ignore-deprecated */ $process
Loading history...
129
                            ->getTypedMap()
130
                            ->get('persist_operations', ArrayObject::class)
131
                            ->add(
132
                                new PersistOperation($u2fRegistration, new Operation(Operation::CREATE)),
133
                                PersistOperation::class
134
                            ),
135
                        ArrayObject::class
136
                    )
137
                    ->set(
138
                        'n_u2f_registrations',
139
                        new IntegerObject($nU2fRegistrations + 1),
140
                        IntegerObject::class
141
                    )
142
                    ->set(
143
                        'u2f_registrations',
144
                        $u2fRegistrations,
145
                        Scalar::_ARRAY
146
                    )
147
                ;
148
                return new ChallengeResponse(
149
                    new AuthenticationProcess($typedMap),
150
                    null,
151
                    false,
152
                    true
153
                );
154
            } catch (ClientErrorException $e) {
155
                $form->addError(new FormError('You already used this U2F device'));
156
            } catch (InvalidDataException $e) {
157
                $form->addError(new FormError('The response is invalid.'));
158
            }
159
        }
160
161
        $u2fRegistrationRequest = $this
162
            ->u2fRegistrationManager
163
            ->generate(new ArrayObject(
0 ignored issues
show
Deprecated Code introduced by
The class LM\Common\Model\ArrayObject has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

163
            ->generate(/** @scrutinizer ignore-deprecated */ new ArrayObject(
Loading history...
164
                $u2fRegistrations,
0 ignored issues
show
Bug introduced by
$u2fRegistrations of type object is incompatible with the type array expected by parameter $items of LM\Common\Model\ArrayObject::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
                /** @scrutinizer ignore-type */ $u2fRegistrations,
Loading history...
165
                IU2fRegistration::class
166
            ))
167
        ;
168
        // ob_start(); // tmp
169
        // var_dump($u2fRegistrationRequest); // tmp
170
        // file_put_contents('/var/www/html/tmp.txt', ob_get_clean()); // tmp
171
172
        $httpResponse = new Response($this
173
            ->twig
174
            ->render('u2f_registration.html.twig', [
175
                'form' => $form->createView(),
176
                'nU2fRegistrations' => $nU2fRegistrations,
177
                'request_json' => $u2fRegistrationRequest->getRequestAsJson(),
178
                'sign_requests' => $u2fRegistrationRequest->getSignRequestsAsJson(),
179
            ]))
180
        ;
181
182
        return new ChallengeResponse(
183
            new AuthenticationProcess(
184
                $process
0 ignored issues
show
Deprecated Code introduced by
The function LM\AuthAbstractor\Model\...nProcess::getTypedMap() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

184
                /** @scrutinizer ignore-deprecated */ $process
Loading history...
185
                ->getTypedMap()
186
                ->add(
187
                    'current_u2f_registration_request',
188
                    $u2fRegistrationRequest,
189
                    U2fRegistrationRequest::class
190
                )
191
            ),
192
            $httpResponse,
193
            false,
194
            false
195
        )
196
        ;
197
    }
198
}
199