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

SingleSignOnAuthenticator::onCredentials()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * AppserverIo\Authenticator\SingleSignOnAuthenticator
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;
22
23
use AppserverIo\Lang\String;
24
use AppserverIo\Lang\Boolean;
25
use AppserverIo\Psr\HttpMessage\Protocol;
26
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface;
27
use AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface;
28
use AppserverIo\Psr\Auth\LoginConfigurationInterface;
29
use AppserverIo\Psr\Auth\AuthenticationManagerInterface;
30
use AppserverIo\Authenticator\FormAuthenticator;
31
use AppserverIo\Authenticator\Utils\FormKeys;
32
use AppserverIo\Authenticator\Utils\FormPageUtil;
33
use AppserverIo\Authenticator\Utils\SingleSignOnFormPageUtil;
34
use AppserverIo\Appserver\Core\Api\Node\AuthenticatorNodeInterface;
0 ignored issues
show
Bug introduced by
The type AppserverIo\Appserver\Co...henticatorNodeInterface 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...
35
36
/**
37
 * A form based authenticator implementation.
38
 *
39
 * @author    Tim Wagner <[email protected]>
40
 * @copyright 2016 TechDivision GmbH <[email protected]>
41
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
42
 * @link      https://github.com/appserver-io/authenticator
43
 * @link      http://www.appserver.io
44
 */
45
class SingleSignOnAuthenticator extends FormAuthenticator
46
{
47
48
    /**
49
     * Defines the auth type which should match the client request type definition
50
     *
51
     * @var string AUTH_TYPE
52
     */
53
    const AUTH_TYPE = 'SingleSignOn';
54
55
    /**
56
     * The authorization code to authenticate the user with.
57
     *
58
     * @var string
59
     */
60
    protected $authorizationCode;
61
62
    /**
63
     * The utility instance to handle SSO functionality.
64
     *
65
     * @var \AppserverIo\Authenticator\Utils\SingleSignOnUtil
0 ignored issues
show
Bug introduced by
The type AppserverIo\Authenticator\Utils\SingleSignOnUtil 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...
66
     */
67
    protected $singleSignOnFormPageUtil;
68
69
    /**
70
     * Constructs the authentication type.
71
     *
72
     * @param \AppserverIo\Psr\Auth\LoginConfigurationInterface               $configData                 The configuration data for auth type instance
73
     * @param \AppserverIo\Psr\Auth\AuthenticationManagerInterface            $authenticationManager      The authentication manager instance
74
     * @param \AppserverIo\Appserver\Core\Api\Node\AuthenticatorNodeInterface $authenticatorConfiguration The authenticator configuration instance
75
     */
76
    public function __construct(
77
        LoginConfigurationInterface $configData,
78
        AuthenticationManagerInterface $authenticationManager,
79
        AuthenticatorNodeInterface $authenticatorConfiguration
80
    ) {
81
82
        // initialize the form page utility
83
        $this->singleSignOnFormPageUtil = new SingleSignOnFormPageUtil(new FormPageUtil());
0 ignored issues
show
Documentation Bug introduced by
It seems like new AppserverIo\Authenti...r\Utils\FormPageUtil()) of type AppserverIo\Authenticato...ingleSignOnFormPageUtil is incompatible with the declared type AppserverIo\Authenticator\Utils\SingleSignOnUtil of property $singleSignOnFormPageUtil.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
85
        // pass the instances to the parent constructor
86
        parent::__construct($configData, $authenticationManager, $authenticatorConfiguration);
87
    }
88
89
    /**
90
     * Returns the parsed authorization code.
91
     *
92
     * @return \AppserverIo\Lang\String The authorization
93
     */
94
    public function getAuthorizationCode()
95
    {
96
        return $this->authorizationCode ? $this->authorizationCode : null;
97
    }
98
99
    /**
100
     * Return's the location for the 307 redirect to the login page.
101
     *
102
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The servlet request instance
103
     *
104
     * @return string The location for the 307 redirect
105
     */
106
    protected function getLoginPage(HttpServletRequestInterface $servletRequest)
107
    {
108
        return $this->singleSignOnFormPageUtil->getLoginPage($servletRequest, $this->getConfigData(), $this->getAuthenticationManager());
109
    }
110
111
    /**
112
     * Return's the array with the login credentials.
113
     *
114
     * @return \AppserverIo\Lang\String[] The array with the login credentials
115
     */
116
    protected function getCredentials()
117
    {
118
        return array($this->getUsername(), $this->getPassword(), $this->getAuthorizationCode());
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($this->getU...getAuthorizationCode()) returns an array which contains values of type string which are incompatible with the documented value type AppserverIo\Lang\String.
Loading history...
119
    }
120
121
    /**
122
     * Will be invoked to load the credentials from the request.
123
     *
124
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The servlet request instance
125
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The servlet response instance
126
     *
127
     * @return void
128
     */
129
    protected function onCredentials(
130
        HttpServletRequestInterface $servletRequest,
131
        HttpServletResponseInterface $servletResponse
132
    ) {
133
134
        // try to load authorization code from the request instead
135
        if ($servletRequest->hasParameter(FormKeys::CODE)) {
136
            // load authorization code from the request
137
            $this->authorizationCode = new String($servletRequest->getParameter(FormKeys::CODE, FILTER_UNSAFE_RAW));
138
        }
139
140
        // also try to load username and password
141
        parent::onCredentials($servletRequest, $servletResponse);
142
    }
143
144
    /**
145
     * Forward's the request to the configured login page.
146
     *
147
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The servlet request instance
148
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The servlet response instance
149
     *
150
     * @return void
151
     */
152
    protected function forwardToLoginPage(
153
        HttpServletRequestInterface $servletRequest,
154
        HttpServletResponseInterface $servletResponse
155
    ) {
156
157
        try {
158
            // load the location for the login page
159
            $location = $this->getLoginPage($servletRequest);
160
            // redirect to the configured login page
161
            $servletRequest->setDispatched(true);
162
            $servletResponse->setStatusCode(307);
163
            $servletResponse->addHeader(Protocol::HEADER_LOCATION, $location);
164
        } catch (SecurityException $se) {
0 ignored issues
show
Bug introduced by
The type AppserverIo\Authenticator\SecurityException 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...
165
            // redirect to the default error page
166
            $servletRequest->setAttribute(
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on AppserverIo\Psr\Servlet\...ServletRequestInterface. ( Ignorable by Annotation )

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

166
            $servletRequest->/** @scrutinizer ignore-call */ 
167
                             setAttribute(

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...
167
                RequestHandlerKeys::ERROR_MESSAGE,
0 ignored issues
show
Bug introduced by
The type AppserverIo\Authenticator\RequestHandlerKeys 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...
168
                $se->getMessage()
169
            );
170
            $servletRequest->setDispatched(true);
171
            $servletResponse->setStatusCode(500);
172
        }
173
    }
174
}
175