|
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; |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
165
|
|
|
// redirect to the default error page |
|
166
|
|
|
$servletRequest->setAttribute( |
|
|
|
|
|
|
167
|
|
|
RequestHandlerKeys::ERROR_MESSAGE, |
|
|
|
|
|
|
168
|
|
|
$se->getMessage() |
|
169
|
|
|
); |
|
170
|
|
|
$servletRequest->setDispatched(true); |
|
171
|
|
|
$servletResponse->setStatusCode(500); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths