/
HttpHeaderAuthModule.java
213 lines (192 loc) · 6.75 KB
/
HttpHeaderAuthModule.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package net.trajano.auth;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.message.AuthException;
import javax.security.auth.message.AuthStatus;
import javax.security.auth.message.MessageInfo;
import javax.security.auth.message.MessagePolicy;
import javax.security.auth.message.callback.CallerPrincipalCallback;
import javax.security.auth.message.callback.GroupPrincipalCallback;
import javax.security.auth.message.config.ServerAuthContext;
import javax.security.auth.message.module.ServerAuthModule;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* HTTP header server auth module. This can be used with SiteMinder type data.
*
* @author Archimedes Trajano
*/
public class HttpHeaderAuthModule implements ServerAuthModule, ServerAuthContext {
/**
* Group Header option key.
*/
private static final String GROUP_HEADER_KEY = "group_header";
/**
* Logger.
*/
private static final Logger LOG;
/**
* Messages resource path.
*/
private static final String MESSAGES = "META-INF/Messages";
/**
* Resource bundle.
*/
private static final ResourceBundle R;
/**
* User Name Header option key.
*/
public static final String USERNAME_HEADER_KEY = "username_header";
static {
LOG = Logger.getLogger("net.trajano.auth.httpheadersam", MESSAGES);
R = ResourceBundle.getBundle(MESSAGES);
}
/**
* Group header option.
*/
private String groupHeader;
/**
* Callback handler that is passed in initialize by the container. This
* processes the callbacks which are objects that populate the "subject".
*/
private CallbackHandler handler;
/**
* Mandatory flag.
*/
private boolean mandatory;
/**
* User name header option.
*/
private String userNameHeader;
/**
* Does nothing.
*
* @param messageInfo
* message info
* @param subject
* subject
*/
@Override
public void cleanSubject(final MessageInfo messageInfo, final Subject subject) throws AuthException {
// Does nothing.
}
/**
* <p>
* Supported message types. For our case we only need to deal with HTTP
* servlet request and responses. On Java EE 7 this will handle WebSockets
* as well.
* </p>
* <p>
* This creates a new array for security at the expense of performance.
* </p>
*
* @return {@link HttpServletRequest} and {@link HttpServletResponse}
* classes.
*/
@SuppressWarnings("rawtypes")
@Override
public Class[] getSupportedMessageTypes() {
return new Class<?>[] { HttpServletRequest.class, HttpServletResponse.class };
}
/**
* Builds a list of groups from the request. If not defined, then "users" is
* returned.
*
* @param req
* servlet request.
* @return array of groups.
*/
private String[] groups(final HttpServletRequest req) {
if (groupHeader == null) {
return new String[] { "users" };
}
final List<String> groupList = new LinkedList<>();
final Enumeration<String> groupHeaders = req.getHeaders(groupHeader);
while (groupHeaders.hasMoreElements()) {
for (final String groupName : groupHeaders.nextElement()
.split(",")) {
groupList.add(groupName.trim());
}
}
return groupList.toArray(new String[0]);
}
/**
* {@inheritDoc}
*
* @param requestPolicy
* request policy, ignored
* @param responsePolicy
* response policy, ignored
* @param h
* callback handler
* @param options
* options
*/
@Override
public void initialize(final MessagePolicy requestPolicy, final MessagePolicy responsePolicy, final CallbackHandler h, @SuppressWarnings("rawtypes") final Map options) throws AuthException {
handler = h;
mandatory = requestPolicy.isMandatory();
userNameHeader = (String) options.get(USERNAME_HEADER_KEY);
if (userNameHeader == null) {
LOG.log(Level.SEVERE, "missingOption", USERNAME_HEADER_KEY);
throw new AuthException(MessageFormat.format(R.getString("missingOption"), USERNAME_HEADER_KEY));
}
groupHeader = (String) options.get(GROUP_HEADER_KEY);
}
/**
* Return {@link AuthStatus#SEND_SUCCESS}.
*
* @param messageInfo
* contains the request and response messages. At this point the
* response message is already committed so nothing can be
* changed.
* @param subject
* subject.
* @return {@link AuthStatus#SEND_SUCCESS}
*/
@Override
public AuthStatus secureResponse(final MessageInfo messageInfo, final Subject subject) throws AuthException {
return AuthStatus.SEND_SUCCESS;
}
/**
* {@inheritDoc}
*/
@Override
public AuthStatus validateRequest(final MessageInfo messageInfo, final Subject client, final Subject serviceSubject) throws AuthException {
final HttpServletRequest req = (HttpServletRequest) messageInfo.getRequestMessage();
final HttpServletResponse resp = (HttpServletResponse) messageInfo.getResponseMessage();
try {
if (!mandatory && !req.isSecure()) {
return AuthStatus.SUCCESS;
}
if (!req.isSecure()) {
resp.sendError(HttpURLConnection.HTTP_FORBIDDEN, R.getString("SSLReq"));
return AuthStatus.SEND_FAILURE;
}
final String userName = req.getHeader(userNameHeader);
if (userName == null && mandatory) {
return AuthStatus.FAILURE;
} else if (userName == null && !mandatory) {
return AuthStatus.SUCCESS;
}
handler.handle(new Callback[] { new CallerPrincipalCallback(client, userName), new GroupPrincipalCallback(client, groups(req)) });
return AuthStatus.SUCCESS;
} catch (final IOException | UnsupportedCallbackException e) {
LOG.throwing(this.getClass()
.getName(), "validateRequest", e);
throw new AuthException(e.getMessage());
}
}
}