Passed
Push — master ( d8cead...33fe5f )
by Tim
05:01 queued 11s
created

SingleSignOnFormPageUtil   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 1
b 0
f 0
dl 0
loc 70
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getLoginPage() 0 40 3
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * AppserverIo\Authenticator\Utils\SingleSignOnFormPageUtil
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/authenticator
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Authenticator\Utils;
22
23
use AppserverIo\Psr\Security\SecurityException;
24
use AppserverIo\Psr\Auth\LoginConfigurationInterface;
25
use AppserverIo\Psr\Auth\AuthenticationManagerInterface;
26
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface;
27
use AppserverIo\Psr\Application\ManagerConfigurationInterface;
28
use AppserverIo\Authenticator\Utils\ParamKeys;
29
use AppserverIo\Server\Dictionaries\ServerVars;
0 ignored issues
show
Bug introduced by
The type AppserverIo\Server\Dictionaries\ServerVars was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
31
/**
32
 * Utility class that helps to read the login form page configuration.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2016 TechDivision GmbH <[email protected]>
36
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link      https://github.com/appserver-io/authenticator
38
 * @link      http://www.appserver.io
39
 */
40
class SingleSignOnFormPageUtil implements FormPageUtilInterface
41
{
42
43
    /**
44
     * The general form page utility instance.
45
     *
46
     * @var \AppserverIo\Authenticator\Utils\FormPageUtilInterface
47
     */
48
    protected $formPageUtil;
49
50
    /**
51
     * Initializes the utiltiy with the general form page utility instance.
52
     *
53
     * @param \AppserverIo\Authenticator\Utils\FormPageUtilInterface $formPageUtil the general form page utility instance
54
     */
55
    public function __construct(FormPageUtilInterface $formPageUtil)
56
    {
57
        $this->formPageUtil = $formPageUtil;
58
    }
59
60
    /**
61
     * Return's the location for the redirect to the login page configured in the `web.xml` file.
62
     *
63
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest        The servlet request instance
64
     * @param \AppserverIo\Psr\Auth\LoginConfigurationInterface         $configData            The login configuration in the `web.xml`
65
     * @param \AppserverIo\Psr\Auth\AuthenticationManagerInterface|null $authenticationManager The authentication manager instance
66
     *
67
     * @return string The location for the redirect to the login page
68
     * @throws \AppserverIo\Psr\Security\SecurityException Is thrown, if the appropriate form configuration in the `web.xml` is missing
69
     */
70
    public function getLoginPage(
71
        HttpServletRequestInterface $servletRequest,
72
        LoginConfigurationInterface $configData,
73
        AuthenticationManagerInterface $authenticationManager = null
74
    ) {
75
76
        // we need an authentication manager instance here
77
        if ($authenticationManager === null) {
78
            throw new SecurityException('Can\'t find mandatory authentication manager instance as 3rd method param');
79
        }
80
81
        // load the manager configuration from the authentication manager
82
        /** @var \AppserverIo\Psr\Application\ManagerConfigurationInterface $managerConfiguration */
83
        $managerConfiguration = $authenticationManager->getManagerConfiguration();
0 ignored issues
show
Bug introduced by
The method getManagerConfiguration() does not exist on AppserverIo\Psr\Auth\Aut...icationManagerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        /** @scrutinizer ignore-call */ 
84
        $managerConfiguration = $authenticationManager->getManagerConfiguration();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
85
        // load the URL of the identity provider and the authorization path
86
        $identityUrl = $managerConfiguration->getParam(ParamKeys::IDENTITY_URL);
0 ignored issues
show
Bug introduced by
The method getParam() does not exist on AppserverIo\Psr\Applicat...rConfigurationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        /** @scrutinizer ignore-call */ 
87
        $identityUrl = $managerConfiguration->getParam(ParamKeys::IDENTITY_URL);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
        $authorizePath = $managerConfiguration->getParam(ParamKeys::AUTHORIZATION_PATH);
88
89
        // create the location with the redirect URI to use
90
        $location = sprintf('%s%s', $identityUrl, $authorizePath);
91
92
        // prepare the redirect URK with the actual scheme HTTP/HTTPS and the server name
93
        $redirectUri = sprintf(
94
            '%s://%s',
95
            $servletRequest->getServerVar(ServerVars::REQUEST_SCHEME),
96
            $servletRequest->getServerVar(ServerVars::SERVER_NAME)
97
        );
98
        // load the actual server port, because by default and in local
99
        // environments we often use 9080/9443 instead of 80/443
100
        $serverPort = (int) $servletRequest->getServerVar(ServerVars::SERVER_PORT);
101
102
        // append the port, if we do NOT have one of the default ports
103
        $redirectUri = in_array($serverPort, [80, 443]) ? $redirectUri : sprintf('%s:%d', $redirectUri, $serverPort);
104
105
        // append the path from the `web.xml` we've to to redirect to
106
        $redirectUri = sprintf('%s%s', $redirectUri, $this->formPageUtil->getLoginPage($servletRequest, $configData));
107
108
        // create the location with the redirect URI to use and return it
109
        return sprintf($location, $redirectUri);
110
    }
111
}
112