|
17 | 17 | use SimpleSAML\Module\casserver\Controller\LoginController; |
18 | 18 | use SimpleSAML\Session; |
19 | 19 | use SimpleSAML\Utils; |
| 20 | +use SimpleSAML\XHTML\Template; |
20 | 21 | use Symfony\Component\HttpFoundation\Request; |
21 | 22 | use Symfony\Component\HttpFoundation\Response; |
22 | 23 |
|
@@ -106,6 +107,7 @@ private function callLogin(LoginController $controller, Request $request, array |
106 | 107 | entityId: isset($params['entityId']) ? (string)$params['entityId'] : null, |
107 | 108 | debugMode: isset($params['debugMode']) ? (string)$params['debugMode'] : null, |
108 | 109 | method: isset($params['method']) ? (string)$params['method'] : null, |
| 110 | + pgtUrl: isset($params['pgtUrl']) ? (string)$params['pgtUrl'] : null, |
109 | 111 | ); |
110 | 112 | } |
111 | 113 |
|
@@ -1086,4 +1088,146 @@ public function testAuthenticatedPostSubmitsViaPostWithTicket(): void |
1086 | 1088 | $this->assertArrayHasKey('ticket', $params); |
1087 | 1089 | $this->assertStringStartsWith('ST-', $params['ticket']); |
1088 | 1090 | } |
| 1091 | + |
| 1092 | + |
| 1093 | + /** |
| 1094 | + * @return array<string, array{array<string, string>}> |
| 1095 | + */ |
| 1096 | + public static function debugModeServiceValidateProvider(): array |
| 1097 | + { |
| 1098 | + $service = 'https://example.com/ssp/module.php/cas/linkback.php'; |
| 1099 | + return [ |
| 1100 | + 'service' => [['service' => $service, 'debugMode' => 'true']], |
| 1101 | + 'TARGET' => [['TARGET' => $service, 'debugMode' => 'true']], |
| 1102 | + 'service with renew' => [['service' => $service, 'debugMode' => 'true', 'renew' => 'true']], |
| 1103 | + 'TARGET with renew' => [['TARGET' => $service, 'debugMode' => 'true', 'renew' => 'true']], |
| 1104 | + ]; |
| 1105 | + } |
| 1106 | + |
| 1107 | + |
| 1108 | + /** |
| 1109 | + * Debug mode validates the ticket it has just issued against the service it was issued for, whichever query |
| 1110 | + * parameter named the service, and whether or not renew was requested. |
| 1111 | + * |
| 1112 | + * @param array<string, string> $requestParams |
| 1113 | + * |
| 1114 | + * @throws \Exception |
| 1115 | + */ |
| 1116 | + #[DataProvider('debugModeServiceValidateProvider')] |
| 1117 | + public function testDebugModeValidatesTheIssuedTicket(array $requestParams): void |
| 1118 | + { |
| 1119 | + $loginRequest = Request::create( |
| 1120 | + uri: Module::getModuleURL('casserver/login'), |
| 1121 | + parameters: $requestParams, |
| 1122 | + ); |
| 1123 | + $response = $this->callLogin($this->createDebugModeController(), $loginRequest, $requestParams); |
| 1124 | + |
| 1125 | + $this->assertDebugModeValidated($response); |
| 1126 | + } |
| 1127 | + |
| 1128 | + |
| 1129 | + /** |
| 1130 | + * @return array<string, array{array<string, string>, array<string, mixed>}> |
| 1131 | + */ |
| 1132 | + public static function debugModeRequestBodyProvider(): array |
| 1133 | + { |
| 1134 | + $service = 'https://example.com/ssp/module.php/cas/linkback.php'; |
| 1135 | + return [ |
| 1136 | + // Would ask for a ticket from a fresh login, which this one, issued from the SSO session, is not |
| 1137 | + 'renew' => [['service' => $service, 'debugMode' => 'true'], ['renew' => 'true']], |
| 1138 | + // Would name a service the ticket was not issued for |
| 1139 | + 'service' => [['TARGET' => $service, 'debugMode' => 'true'], ['service' => 'https://example.com/other']], |
| 1140 | + // Would not be a string |
| 1141 | + 'TARGET array' => [['service' => $service, 'debugMode' => 'true'], ['TARGET' => ['x']]], |
| 1142 | + ]; |
| 1143 | + } |
| 1144 | + |
| 1145 | + |
| 1146 | + /** |
| 1147 | + * Debug mode validates the ticket with the query parameters login() issued it with, so parameters |
| 1148 | + * in the POST body cannot change the result. |
| 1149 | + * |
| 1150 | + * @param array<string, string> $queryParams |
| 1151 | + * @param array<string, mixed> $bodyParams |
| 1152 | + * |
| 1153 | + * @throws \Exception |
| 1154 | + */ |
| 1155 | + #[DataProvider('debugModeRequestBodyProvider')] |
| 1156 | + public function testDebugModeIgnoresTheRequestBody(array $queryParams, array $bodyParams): void |
| 1157 | + { |
| 1158 | + $loginRequest = Request::create( |
| 1159 | + uri: Module::getModuleURL('casserver/login') . '?' . http_build_query($queryParams), |
| 1160 | + method: 'POST', |
| 1161 | + parameters: $bodyParams, |
| 1162 | + ); |
| 1163 | + $response = $this->callLogin($this->createDebugModeController(), $loginRequest, $queryParams); |
| 1164 | + |
| 1165 | + $this->assertDebugModeValidated($response); |
| 1166 | + } |
| 1167 | + |
| 1168 | + |
| 1169 | + /** |
| 1170 | + * An unsupported debug mode renders the error template, without any XML to show. |
| 1171 | + * |
| 1172 | + * @throws \Exception |
| 1173 | + */ |
| 1174 | + public function testUnsupportedDebugModeRendersTheErrorWithoutXml(): void |
| 1175 | + { |
| 1176 | + $requestParams = [ |
| 1177 | + 'service' => 'https://example.com/ssp/module.php/cas/linkback.php', |
| 1178 | + 'debugMode' => 'unsupported', |
| 1179 | + ]; |
| 1180 | + $loginRequest = Request::create( |
| 1181 | + uri: Module::getModuleURL('casserver/login'), |
| 1182 | + parameters: $requestParams, |
| 1183 | + ); |
| 1184 | + $response = $this->callLogin($this->createDebugModeController(), $loginRequest, $requestParams); |
| 1185 | + |
| 1186 | + $this->assertInstanceOf(Template::class, $response); |
| 1187 | + $this->assertSame(Response::HTTP_BAD_REQUEST, $response->data['statusCode']); |
| 1188 | + $this->assertSame('unsupported', $response->data['debugMode']); |
| 1189 | + $this->assertArrayNotHasKey('DebugModeXml', $response->data); |
| 1190 | + } |
| 1191 | + |
| 1192 | + |
| 1193 | + /** |
| 1194 | + * A controller with debug mode enabled, for a user with an SSO session. |
| 1195 | + */ |
| 1196 | + private function createDebugModeController(): LoginController |
| 1197 | + { |
| 1198 | + $moduleConfig = $this->moduleConfig; |
| 1199 | + $moduleConfig['debugMode'] = true; |
| 1200 | + $casconfig = Configuration::loadFromArray($moduleConfig); |
| 1201 | + |
| 1202 | + $controllerMock = $this->getMockBuilder(LoginController::class) |
| 1203 | + ->setConstructorArgs([$this->sspConfig, $casconfig, $this->authSimpleMock, $this->httpUtils]) |
| 1204 | + ->onlyMethods(['getSession']) |
| 1205 | + ->getMock(); |
| 1206 | + $controllerMock->expects($this->once())->method('getSession')->willReturn($this->sessionMock); |
| 1207 | + $this->sessionMock->expects($this->exactly(2))->method('getSessionId')->willReturn(session_create_id()); |
| 1208 | + $this->authSimpleMock->expects($this->atLeastOnce())->method('isAuthenticated')->willReturn(true); |
| 1209 | + $this->authSimpleMock->expects($this->once())->method('getAuthData')->with('Expire')->willReturn(9999999999); |
| 1210 | + $this->authSimpleMock->expects($this->once())->method('getAuthDataArray')->willReturn([ |
| 1211 | + 'Attributes' => ['eduPersonPrincipalName' => ['testuser@example.com']], |
| 1212 | + 'Expire' => 9999999999, |
| 1213 | + ]); |
| 1214 | + |
| 1215 | + return $controllerMock; |
| 1216 | + } |
| 1217 | + |
| 1218 | + |
| 1219 | + /** |
| 1220 | + * Debug mode rendered a successful serviceValidate response for the test user. |
| 1221 | + */ |
| 1222 | + private function assertDebugModeValidated(mixed $response): void |
| 1223 | + { |
| 1224 | + $this->assertInstanceOf(Template::class, $response); |
| 1225 | + $this->assertSame(Response::HTTP_OK, $response->data['statusCode']); |
| 1226 | + $xml = simplexml_load_string((string)$response->data['DebugModeXml']); |
| 1227 | + $xml->registerXPathNamespace('cas', 'serviceResponse'); |
| 1228 | + $this->assertSame( |
| 1229 | + 'testuser@example.com', |
| 1230 | + (string)$xml->xpath('//cas:authenticationSuccess/cas:user')[0], |
| 1231 | + ); |
| 1232 | + } |
1089 | 1233 | } |
0 commit comments