-
Notifications
You must be signed in to change notification settings - Fork 214
/
SecurityFilter.java
80 lines (70 loc) · 3.32 KB
/
SecurityFilter.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
/*
* TeleStax, Open Source Cloud Communications
* Copyright 2011-2014, Telestax Inc and individual contributors
* by the @authors tag.
*
* This program is free software: you can redistribute it and/or modify
* 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.restcomm.connect.http.security;
import static javax.ws.rs.core.Response.status;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;
import org.apache.log4j.Logger;
import org.restcomm.connect.dao.AccountsDao;
import org.restcomm.connect.dao.DaoManager;
import org.restcomm.connect.dao.entities.Account;
import org.restcomm.connect.identity.UserIdentityContext;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
@Provider
public class SecurityFilter implements ContainerRequestFilter {
private final Logger logger = Logger.getLogger(SecurityFilter.class);
@Context
private HttpServletRequest servletRequest;
// We return Access-* headers only in case allowedOrigin is present and equals to the 'Origin' header.
@Override
public ContainerRequest filter(ContainerRequest cr) {
final DaoManager storage = (DaoManager) servletRequest.getServletContext().getAttribute(DaoManager.class.getName());
AccountsDao accountsDao = storage.getAccountsDao();
UserIdentityContext userIdentityContext = new UserIdentityContext(servletRequest, accountsDao);
checkAuthenticatedAccount(userIdentityContext);
filterClosedAccounts(userIdentityContext);
String scheme = cr.getAuthenticationScheme();
AccountPrincipal aPrincipal = new AccountPrincipal(userIdentityContext);
cr.setSecurityContext(new RCSecContext(aPrincipal, scheme));
return cr;
}
/**
* Grants general purpose access if any valid token exists in the request
*/
protected void checkAuthenticatedAccount(UserIdentityContext userIdentityContext) {
if (userIdentityContext.getEffectiveAccount() == null) {
throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED).header("WWW-Authenticate", "Basic realm=\"Restcomm realm\"").build());
}
}
/**
* filter out accounts that are not active
* @param userIdentityContext
*/
protected void filterClosedAccounts(UserIdentityContext userIdentityContext){
if(userIdentityContext.getEffectiveAccount() != null && !userIdentityContext.getEffectiveAccount().getStatus().equals(Account.Status.ACTIVE)){
throw new WebApplicationException(status(Status.FORBIDDEN).entity("Provided Account is not active").build());
}
}
}