RecoveryTokenService   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 142
rs 10
c 0
b 0
f 0
wmc 30

19 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticateSafeStore() 0 3 1
A hasStepUpRequest() 0 3 1
A resetReturnTo() 0 3 1
A deleteStepUpRequest() 0 3 1
A startStepUpRequest() 0 3 1
A getRecoveryToken() 0 3 1
A getStepUpRequest() 0 3 1
A getRemainingTokenTypes() 0 14 3
A delete() 0 3 1
A resetStepUpGiven() 0 3 1
A getRecoveryTokensForIdentity() 0 3 1
A getAvailableTokens() 0 21 5
A excludeDisabledRecoveryTokens() 0 11 6
A __construct() 0 7 1
A returnTo() 0 3 1
A hasRecoveryToken() 0 3 1
A setReturnTo() 0 3 1
A wasStepUpGiven() 0 3 1
A stepUpGiven() 0 3 1
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\SelfAssertedTokens;
22
23
use Psr\Log\LoggerInterface;
24
use Surfnet\StepupMiddlewareClient\Identity\Dto\RecoveryToken;
25
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
26
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactor;
27
use Surfnet\StepupMiddlewareClientBundle\Identity\Service\RecoveryTokenService as MiddlewareRecoveryTokenService;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SafeStoreAuthenticationCommand;
29
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfAssertedTokens\Dto\ReturnTo;
30
31
readonly class RecoveryTokenService
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class RecoveryTokenService
Loading history...
32
{
33
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
34
        private MiddlewareRecoveryTokenService $recoveryTokenService,
35
        private SafeStoreService $safeStoreService,
36
        private RecoveryTokenState $stateStore,
37
        private RecoveryTokenConfig $config,
38
        private LoggerInterface $logger
39
    ) {
40
    }
41
42
    public function hasRecoveryToken(Identity $identity): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function hasRecoveryToken()
Loading history...
43
    {
44
        return $this->recoveryTokenService->hasRecoveryToken($identity);
45
    }
46
47
    public function getRecoveryToken(string $recoveryTokenId): RecoveryToken
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRecoveryToken()
Loading history...
48
    {
49
        return $this->recoveryTokenService->findOne($recoveryTokenId);
50
    }
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $identity should have a doc-comment as per coding-style.
Loading history...
53
     * @return RecoveryToken[]
54
     */
55
    public function getRecoveryTokensForIdentity(Identity $identity): array
56
    {
57
        return $this->recoveryTokenService->findAllFor($identity);
58
    }
59
60
    public function getRemainingTokenTypes(Identity $identity): array
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRemainingTokenTypes()
Loading history...
61
    {
62
        $tokens = $this->getRecoveryTokensForIdentity($identity);
63
        $tokenTypes = $this->excludeDisabledRecoveryTokens(
64
            $this->recoveryTokenService->getAvailableRecoveryTokenTypes()
65
        );
66
67
        /** @var RecoveryToken $token */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
68
        foreach ($tokens as $token) {
69
            if (in_array($token->type, $tokenTypes)) {
70
                unset($tokenTypes[$token->type]);
71
            }
72
        }
73
        return $tokenTypes;
74
    }
75
76
    public function delete(RecoveryToken $recoveryToken): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function delete()
Loading history...
77
    {
78
        $this->recoveryTokenService->delete($recoveryToken);
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Surfnet\StepupMiddleware...ce\RecoveryTokenService. ( Ignorable by Annotation )

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

78
        $this->recoveryTokenService->/** @scrutinizer ignore-call */ 
79
                                     delete($recoveryToken);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
    }
80
81
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $command should have a doc-comment as per coding-style.
Loading history...
82
     * Verify the password hash with the secret specified on the command.
83
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
84
    public function authenticateSafeStore(SafeStoreAuthenticationCommand $command): bool
85
    {
86
        return $this->safeStoreService->authenticate($command->secret, $command->recoveryToken->identifier);
87
    }
88
89
    public function getAvailableTokens(Identity $identity, VerifiedSecondFactor $secondFactor): array
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getAvailableTokens()
Loading history...
90
    {
91
        $tokens = $this->getRecoveryTokensForIdentity($identity);
92
        if ($secondFactor->type === 'sms' && array_key_exists('sms', $tokens)) {
93
            // Check if the phone number of the recovery token is the same as that of the second factor token
94
            $smsRecoveryToken = $tokens['sms'];
95
            if ($smsRecoveryToken->identifier === $secondFactor->secondFactorIdentifier) {
96
                $this->logger->info(
97
                    sprintf(
98
                        'Filtering the SMS recovery token from the available recovery tokens: [%s]. As the phone ' .
99
                        ' numbers are the same for both second factor and recovery tokens.',
100
                        implode(', ', array_keys($tokens))
101
                    )
102
                );
103
                unset($tokens['sms']);
104
            }
105
        }
106
        if ($tokens === []) {
107
            $this->logger->info('No recovery tokens are available for second factor registration');
108
        }
109
        return $tokens;
110
    }
111
112
    public function startStepUpRequest(string $requestId): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function startStepUpRequest()
Loading history...
113
    {
114
        $this->stateStore->startStepUpRequest($requestId);
115
    }
116
117
    public function hasStepUpRequest(): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function hasStepUpRequest()
Loading history...
118
    {
119
        return $this->stateStore->hasStepUpRequest();
120
    }
121
122
    public function getStepUpRequest(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getStepUpRequest()
Loading history...
123
    {
124
        return $this->stateStore->getStepUpRequest();
125
    }
126
127
    public function deleteStepUpRequest(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function deleteStepUpRequest()
Loading history...
128
    {
129
        $this->stateStore->deleteStepUpRequest();
130
    }
131
132
    public function wasStepUpGiven(): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function wasStepUpGiven()
Loading history...
133
    {
134
        return $this->stateStore->getStepUpGiven();
135
    }
136
137
    public function stepUpGiven(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function stepUpGiven()
Loading history...
138
    {
139
        $this->stateStore->setStepUpGiven(true);
140
    }
141
142
    public function setReturnTo(string $route, array $parameters = []): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setReturnTo()
Loading history...
143
    {
144
        $this->stateStore->setReturnTo($route, $parameters);
145
    }
146
147
    public function returnTo(): ReturnTo
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function returnTo()
Loading history...
148
    {
149
        return $this->stateStore->returnTo();
150
    }
151
152
    public function resetReturnTo() :void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resetReturnTo()
Loading history...
153
    {
154
        $this->stateStore->resetReturnTo();
155
    }
156
157
    public function resetStepUpGiven(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resetStepUpGiven()
Loading history...
158
    {
159
        $this->stateStore->resetStepUpGiven();
160
    }
161
162
    private function excludeDisabledRecoveryTokens(array $availableRecoveryTokenTypes): array
0 ignored issues
show
Coding Style introduced by
Private method name "RecoveryTokenService::excludeDisabledRecoveryTokens" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing doc comment for function excludeDisabledRecoveryTokens()
Loading history...
163
    {
164
        foreach ($availableRecoveryTokenTypes as $identifier => $token) {
165
            if ($token === 'sms' && $this->config->isSmsDisabled()) {
166
                unset($availableRecoveryTokenTypes[$identifier]);
167
            }
168
            if ($token === 'safe-store' && $this->config->isSafeStoreCodeDisabled()) {
169
                unset($availableRecoveryTokenTypes[$identifier]);
170
            }
171
        }
172
        return $availableRecoveryTokenTypes;
173
    }
174
}
175