-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
AuthenticationEventImpl.java
79 lines (70 loc) · 2.96 KB
/
AuthenticationEventImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.authentication.event;
import com.google.common.base.Joiner;
import java.util.Collections;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.core.util.stream.Collectors;
import static java.util.Objects.requireNonNull;
public class AuthenticationEventImpl implements AuthenticationEvent {
private static final Logger LOGGER = Loggers.get("auth.event");
private static final int FLOOD_THRESHOLD = 128;
@Override
public void login(HttpServletRequest request, @Nullable String login, Source source) {
requireNonNull(request, "request can't be null");
requireNonNull(source, "source can't be null");
if (!LOGGER.isDebugEnabled()) {
return;
}
LOGGER.debug("login success [method|{}][provider|{}|{}][IP|{}|{}][login|{}]",
source.getMethod(), source.getProvider(), source.getProviderName(),
request.getRemoteAddr(), getAllIps(request),
preventLogFlood(emptyIfNull(login)));
}
private static String getAllIps(HttpServletRequest request) {
return Collections.list(request.getHeaders("X-Forwarded-For")).stream().collect(Collectors.join(Joiner.on(",")));
}
@Override
public void failure(HttpServletRequest request, AuthenticationException e) {
requireNonNull(request, "request can't be null");
requireNonNull(e, "AuthenticationException can't be null");
if (!LOGGER.isDebugEnabled()) {
return;
}
Source source = e.getSource();
LOGGER.debug("login failure [cause|{}][method|{}][provider|{}|{}][IP|{}|{}][login|{}]",
emptyIfNull(e.getMessage()),
source.getMethod(), source.getProvider(), source.getProviderName(),
request.getRemoteAddr(), getAllIps(request),
preventLogFlood(emptyIfNull(e.getLogin())));
}
private static String emptyIfNull(@Nullable String login) {
return login == null ? "" : login;
}
private static String preventLogFlood(String str) {
if (str.length() > FLOOD_THRESHOLD) {
return str.substring(0, FLOOD_THRESHOLD) + "...(" + str.length() + ")";
}
return str;
}
}