"/api/users/identity_providers", "/api/l10n/index",
LOGIN_URL, LOGOUT_URL, VALIDATE_URL);
+ private static final Set<String> URL_USING_PASSCODE = ImmutableSet.of(
+ "/api/system/health"
+ );
+
private static final UrlPattern URL_PATTERN = UrlPattern.builder()
.includes("/*")
.excludes(staticResourcePatterns())
.excludes(SKIPPED_URLS)
.build();
+ private static final UrlPattern PASSCODE_URLS = UrlPattern.builder()
+ .includes(URL_USING_PASSCODE)
+ .build();
+
private final Configuration config;
private final ThreadLocalUserSession threadLocalSession;
private final AuthenticationEvent authenticationEvent;
try {
// Do not set user session when url is excluded
if (URL_PATTERN.matches(path)) {
- loadUserSession(request, response);
+ loadUserSession(request, response, PASSCODE_URLS.matches(path));
}
return true;
} catch (AuthenticationException e) {
return provider != AuthenticationEvent.Provider.LOCAL && provider != AuthenticationEvent.Provider.JWT;
}
- private void loadUserSession(HttpServletRequest request, HttpServletResponse response) {
+ private void loadUserSession(HttpServletRequest request, HttpServletResponse response, boolean usingPasscode) {
UserSession session;
Optional<UserDto> user = authenticators.authenticate(request, response);
if (user.isPresent()) {
session = userSessionFactory.create(user.get());
} else {
- failIfAuthenticationIsRequired();
+ if (!usingPasscode) {
+ failIfAuthenticationIsRequired();
+ }
session = userSessionFactory.createAnonymous();
}
threadLocalSession.set(session);
assertPathIsIgnored("/api/users/identity_providers");
assertPathIsIgnored("/api/l10n/index");
+ // WS with Passcode
+ assertPathIsIgnoredWithAnonymousAccess("/api/system/health");
+
// exclude static resources
assertPathIsIgnored("/css/style.css");
assertPathIsIgnored("/fonts/font.ttf");
reset(userSession, authenticators);
}
+ private void assertPathIsIgnoredWithAnonymousAccess(String path) {
+ when(request.getRequestURI()).thenReturn(path);
+ when(authenticators.authenticate(request, response)).thenReturn(Optional.empty());
+
+ assertThat(underTest.initUserSession(request, response)).isTrue();
+
+ verify(userSession).set(any(UserSession.class));
+ reset(userSession, authenticators);
+ }
+
private void assertPathIsNotIgnored(String path) {
when(request.getRequestURI()).thenReturn(path);
when(authenticators.authenticate(request, response)).thenReturn(Optional.of(user));
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.OrchestratorBuilder;
+import java.util.Arrays;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
assertThat(response.code()).isEqualTo(401);
}
+ @Test
+ public void system_access_is_granted_even_with_forceAuthentication_is_set_to_true() {
+ tester.settings().setGlobalSetting("sonar.forceAuthentication", "true");
+ Arrays.asList("/api/system/health")
+ .forEach(url -> {
+ WsRequest request = new GetRequest("api/system/health")
+ .setHeader(PASSCODE_HEADER, VALID_PASSCODE);
+
+ WsResponse response = tester.asAnonymous().wsClient().wsConnector().call(request);
+ assertThat(response.code()).isEqualTo(200);
+ }
+ );
+ tester.settings().setGlobalSetting("sonar.forceAuthentication", "false");
+ }
+
+
private static GetRequest newRequest() {
return new GetRequest("api/system_passcode/check");
}