3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.authentication;
22 import com.google.common.base.Splitter;
23 import com.google.common.collect.ImmutableMap;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.EnumSet;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Locale;
31 import java.util.Optional;
32 import javax.annotation.CheckForNull;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import org.sonar.api.Startable;
36 import org.sonar.api.config.Configuration;
37 import org.sonar.api.server.authentication.Display;
38 import org.sonar.api.server.authentication.IdentityProvider;
39 import org.sonar.api.server.authentication.UserIdentity;
40 import org.sonar.api.utils.System2;
41 import org.sonar.api.utils.log.Logger;
42 import org.sonar.api.utils.log.Loggers;
43 import org.sonar.db.user.UserDto;
44 import org.sonar.process.ProcessProperties;
45 import org.sonar.server.authentication.UserRegistration.ExistingEmailStrategy;
46 import org.sonar.server.authentication.event.AuthenticationEvent;
47 import org.sonar.server.authentication.event.AuthenticationEvent.Source;
48 import org.sonar.server.authentication.event.AuthenticationException;
49 import org.sonar.server.exceptions.BadRequestException;
51 import static org.apache.commons.lang.time.DateUtils.addMinutes;
52 import static org.sonar.process.ProcessProperties.Property.SONAR_WEB_SSO_EMAIL_HEADER;
53 import static org.sonar.process.ProcessProperties.Property.SONAR_WEB_SSO_ENABLE;
54 import static org.sonar.process.ProcessProperties.Property.SONAR_WEB_SSO_GROUPS_HEADER;
55 import static org.sonar.process.ProcessProperties.Property.SONAR_WEB_SSO_LOGIN_HEADER;
56 import static org.sonar.process.ProcessProperties.Property.SONAR_WEB_SSO_NAME_HEADER;
57 import static org.sonar.process.ProcessProperties.Property.SONAR_WEB_SSO_REFRESH_INTERVAL_IN_MINUTES;
58 import static org.sonar.server.user.ExternalIdentity.SQ_AUTHORITY;
61 * Authentication based on the HTTP request headers. The front proxy
62 * is responsible for validating user identity.
64 public class HttpHeadersAuthentication implements Startable {
66 private static final Logger LOG = Loggers.get(HttpHeadersAuthentication.class);
68 private static final Splitter COMA_SPLITTER = Splitter.on(",").trimResults().omitEmptyStrings();
70 private static final String LAST_REFRESH_TIME_TOKEN_PARAM = "ssoLastRefreshTime";
72 private static final EnumSet<ProcessProperties.Property> PROPERTIES = EnumSet.of(
73 SONAR_WEB_SSO_LOGIN_HEADER,
74 SONAR_WEB_SSO_NAME_HEADER,
75 SONAR_WEB_SSO_EMAIL_HEADER,
76 SONAR_WEB_SSO_GROUPS_HEADER,
77 SONAR_WEB_SSO_REFRESH_INTERVAL_IN_MINUTES);
79 private final System2 system2;
80 private final Configuration config;
81 private final UserRegistrar userRegistrar;
82 private final JwtHttpHandler jwtHttpHandler;
83 private final AuthenticationEvent authenticationEvent;
84 private final Map<String, String> settingsByKey = new HashMap<>();
86 private boolean enabled = false;
88 public HttpHeadersAuthentication(System2 system2, Configuration config, UserRegistrar userRegistrar,
89 JwtHttpHandler jwtHttpHandler, AuthenticationEvent authenticationEvent) {
90 this.system2 = system2;
92 this.userRegistrar = userRegistrar;
93 this.jwtHttpHandler = jwtHttpHandler;
94 this.authenticationEvent = authenticationEvent;
99 if (config.getBoolean(SONAR_WEB_SSO_ENABLE.getKey()).orElse(false)) {
100 LOG.info("HTTP headers authentication enabled");
102 PROPERTIES.forEach(entry -> settingsByKey.put(entry.getKey(), config.get(entry.getKey()).orElse(entry.getDefaultValue())));
111 public Optional<UserDto> authenticate(HttpServletRequest request, HttpServletResponse response) {
113 return doAuthenticate(request, response);
114 } catch (BadRequestException e) {
115 throw AuthenticationException.newBuilder()
116 .setSource(Source.sso())
117 .setMessage(e.getMessage())
122 private Optional<UserDto> doAuthenticate(HttpServletRequest request, HttpServletResponse response) {
124 return Optional.empty();
126 Map<String, String> headerValuesByNames = getHeaders(request);
127 String login = getHeaderValue(headerValuesByNames, SONAR_WEB_SSO_LOGIN_HEADER.getKey());
129 return Optional.empty();
131 Optional<UserDto> user = getUserFromToken(request, response);
132 if (user.isPresent() && login.equals(user.get().getLogin())) {
136 UserDto userDto = doAuthenticate(headerValuesByNames, login);
137 jwtHttpHandler.generateToken(userDto, ImmutableMap.of(LAST_REFRESH_TIME_TOKEN_PARAM, system2.now()), request, response);
138 authenticationEvent.loginSuccess(request, userDto.getLogin(), Source.sso());
139 return Optional.of(userDto);
142 private Optional<UserDto> getUserFromToken(HttpServletRequest request, HttpServletResponse response) {
143 Optional<JwtHttpHandler.Token> token = jwtHttpHandler.getToken(request, response);
144 if (!token.isPresent()) {
145 return Optional.empty();
147 Date now = new Date(system2.now());
148 int refreshIntervalInMinutes = Integer.parseInt(settingsByKey.get(SONAR_WEB_SSO_REFRESH_INTERVAL_IN_MINUTES.getKey()));
149 Long lastFreshTime = (Long) token.get().getProperties().get(LAST_REFRESH_TIME_TOKEN_PARAM);
150 if (lastFreshTime == null || now.after(addMinutes(new Date(lastFreshTime), refreshIntervalInMinutes))) {
151 return Optional.empty();
153 return Optional.of(token.get().getUserDto());
156 private UserDto doAuthenticate(Map<String, String> headerValuesByNames, String login) {
157 String name = getHeaderValue(headerValuesByNames, SONAR_WEB_SSO_NAME_HEADER.getKey());
158 String email = getHeaderValue(headerValuesByNames, SONAR_WEB_SSO_EMAIL_HEADER.getKey());
159 UserIdentity.Builder userIdentityBuilder = UserIdentity.builder()
160 .setName(name == null ? login : name)
162 .setProviderLogin(login);
163 if (hasHeader(headerValuesByNames, SONAR_WEB_SSO_GROUPS_HEADER.getKey())) {
164 String groupsValue = getHeaderValue(headerValuesByNames, SONAR_WEB_SSO_GROUPS_HEADER.getKey());
165 userIdentityBuilder.setGroups(groupsValue == null ? Collections.emptySet() : new HashSet<>(COMA_SPLITTER.splitToList(groupsValue)));
167 return userRegistrar.register(
168 UserRegistration.builder()
169 .setUserIdentity(userIdentityBuilder.build())
170 .setProvider(new SsoIdentityProvider())
171 .setSource(Source.sso())
172 .setExistingEmailStrategy(ExistingEmailStrategy.FORBID)
177 private String getHeaderValue(Map<String, String> headerValuesByNames, String settingKey) {
178 return headerValuesByNames.get(settingsByKey.get(settingKey).toLowerCase(Locale.ENGLISH));
181 private static Map<String, String> getHeaders(HttpServletRequest request) {
182 Map<String, String> headers = new HashMap<>();
183 Collections.list(request.getHeaderNames()).forEach(header -> headers.put(header.toLowerCase(Locale.ENGLISH), request.getHeader(header)));
187 private boolean hasHeader(Map<String, String> headerValuesByNames, String settingKey) {
188 return headerValuesByNames.containsKey(settingsByKey.get(settingKey).toLowerCase(Locale.ENGLISH));
191 private static class SsoIdentityProvider implements IdentityProvider {
193 public String getKey() {
198 public String getName() {
203 public Display getDisplay() {
208 public boolean isEnabled() {
213 public boolean allowsUsersToSignUp() {