clearSmsVerificationState()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2014 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\Service;
22
23
use Surfnet\StepupBundle\Command\SendSmsChallengeCommand as StepupSendSmsChallengeCommand;
24
use Surfnet\StepupBundle\Command\VerifyPossessionOfPhoneCommand;
25
use Surfnet\StepupBundle\Service\Exception\TooManyChallengesRequestedException;
26
use Surfnet\StepupBundle\Service\SmsSecondFactorService as StepupSmsSecondFactorService;
27
use Surfnet\StepupBundle\Value\PhoneNumber\InternationalPhoneNumber;
28
use Surfnet\StepupBundle\Value\PhoneNumber\PhoneNumber;
29
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\ProvePhonePossessionCommand;
30
use Surfnet\StepupMiddlewareClientBundle\Uuid\Uuid;
31
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SendSmsChallengeCommand;
32
use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifySmsChallengeCommand;
33
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactor\ProofOfPossessionResult;
34
use Symfony\Contracts\Translation\TranslatorInterface;
35
36
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - Quite some commands and VOs are used here.
38
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
39
class SmsSecondFactorService implements SmsSecondFactorServiceInterface
40
{
41
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
42
        private readonly StepupSmsSecondFactorService $smsSecondFactorService,
43
        private readonly TranslatorInterface $translator,
44
        private readonly CommandService $commandService,
45
    ) {
46
    }
47
48
    public function getOtpRequestsRemainingCount(string $identifier): int
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getOtpRequestsRemainingCount()
Loading history...
49
    {
50
        return $this->smsSecondFactorService->getOtpRequestsRemainingCount($identifier);
51
    }
52
53
    public function getMaximumOtpRequestsCount(): int
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getMaximumOtpRequestsCount()
Loading history...
54
    {
55
        return $this->smsSecondFactorService->getMaximumOtpRequestsCount();
56
    }
57
58
    public function hasSmsVerificationState(string $secondFactorId): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function hasSmsVerificationState()
Loading history...
59
    {
60
        return $this->smsSecondFactorService->hasSmsVerificationState($secondFactorId);
61
    }
62
63
    public function clearSmsVerificationState(string $secondFactorId): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function clearSmsVerificationState()
Loading history...
64
    {
65
        $this->smsSecondFactorService->clearSmsVerificationState($secondFactorId);
66
    }
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $command should have a doc-comment as per coding-style.
Loading history...
69
     * @throws TooManyChallengesRequestedException
70
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
71
    public function sendChallenge(SendSmsChallengeCommand $command): bool
72
    {
73
        $phoneNumber = new InternationalPhoneNumber(
74
            $command->country->getCountryCode(),
75
            new PhoneNumber($command->subscriber)
76
        );
77
78
        $stepupCommand = new StepupSendSmsChallengeCommand();
79
        $stepupCommand->phoneNumber = $phoneNumber;
80
        $stepupCommand->body = $this->translator->trans('ss.registration.sms.challenge_body');
81
        $stepupCommand->identity = $command->identity;
82
        $stepupCommand->institution = $command->institution;
83
        $stepupCommand->secondFactorId = $command->secondFactorId;
84
85
        return $this->smsSecondFactorService->sendChallenge($stepupCommand);
86
    }
87
88
    public function provePossession(VerifySmsChallengeCommand $challengeCommand): ProofOfPossessionResult
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function provePossession()
Loading history...
89
    {
90
        $stepupCommand = new VerifyPossessionOfPhoneCommand();
91
        $stepupCommand->challenge = $challengeCommand->challenge;
92
        $stepupCommand->secondFactorId = $challengeCommand->secondFactorId;
93
94
        $verification = $this->smsSecondFactorService->verifyPossession($stepupCommand);
95
96
        if ($verification->didOtpExpire()) {
97
            return ProofOfPossessionResult::challengeExpired();
98
        } elseif ($verification->wasAttemptedTooManyTimes()) {
99
            return ProofOfPossessionResult::tooManyAttempts();
100
        } elseif (!$verification->wasSuccessful()) {
101
            return ProofOfPossessionResult::incorrectChallenge();
102
        }
103
104
        $command = new ProvePhonePossessionCommand();
105
        $command->identityId = $challengeCommand->identity;
106
        $command->secondFactorId = Uuid::generate();
107
        $command->phoneNumber = $verification->getPhoneNumber();
108
109
        $result = $this->commandService->execute($command);
110
111
        if (!$result->isSuccessful()) {
112
            return ProofOfPossessionResult::proofOfPossessionCommandFailed();
113
        }
114
115
        return ProofOfPossessionResult::secondFactorCreated($command->secondFactorId);
116
    }
117
}
118