Completed
Push — develop ( 44ec0d...21eab1 )
by Michiel
02:03
created

Authentication/SamlInteractionProvider.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\StepupSelfService\SelfServiceBundle\Security\Authentication;
20
21
use Surfnet\SamlBundle\Entity\IdentityProvider;
22
use Surfnet\SamlBundle\Entity\ServiceProvider;
23
use Surfnet\SamlBundle\Http\PostBinding;
24
use Surfnet\SamlBundle\Http\RedirectBinding;
25
use Surfnet\SamlBundle\SAML2\AuthnRequestFactory;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\UnexpectedIssuerException;
27
use Symfony\Component\HttpFoundation\Request;
28
29
class SamlInteractionProvider
30
{
31
    /**
32
     * @var \Surfnet\SamlBundle\Entity\ServiceProvider
33
     */
34
    private $serviceProvider;
35
36
    /**
37
     * @var \Surfnet\SamlBundle\Entity\IdentityProvider
38
     */
39
    private $identityProvider;
40
41
    /**
42
     * @var \Surfnet\SamlBundle\Http\RedirectBinding
43
     */
44
    private $redirectBinding;
45
46
    /**
47
     * @var \Surfnet\SamlBundle\Http\PostBinding
48
     */
49
    private $postBinding;
50
51
    /**
52
     * @var SamlAuthenticationStateHandler
53
     */
54
    private $samlAuthenticationStateHandler;
55
56
    public function __construct(
57
        ServiceProvider $serviceProvider,
58
        IdentityProvider $identityProvider,
59
        RedirectBinding $redirectBinding,
60
        PostBinding $postBinding,
61
        SamlAuthenticationStateHandler $samlAuthenticationStateHandler
62
    ) {
63
        $this->serviceProvider                = $serviceProvider;
64
        $this->identityProvider               = $identityProvider;
65
        $this->redirectBinding                = $redirectBinding;
66
        $this->postBinding                    = $postBinding;
67
        $this->samlAuthenticationStateHandler = $samlAuthenticationStateHandler;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function isSamlAuthenticationInitiated()
74
    {
75
        return $this->samlAuthenticationStateHandler->hasRequestId();
76
    }
77
78
    /**
79
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
80
     */
81
    public function initiateSamlRequest()
82
    {
83
        $authnRequest = AuthnRequestFactory::createNewRequest(
84
            $this->serviceProvider,
85
            $this->identityProvider
86
        );
87
88
        $this->samlAuthenticationStateHandler->setRequestId($authnRequest->getRequestId());
89
90
        return $this->redirectBinding->createRedirectResponseFor($authnRequest);
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\SamlBundle\Http\...teRedirectResponseFor() has been deprecated with message: Please use the `createResponseFor` method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
91
    }
92
93
    /**
94
     * @param Request $request
95
     * @return \SAML2\Assertion
96
     */
97
    public function processSamlResponse(Request $request)
98
    {
99
        /** @var \SAML2\Assertion $assertion */
100
        $assertion = $this->postBinding->processResponse(
101
            $request,
102
            $this->identityProvider,
103
            $this->serviceProvider
104
        );
105
106
        if ($assertion->getIssuer() !== $this->identityProvider->getEntityId()) {
107
            throw new UnexpectedIssuerException(sprintf(
108
                'Expected issuer to be configured remote IdP "%s", got "%s"',
109
                $this->identityProvider->getEntityId(),
110
                $assertion->getIssuer()
111
            ));
112
        }
113
114
        $this->samlAuthenticationStateHandler->clearRequestId();
115
116
        return $assertion;
117
    }
118
119
    /**
120
     * Resets the SAML flow.
121
     */
122
    public function reset()
123
    {
124
        $this->samlAuthenticationStateHandler->clearRequestId();
125
    }
126
}
127