Completed
Push — feature/implement-state-handli... ( a0fda0...18e737 )
by Michiel
02:06
created

AuthenticationLogger::getStateHandler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 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\StepupGateway\GatewayBundle\Monolog\Logger;
20
21
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger;
22
use Surfnet\StepupBundle\Service\LoaResolutionService;
23
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
24
use Surfnet\StepupBundle\Value\Loa;
25
use Surfnet\StepupGateway\GatewayBundle\Exception\InvalidArgumentException;
26
use Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler;
27
use Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService;
28
29
class AuthenticationLogger
30
{
31
    /**
32
     * @var ProxyStateHandler
33
     */
34
    private $ssoProxyStateHandler;
35
36
    /**
37
     * @var ProxyStateHandler
38
     */
39
    private $sfoProxyStateHandler;
40
41
    /**
42
     * @var SecondFactorService
43
     */
44
    private $secondFactorService;
45
46
    /**
47
     * @var LoaResolutionService
48
     */
49
    private $loaResolutionService;
50
51
    /**
52
     * @var SamlAuthenticationLogger
53
     */
54
    private $authenticationChannelLogger;
55
56
    /**
57
     * @var SecondFactorTypeService
58
     */
59
    private $secondFactorTypeService;
60
61 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
        LoaResolutionService $loaResolutionService,
63
        ProxyStateHandler $ssoProxyStateHandler,
64
        ProxyStateHandler $sfoProxyStateHandler,
65
        SecondFactorService $secondFactorService,
66
        SamlAuthenticationLogger $authenticationChannelLogger,
67
        SecondFactorTypeService $service
68
    ) {
69
        $this->loaResolutionService = $loaResolutionService;
70
        $this->ssoProxyStateHandler = $ssoProxyStateHandler;
71
        $this->sfoProxyStateHandler = $sfoProxyStateHandler;
72
        $this->secondFactorService  = $secondFactorService;
73
        $this->authenticationChannelLogger = $authenticationChannelLogger;
74
        $this->secondFactorTypeService = $service;
75
    }
76
77
    /**
78
     * @param string $requestId The SAML authentication request ID of the original request (not the proxy request).
79
     */
80
    public function logIntrinsicLoaAuthentication($requestId)
81
    {
82
        $context = [
83
            'second_factor_id'      => '',
84
            'second_factor_type'    => '',
85
            'institution'           => '',
86
            'authentication_result' => 'NONE',
87
            'resulting_loa'         => (string) $this->loaResolutionService->getLoaByLevel(Loa::LOA_1),
88
        ];
89
90
        $this->log('Intrinsic Loa Requested', $context, $requestId);
91
    }
92
93
    /**
94
     * @param string $requestId The SAML authentication request ID of the original request (not the proxy request).
95
     * @param string $authenticationMode
96
     */
97
    public function logSecondFactorAuthentication($requestId, $authenticationMode)
98
    {
99
        $stateHandler = $this->getStateHandler($authenticationMode);
100
        $secondFactor = $this->secondFactorService->findByUuid($stateHandler->getSelectedSecondFactorId());
101
        $loa = $this->loaResolutionService->getLoaByLevel($secondFactor->getLoaLevel($this->secondFactorTypeService));
102
103
        $context = [
104
            'second_factor_id'      => $secondFactor->secondFactorId,
105
            'second_factor_type'    => $secondFactor->secondFactorType,
106
            'institution'           => $secondFactor->institution,
107
            'authentication_result' => $stateHandler->isSecondFactorVerified() ? 'OK' : 'FAILED',
108
            'resulting_loa'         => (string) $loa,
109
        ];
110
111
        $this->log('Second Factor Authenticated', $context, $requestId);
112
    }
113
114
    /**
115
     * @param string $message
116
     * @param array  $context
117
     * @param string $requestId
118
     */
119
    private function log($message, array $context, $requestId)
120
    {
121
        if (!is_string($requestId)) {
122
            throw InvalidArgumentException::invalidType('string', 'requestId', $requestId);
123
        }
124
        // Regardless of authentication type, the authentication mode can be retrieved from any state handler
125
        // given you provide the request id
126
        $authenticationMode = $this->getStateHandler('sso')->getAuthenticationModeForRequestId($requestId);
127
        $stateHandler = $this->getStateHandler($authenticationMode);
128
129
        $context['identity_id']        = $stateHandler->getIdentityNameId();
130
        $context['authenticating_idp'] = $stateHandler->getAuthenticatingIdp();
131
        $context['requesting_sp']      = $stateHandler->getRequestServiceProvider();
132
        $context['datetime']           = (new \DateTime())->format('Y-m-d\\TH:i:sP');
133
134
        $this->authenticationChannelLogger->forAuthentication($requestId)->notice($message, $context);
135
    }
136
137
    /**
138
     * @param string $authenticationMode
139
     * @return ProxyStateHandler
140
     */
141
    private function getStateHandler($authenticationMode)
142
    {
143
        if ($authenticationMode === 'sfo') {
144
            return $this->sfoProxyStateHandler;
145
        } else if ($authenticationMode === 'sso') {
146
            return $this->ssoProxyStateHandler;
147
        }
148
        throw new InvalidArgumentException(
149
            sprintf('Retrieving a state handler for authentication type %s is not supported', $authenticationMode)
150
        );
151
    }
152
}
153