-
Notifications
You must be signed in to change notification settings - Fork 539
/
LoginServlet.java
149 lines (124 loc) · 5.4 KB
/
LoginServlet.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
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
package io.hawt.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.Principal;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.security.auth.Subject;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import io.hawt.system.ConfigManager;
import io.hawt.system.Helpers;
import org.jolokia.converter.Converters;
import org.jolokia.converter.json.JsonConvertOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final transient Logger LOG = LoggerFactory.getLogger(LoginServlet.class);
private static final int DEFAULT_SESSION_TIMEOUT = 1800;
protected Converters converters = new Converters();
protected JsonConvertOptions options = JsonConvertOptions.DEFAULT;
protected ConfigManager config;
private Integer timeout = DEFAULT_SESSION_TIMEOUT;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
config = (ConfigManager) servletConfig.getServletContext().getAttribute("ConfigManager");
if (config != null) {
String s = config.get("sessionTimeout", "" + DEFAULT_SESSION_TIMEOUT);
if (s != null) {
try {
timeout = Integer.parseInt(s);
// timeout of 0 means default timeout
if (timeout == 0) {
timeout = DEFAULT_SESSION_TIMEOUT;
}
} catch (Exception e) {
// ignore and use our own default of 1/2 hour
timeout = DEFAULT_SESSION_TIMEOUT;
}
}
}
LOG.info("hawtio login is using " + (timeout != null ? timeout + " sec." : "default") + " HttpSession timeout");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json");
final PrintWriter out = resp.getWriter();
HttpSession session = req.getSession(false);
if (session != null) {
Subject subject = (Subject) session.getAttribute("subject");
if (subject == null) {
LOG.warn("No security subject stored in existing session, invalidating");
session.invalidate();
Helpers.doForbidden(resp);
return;
}
sendResponse(session, subject, out);
return;
}
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
if (subject == null) {
Helpers.doForbidden(resp);
return;
}
Set<Principal> principals = subject.getPrincipals();
String username = null;
if (principals != null) {
for (Principal principal : principals) {
// Should be name of class configurable like for Role principal?
String principalClass = principal.getClass().getSimpleName();
if (principalClass.equals("UserPrincipal") || principalClass.equals("KeycloakPrincipal")) {
username = principal.getName();
LOG.debug("Authorizing user {}", username);
}
}
}
session = req.getSession(true);
session.setAttribute("subject", subject);
session.setAttribute("user", username);
session.setAttribute("org.osgi.service.http.authentication.remote.user", username);
session.setAttribute("org.osgi.service.http.authentication.type", HttpServletRequest.BASIC_AUTH);
session.setAttribute("loginTime", GregorianCalendar.getInstance().getTimeInMillis());
if (timeout != null) {
session.setMaxInactiveInterval(timeout);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Http session timeout for user {} is {} sec.", username, session.getMaxInactiveInterval());
}
sendResponse(session, subject, out);
}
protected void sendResponse(HttpSession session, Subject subject, PrintWriter out) {
Map<String, Object> answer = new HashMap<String, Object>();
List<Object> principals = new ArrayList<Object>();
for (Principal principal : subject.getPrincipals()) {
Map<String, String> data = new HashMap<String, String>();
data.put("type", principal.getClass().getName());
data.put("name", principal.getName());
principals.add(data);
}
List<Object> credentials = new ArrayList<Object>();
for (Object credential : subject.getPublicCredentials()) {
Map<String, Object> data = new HashMap<String, Object>();
data.put("type", credential.getClass().getName());
data.put("credential", credential);
}
answer.put("principals", principals);
answer.put("credentials", credentials);
ServletHelpers.writeObject(converters, options, out, answer);
}
}