|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2018 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\SecondFactorOnlyBundle\Service\Gateway; |
|
20
|
|
|
|
|
21
|
|
|
use Exception; |
|
22
|
|
|
use Psr\Log\LoggerInterface; |
|
23
|
|
|
use Surfnet\SamlBundle\SAML2\ReceivedAuthnRequest; |
|
24
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext; |
|
25
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\Exception\InvalidAdfsRequestException; |
|
26
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\Exception\InvalidAdfsResponseException; |
|
27
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\RequestHelper; |
|
28
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\ResponseHelper; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
30
|
|
|
|
|
31
|
|
|
class AdfsService |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var RequestHelper */ |
|
34
|
|
|
private $adfsRequestHelper; |
|
35
|
|
|
|
|
36
|
|
|
/** @var ResponseHelper */ |
|
37
|
|
|
private $adfsResponseHelper; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* SecondFactorAdfsService constructor. |
|
41
|
|
|
* @param RequestHelper $adfsRequestHelper |
|
42
|
|
|
* @param ResponseHelper $adfsResponseHelper |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(RequestHelper $adfsRequestHelper, ResponseHelper $adfsResponseHelper) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->adfsRequestHelper = $adfsRequestHelper; |
|
47
|
|
|
$this->adfsResponseHelper = $adfsResponseHelper; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* This method detects if a request is made by ADFS, and converts it to a valid |
|
52
|
|
|
* Saml AuthnRequest request which could be processed. |
|
53
|
|
|
* |
|
54
|
|
|
* @param LoggerInterface $logger |
|
55
|
|
|
* @param Request $httpRequest |
|
56
|
|
|
* @param ReceivedAuthnRequest $originalRequest |
|
57
|
|
|
* @return Request |
|
58
|
|
|
* @throws InvalidAdfsRequestException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function handleAdfsRequest(LoggerInterface $logger, Request $httpRequest, ReceivedAuthnRequest $originalRequest) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->adfsRequestHelper->isAdfsRequest($httpRequest)) { |
|
63
|
|
|
$logger->notice('Received AuthnRequest from an ADFS'); |
|
64
|
|
|
try { |
|
65
|
|
|
$httpRequest = $this->adfsRequestHelper->transformRequest( |
|
66
|
|
|
$httpRequest, |
|
67
|
|
|
$originalRequest->getRequestId() |
|
68
|
|
|
); |
|
69
|
|
|
} catch (Exception $e) { |
|
70
|
|
|
throw new InvalidAdfsRequestException( |
|
71
|
|
|
sprintf('Could not process ADFS Request, error: "%s"', $e->getMessage()) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $httpRequest; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* This method detectds if we need to return a ADFS response, If so ADFS parameters are returned. |
|
81
|
|
|
* |
|
82
|
|
|
* Second factor verification handled by SecondFactorController is |
|
83
|
|
|
* finished. The user was forwarded back to this action with an internal |
|
84
|
|
|
* redirect. This method sends a AuthnResponse back to the service |
|
85
|
|
|
* provider in response to the AuthnRequest received in ssoAction(). |
|
86
|
|
|
* |
|
87
|
|
|
* @param LoggerInterface $logger |
|
88
|
|
|
* @param ResponseContext $responseContext |
|
89
|
|
|
* @return null|\Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\ValueObject\Response |
|
90
|
|
|
* @throws InvalidAdfsResponseException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function handleAdfsResponse(LoggerInterface $logger, ResponseContext $responseContext) |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->adfsResponseHelper->isAdfsResponse($responseContext->getInResponseTo())) { |
|
95
|
|
|
|
|
96
|
|
|
try { |
|
97
|
|
|
$adfsParameters = $this->adfsResponseHelper->retrieveAdfsParameters(); |
|
98
|
|
|
} catch (Exception $e) { |
|
99
|
|
|
throw new InvalidAdfsResponseException( |
|
100
|
|
|
sprintf('Could not process ADFS Response parameters, error: "%s"', $e->getMessage()) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$logger->notice('Sending ACS Response to ADFS plugin'); |
|
105
|
|
|
|
|
106
|
|
|
return $adfsParameters; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return null; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.