Passed
Pull Request — master (#106)
by Guy
01:55
created

completeRegistrationRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 28
c 0
b 0
f 0
rs 9.8333
cc 4
nc 4
nop 3
1
<?php
2
3
namespace SilverStripe\MFA\RequestHandler;
4
5
use Exception;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Control\HTTPResponse;
8
use SilverStripe\MFA\Method\MethodInterface;
9
use SilverStripe\MFA\Service\RegisteredMethodManager;
10
use SilverStripe\MFA\State\Result;
11
use SilverStripe\MFA\Store\StoreInterface;
12
use SilverStripe\ORM\ValidationResult;
13
14
trait RegistrationHandlerTrait
15
{
16
    public function createStartRegistrationResponse(StoreInterface $store, MethodInterface $method): HTTPResponse
17
    {
18
        $member = $store->getMember();
19
20
        // Sanity check that the method hasn't already been registered
21
        $existingRegisteredMethod = RegisteredMethodManager::singleton()->getFromMember($member, $method);
22
23
        $response = HTTPResponse::create()
24
            ->addHeader('Content-Type', 'application/json');
25
26
        if ($existingRegisteredMethod) {
27
            return $response->setBody(json_encode(['errors' => [_t(
28
                __CLASS__ . '.METHOD_ALREADY_REGISTERED', 'That method has already been registered against this Member'
29
            )]]))->setStatusCode(400);
30
        }
31
32
        // Mark the given method as started within the session
33
        $store->setMethod($method->getURLSegment());
34
        // Allow the registration handler to begin the process and generate some data to pass through to the front-end
35
        $data = $method->getRegisterHandler()->start($store);
36
37
        return $response->setBody(json_encode($data));
38
    }
39
40
    public function completeRegistrationRequest(
41
        StoreInterface $store,
42
        MethodInterface $method,
43
        HTTPRequest $request
44
    ): Result {
45
        $storedMethodName = $store->getMethod();
46
47
        // If a registration process hasn't been initiated in a previous request, calling this method is invalid
48
        if (!$storedMethodName) {
49
            return Result::create(false, _t(__CLASS__ . '.NO_REGISTRATION_IN_PROGRESS', 'No registration in progress'));
50
        }
51
52
        // Assert the method in progress matches the request for completion
53
        if ($storedMethodName !== $method->getURLSegment()) {
54
            return Result::create(_t(__CLASS__ . '.METHOD_MISMATCH', 'Method does not match registration in progress'));
0 ignored issues
show
Bug introduced by
_t(__CLASS__ . '.METHOD_...istration in progress') of type string is incompatible with the type boolean expected by parameter $success of SilverStripe\MFA\State\Result::create(). ( Ignorable by Annotation )

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

54
            return Result::create(/** @scrutinizer ignore-type */ _t(__CLASS__ . '.METHOD_MISMATCH', 'Method does not match registration in progress'));
Loading history...
55
        }
56
57
        $registrationHandler = $method->getRegisterHandler();
58
        $result = $registrationHandler->register($request, $store);
59
60
        if ($result->isSuccessful()) {
61
            RegisteredMethodManager::singleton()
62
                ->registerForMember($store->getMember(), $method, $result->getContext());
63
        } else {
64
            $this->extend('onRegisterMethodFailure', $store->getMember(), $method);
0 ignored issues
show
Bug introduced by
It seems like extend() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

64
            $this->/** @scrutinizer ignore-call */ 
65
                   extend('onRegisterMethodFailure', $store->getMember(), $method);
Loading history...
65
        }
66
67
        return $result;
68
    }
69
}
70