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
|
//--- BEGIN COPYRIGHT BLOCK ---
//This program 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; version 2 of the License.
//
//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 General Public License for more details.
//
//You should have received a copy of the GNU 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.
//
//(C) 2012 Red Hat, Inc.
//All rights reserved.
//--- END COPYRIGHT BLOCK ---
package org.dogtagpki.server.rest;
import java.io.IOException;
import java.security.Principal;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.ext.Provider;
import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.authentication.IAuthToken;
import com.netscape.certsrv.base.ForbiddenException;
import com.netscape.certsrv.base.SessionContext;
import com.netscape.cms.realm.PKIPrincipal;
import com.netscape.cms.servlet.base.UserInfo;
/**
* @author Endi S. Dewata
*/
@Provider
public class SessionContextInterceptor implements ContainerRequestFilter {
@Context
HttpServletRequest servletRequest;
@Context
SecurityContext securityContext;
public Locale getLocale(HttpServletRequest req) {
String lang = req.getHeader("accept-language");
if (lang == null)
return Locale.getDefault();
return new Locale(UserInfo.getUserLanguage(lang), UserInfo.getUserCountry(lang));
}
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
Principal principal = securityContext.getUserPrincipal();
// If unauthenticated, ignore.
if (principal == null) {
CMS.debug("SessionContextInterceptor: Not authenticated.");
SessionContext.releaseContext();
return;
}
CMS.debug("SessionContextInterceptor: principal: " + principal.getName());
// If unrecognized principal, reject request.
if (!(principal instanceof PKIPrincipal)) {
CMS.debug("SessionContextInterceptor: Invalid user principal.");
throw new ForbiddenException("Invalid user principal.");
}
PKIPrincipal pkiPrincipal = (PKIPrincipal) principal;
IAuthToken authToken = pkiPrincipal.getAuthToken();
// If missing auth token, reject request.
if (authToken == null) {
CMS.debug("SessionContextInterceptor: No authorization token present.");
throw new ForbiddenException("No authorization token present.");
}
SessionContext context = SessionContext.getContext();
String ip = servletRequest.getRemoteAddr();
context.put(SessionContext.IPADDRESS, ip);
Locale locale = getLocale(servletRequest);
context.put(SessionContext.LOCALE, locale);
context.put(SessionContext.AUTH_TOKEN, authToken);
context.put(SessionContext.USER_ID, pkiPrincipal.getName());
context.put(SessionContext.USER, pkiPrincipal.getUser());
}
}
|