Completed
Push — feature/upgrade-remote-vetting ( 883904 )
by
unknown
65:35
created

RemoteVettingService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 150
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A start() 0 6 1
A startValidation() 0 6 1
A finishValidation() 0 6 1
A done() 0 17 1
A getAttributeMatchCollection() 0 7 1
A getActiveIdentityProviderSlug() 0 4 1
A aggregateIdentityData() 0 26 1
1
<?php
2
3
/**
4
 * Copyright 2019 SURFnet B.V.
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;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\AttributeMapper;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Dto\AttributeListDto;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Dto\RemoteVettingTokenDto;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Encryption\IdentityData;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Encryption\IdentityEncrypterInterface;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\RemoteVettingContext;
29
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeCollectionAggregate;
30
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeMatchCollection;
31
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\ProcessId;
32
33
class RemoteVettingService
34
{
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
    /**
40
     * @var RemoteVettingContext
41
     */
42
    private $remoteVettingContext;
43
    /**
44
     * @var IdentityEncrypterInterface
45
     */
46
    private $identityEncrypter;
47
    /**
48
     * @var AttributeMapper
49
     */
50
    private $attributeMapper;
51
    /**
52
     * @var \Surfnet\StepupSelfService\SelfServiceBundle\Service\ApplicationHelper
53
     */
54
    private $applicationHelper;
55
56
57
    public function __construct(
58
        RemoteVettingContext $remoteVettingContext,
59
        AttributeMapper $attributeMapper,
60
        IdentityEncrypterInterface $identityEncrypter,
61
        ApplicationHelper $applicationHelper,
62
        LoggerInterface $logger
63
    ) {
64
        $this->remoteVettingContext = $remoteVettingContext;
65
        $this->logger = $logger;
66
        $this->identityEncrypter = $identityEncrypter;
67
        $this->attributeMapper = $attributeMapper;
68
        $this->applicationHelper = $applicationHelper;
69
    }
70
71
    /**
72
     * @param string $identityProviderSlug
73
     * @param RemoteVettingTokenDto $remoteVettingToken
74
     */
75
    public function start($identityProviderSlug, RemoteVettingTokenDto $remoteVettingToken)
76
    {
77
        $this->logger->info('Starting an remote vetting process for the provided token');
78
79
        $this->remoteVettingContext->initialize($identityProviderSlug, $remoteVettingToken);
80
    }
81
82
    /**
83
     * @param ProcessId $processId
84
     */
85
    public function startValidation(ProcessId $processId)
86
    {
87
        $this->logger->info('Starting an remote vetting authentication for the current process');
88
89
        $this->remoteVettingContext->validating($processId);
90
    }
91
92
    /**
93
     * @param ProcessId $processId
94
     * @param AttributeListDto $externalAttributes
95
     */
96
    public function finishValidation(ProcessId $processId, AttributeListDto $externalAttributes)
97
    {
98
        $this->logger->info('Finishing a remote vetting authentication for the current process');
99
100
        $this->remoteVettingContext->validated($processId, $externalAttributes);
101
    }
102
103
    /**
104
     * @param ProcessId $processId
105
     * @param Identity $identity
106
     * @param AttributeListDto $localAttributes
107
     * @param AttributeMatchCollection $attributeMatches
108
     * @param string $remarks
109
     * @return RemoteVettingTokenDto
110
     */
111
    public function done(
112
        ProcessId $processId,
113
        Identity $identity,
114
        AttributeListDto $localAttributes,
115
        AttributeMatchCollection $attributeMatches,
116
        $remarks
117
    ) {
118
        $this->remoteVettingContext->done($processId);
119
        $this->logger->info('Saving the encrypted assertion to the filesystem');
120
121
        $identityData = $this->aggregateIdentityData($identity, $localAttributes, $attributeMatches, $remarks);
122
        $this->identityEncrypter->encrypt($identityData->serialize());
123
124
        $this->logger->info('Finished the remote vetting process for the current process');
125
126
        return $this->remoteVettingContext->getValidatedToken();
127
    }
128
129
    /**
130
     * @param AttributeListDto $localAttributes
131
     * @return AttributeMatchCollection
132
     */
133
    public function getAttributeMatchCollection(AttributeListDto $localAttributes)
134
    {
135
        $externalAttributes = $this->remoteVettingContext->getAttributes();
136
        $identityProviderSlug = $this->remoteVettingContext->getIdentityProviderSlug();
137
138
        return $this->attributeMapper->map($identityProviderSlug, $localAttributes, $externalAttributes);
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getActiveIdentityProviderSlug()
145
    {
146
        return $this->remoteVettingContext->getIdentityProviderSlug();
147
    }
148
149
    /**
150
     * @param Identity $identity
151
     * @param AttributeListDto $localAttributes
152
     * @param AttributeMatchCollection $attributeMatches
153
     * @param string $remarks
154
     * @return IdentityData
155
     */
156
    private function aggregateIdentityData(
157
        Identity $identity,
158
        AttributeListDto $localAttributes,
159
        AttributeMatchCollection $attributeMatches,
160
        $remarks
161
    ) {
162
        $nameId = $identity->nameId;
163
        $institution = $identity->institution;
164
        $version = $this->applicationHelper->getApplicationVersion();
165
        $remarks = (string)$remarks;
166
        $remoteVettingSource = $this->remoteVettingContext->getIdentityProviderSlug();
167
168
        $attributeCollectionAggregate = new AttributeCollectionAggregate();
169
        $attributeCollectionAggregate->add('local-attributes', $localAttributes);
170
        $attributeCollectionAggregate->add('remote-attributes', $this->remoteVettingContext->getAttributes());
171
        $attributeCollectionAggregate->add('matching-results', $attributeMatches);
172
173
        return new IdentityData(
174
            $attributeCollectionAggregate,
175
            $nameId,
176
            $version,
177
            $remarks,
178
            $institution,
179
            $remoteVettingSource
180
        );
181
    }
182
}
183