Repository navigation
Expand file tree
/
Copy pathACS.java
More file actions
185 lines (161 loc) · 6.7 KB
/
Copy pathACS.java
File metadata and controls
185 lines (161 loc) · 6.7 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package servlets;
import dbProcs.Getter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.pac4j.saml.profile.SAML2Profile;
import utils.Hash;
import utils.LoginMethod;
import utils.SamlSso;
import utils.ShepherdLogManager;
import utils.SsoUserMapper;
import utils.UserKicker;
/**
* Control class for the authentication procedure. <br>
* <br>
* This file is part of the Security Shepherd Project.
*
* <p>The Security Shepherd project is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.<br>
*
* <p>The Security Shepherd project 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 General Public License for more details.<br>
*
* <p>You should have received a copy of the GNU General Public License along with the Security
* Shepherd project. If not, see <http://www.gnu.org/licenses/>.
*
* @author Mark Denihan
*/
public class ACS extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger log = LogManager.getLogger(ACS.class);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (!LoginMethod.isSaml()) {
// SSO is off (sso.enabled in sso.properties): this endpoint must not authenticate anyone,
// even if a SAML configuration is present
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
ShepherdLogManager.setRequestIp(request.getRemoteAddr(), request.getHeader("X-Forwarded-For"));
log.debug("**** servlets.ACS ***");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
HttpSession ses = request.getSession(true);
Object language = ses.getAttribute("lang");
response.setContentType("text/plain");
log.debug("Getting ApplicationRoot");
String ApplicationRoot = getServletContext().getRealPath("");
log.debug("Servlet root = " + ApplicationRoot);
log.debug("Processing SAML data...");
Optional<SAML2Profile> profile = SamlSso.authenticate(request, response);
log.debug("SAML data processed");
if (!profile.isPresent()) {
log.debug("User not authenticated");
ses.setAttribute("loginFailed", "Not authenticated");
response.sendRedirect("../login.jsp");
log.debug("**** End servlets.ACS ***");
return;
}
log.debug("User authenticated, unpacking SAML attributes...");
SsoUserMapper.Result ssoUser =
SsoUserMapper.map(profile.get().getAttributes(), loadSsoProperties());
String errorMessage = "";
boolean loggedIn = false;
if (ssoUser.isValid()) {
log.debug("ssoName = " + ssoUser.ssoName);
log.debug("userName = " + ssoUser.userName);
log.debug("userRole = " + ssoUser.userRole);
log.debug("Saml userdata loaded, calling authUserSSO");
String user[] =
Getter.authUserSSO(
ApplicationRoot, null, ssoUser.userName, ssoUser.ssoName, ssoUser.userRole);
if (user != null && !user[0].isEmpty()) {
// Kill Session and Create a new one with user logged in
log.debug("Creating new session for " + user[2] + " " + user[1]);
ses.invalidate();
ses = request.getSession(true);
ses.setAttribute("userStamp", user[0]);
ses.setAttribute("userName", user[1]);
ses.setAttribute("userRole", user[2]);
ses.setAttribute("lang", language);
// Kept for single logout, which needs the NameID and session index
ses.setAttribute(SamlSso.PROFILE_SESSION_ATTRIBUTE, profile.get());
// Lets an IdP-initiated logout end this session
SamlSso.registerLoginSession(ses, profile.get());
log.debug("userClassId = " + user[4]);
ses.setAttribute("userClass", user[4]);
if (user[5].equalsIgnoreCase("true")) {
log.debug("Temporary Username Detected, user will be prompted to change");
ses.setAttribute("ChangeUsername", "true");
}
log.debug("Setting CSRF cookie");
Cookie token = new Cookie("token", Hash.randomString());
if (request.getRequestURL().toString().startsWith("https")) // If Requested over HTTPs
{
token.setSecure(true);
}
// We must set the path because the ACS servlet is in a subdir...
token.setPath("/");
response.addCookie(token);
// Removing user from kick list. If they were on it before, their suspension
// must have ended if their authentication Succeeded
UserKicker.removeFromKicklist(user[1]);
loggedIn = true;
}
} else {
log.debug("SSO attributes rejected: " + ssoUser.error);
errorMessage = ssoUser.error;
}
if (loggedIn) {
response.sendRedirect("../index.jsp");
} else {
log.debug("Could not authenticate");
ses.setAttribute("loginFailed", errorMessage + "SSO login failed.");
// Lagging Response
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
response.sendRedirect("../login.jsp");
}
log.debug("**** End servlets.ACS ***");
}
/** Redirects user to index.jsp */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("../index.jsp");
}
private Properties loadSsoProperties() {
String unpackFileName = "sso.properties";
log.debug("Loading saml unpack properties file");
try (InputStream inputStream =
getClass().getClassLoader().getResourceAsStream(unpackFileName)) {
if (inputStream == null) {
String errorMsg =
"SAML unpack properties file '" + unpackFileName + "' not found in the classpath";
log.error(errorMsg);
throw new RuntimeException(errorMsg);
}
Properties prop = new Properties();
prop.load(inputStream);
return prop;
} catch (IOException e) {
String errorMsg = "SAML unpack properties file '" + unpackFileName + "' cannot be loaded";
log.error(errorMsg);
throw new RuntimeException(errorMsg);
}
}
}