Completed
Push — 0.3.x ( 54f739...50bfd1 )
by Dmitry
06:06 queued 02:35
created

SingleSignOnController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 2 Features 5
Metric Value
wmc 8
c 9
b 2
f 5
lcom 1
cbo 5
dl 0
loc 53
ccs 0
cts 32
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B ssoLoginAction() 0 29 6
A ssoLogoutAction() 0 12 2
1
<?php
2
3
namespace Krtv\Bundle\SingleSignOnIdentityProviderBundle\Controller;
4
5
use Krtv\Bundle\SingleSignOnIdentityProviderBundle\Event\SsoAuthorizedEvent;
6
use Krtv\Bundle\SingleSignOnIdentityProviderBundle\Event\SsoEvents;
7
use Krtv\Bundle\SingleSignOnIdentityProviderBundle\Manager\ServiceManager;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13
14
/**
15
 * Class SingleSignOnController
16
 * @package Krtv\Bundle\SingleSignOnIdentityProviderBundle\Controller
17
 */
18
class SingleSignOnController extends Controller
19
{
20
    /**
21
     * @param Request $request
22
     * @return RedirectResponse
23
     */
24
    public function ssoLoginAction(Request $request)
25
    {
26
        $httpUtils = $this->get('krtv_single_sign_on_identity_provider.security.http_utils');
27
28
        if (!$httpUtils->hasTargetPath($request)) {
29
            throw new BadRequestHttpException('Target path not specified');
30
        }
31
32
        if (false === $httpUtils->checkUrl($request->getSchemeAndHttpHost().$request->getRequestUri())) {
33
            throw new BadRequestHttpException('Malformed uri');
34
        }
35
36
        if (false === $this->get('security.authorization_checker')->isGranted('ROLE_USER') && $request->get('_failure_path')) {
37
            return $httpUtils->createRedirectResponse($request, $request->get('_failure_path'));
38
        } elseif (false === $this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
39
            throw new AccessDeniedException();
40
        }
41
42
        $this->get('event_dispatcher')->dispatch(SsoEvents::SSO_AUTHORIZED, new SsoAuthorizedEvent());
43
44
        $otpOrmManager = $this->get('krtv_single_sign_on_identity_provider.security.authentication.otp_manager.orm');
45
        $otpEncoder = $this->get('krtv_single_sign_on_identity_provider.security.authentication.encoder');
46
47
        $otp = $otpOrmManager->create(
48
            $otpEncoder->generateOneTimePasswordValue($this->getUser()->getUsername(), microtime(true) + 300)
49
        );
50
51
        return $httpUtils->createSignedRedirectResponse($request, $httpUtils->createWrappedTargetPath($request, $otp));
52
    }
53
54
    /**
55
     * @param Request $request
56
     * @return RedirectResponse
57
     */
58
    public function ssoLogoutAction(Request $request)
59
    {
60
        $serviceManager = $this->get('krtv_single_sign_on_identity_provider.manager.service_manager');
61
        $logoutManager = $this->get('krtv_single_sign_on_identity_provider.manager.logout_manager');
62
        $httpUtils = $this->get('krtv_single_sign_on_identity_provider.security.http_utils');
63
64
        if (!$serviceManager->getRequestService()) {
65
            $serviceManager->setDefaults();
66
        }
67
 
68
        return $httpUtils->createRedirectResponse($request, $logoutManager->getNextLogoutUrl());
69
    }
70
}
71