-
Notifications
You must be signed in to change notification settings - Fork 131
/
RequestUser.java
101 lines (85 loc) · 3.57 KB
/
RequestUser.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
/* dCache - http://www.dcache.org/
*
* Copyright (C) 2001 - 2017 Deutsches Elektronen-Synchrotron
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dcache.restful.util;
import static com.google.common.base.Preconditions.checkState;
import java.io.IOException;
import java.security.AccessController;
import javax.inject.Inject;
import javax.security.auth.Subject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import org.dcache.auth.Subjects;
import org.dcache.auth.attributes.Restriction;
/**
* Utility class to handle the identity of the user issuing the request. It also acts as a JAX-RS
* Filter to populate thread-local to avoid having to work with HttpServletRequest objects.
*/
public class RequestUser implements ContainerRequestFilter, ContainerResponseFilter {
private static final ThreadLocal<Restriction> RESTRICTIONS = new ThreadLocal<>();
private static final ThreadLocal<Boolean> IS_ADMIN = new ThreadLocal<>();
@Inject
private HttpServletRequest request;
public RequestUser() {
// Allow instantiation as a JAX-RS Filter.
}
public static Subject getSubject() {
return Subject.getSubject(AccessController.getContext());
}
public static boolean isAnonymous() {
return Subjects.isNobody(getSubject());
}
public static Restriction getRestriction() {
Restriction restriction = RESTRICTIONS.get();
checkState(restriction != null,
"RequestUser#getRestriction called outside of REST request");
return restriction;
}
public static boolean isAdmin() {
Boolean isAdmin = IS_ADMIN.get();
checkState(isAdmin != null, "RequestUser#isAdmin called outside of REST request");
return isAdmin;
}
public static boolean canViewFileOperations(boolean isUnlimitedVisibility) {
return RequestUser.isAdmin() || isUnlimitedVisibility;
}
public static Long getSubjectUidForFileOperations(boolean isUnlimitedVisibility) {
if (canViewFileOperations(isUnlimitedVisibility)) {
return null;
}
Subject user = getSubject();
if (Subjects.isNobody(user)) {
throw new NotAuthorizedException("visibility limited to non-anonymous users.");
}
return Subjects.getUid(user);
}
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
RESTRICTIONS.set(HttpServletRequests.getRestriction(request));
IS_ADMIN.set(HttpServletRequests.isAdmin(request));
}
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
RESTRICTIONS.set(null);
IS_ADMIN.set(null);
}
}