Passed
Push — master ( c4afc2...9cde23 )
by Pieter van der
27:49 queued 12:42
created

SafeStoreService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 65
rs 10
c 1
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A produceSecret() 0 10 2
A promisePossession() 0 9 1
A authenticate() 0 3 1
A revokeRecoveryToken() 0 6 1
A forgetSafeStoreTokenCreatedDuringSecondFactorRegistration() 0 3 1
A __construct() 0 4 1
A wasSafeStoreTokenCreatedDuringSecondFactorRegistration() 0 3 1
1
<?php
2
3
/**
4
 * Copyright 2022 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfAssertedTokens;
20
21
use Surfnet\StepupMiddlewareClient\Service\ExecutionResult;
22
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\PromiseSafeStoreSecretTokenPossessionCommand;
23
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\RevokeOwnRecoveryTokenCommand;
24
use Surfnet\StepupMiddlewareClientBundle\Uuid\Uuid;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Command\PromiseSafeStorePossessionCommand;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Command\RevokeRecoveryTokenCommand;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Service\CommandService;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfAssertedTokens\Dto\SafeStoreSecret;
29
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfAssertedTokens\Exception\SafeStoreSecretNotFoundException;
30
31
class SafeStoreService
32
{
33
    /**
34
     * @var RecoveryTokenState
35
     */
36
    private $stateStore;
37
38
    /**
39
     * @var CommandService
40
     */
41
    private $commandService;
42
43
    public function __construct(RecoveryTokenState $stateStore, CommandService $commandService)
44
    {
45
        $this->stateStore = $stateStore;
46
        $this->commandService = $commandService;
47
    }
48
49
    public function produceSecret(): SafeStoreSecret
50
    {
51
        try {
52
            // On another request, we might have already created a secret, retrieve that
53
            $secret = $this->stateStore->retrieveSecret();
54
        } catch (SafeStoreSecretNotFoundException $e) {
55
            $secret = new SafeStoreSecret();
56
            $this->stateStore->store($secret);
57
        }
58
        return $secret;
59
    }
60
61
    public function promisePossession(PromiseSafeStorePossessionCommand $command): ExecutionResult
62
    {
63
        $apiCommand = new PromiseSafeStoreSecretTokenPossessionCommand();
64
        $apiCommand->identityId = $command->identity->id;
0 ignored issues
show
Bug introduced by
The property id does not exist on string.
Loading history...
65
        $apiCommand->recoveryTokenId = Uuid::generate();
66
        $apiCommand->secret = $command->secret->display();
67
        $this->stateStore->forget();
68
        $this->stateStore->tokenCreatedDuringSecondFactorRegistration();
69
        return $this->commandService->execute($apiCommand);
70
    }
71
72
    public function revokeRecoveryToken(RevokeRecoveryTokenCommand $command): ExecutionResult
73
    {
74
        $apiCommand = new RevokeOwnRecoveryTokenCommand();
75
        $apiCommand->identityId = $command->identity->id;
76
        $apiCommand->recoveryTokenId = $command->recoveryToken->recoveryTokenId;
77
        return $this->commandService->execute($apiCommand);
78
    }
79
80
    public function wasSafeStoreTokenCreatedDuringSecondFactorRegistration(): bool
81
    {
82
        return $this->stateStore->wasRecoveryTokenCreatedDuringSecondFactorRegistration();
83
    }
84
85
    /**
86
     * Verifies if the password hash matches the secret that was provided
87
     */
88
    public function authenticate(string $secret, string $passwordHash)
89
    {
90
        return password_verify($secret, $passwordHash);
91
    }
92
93
    public function forgetSafeStoreTokenCreatedDuringSecondFactorRegistration(): void
94
    {
95
        $this->stateStore->forgetTokenCreatedDuringSecondFactorRegistration();
96
    }
97
}
98