source: DASISH/t5.6/backend/annotator-backend/branches/Jersey-2.12-adaptation/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/DummySecurityFilter.java @ 5739

Last change on this file since 5739 was 5739, checked in by olhsha@mpi.nl, 10 years ago

bug in AnnotationTest?: httpSevletRequest = null;

File size: 4.0 KB
Line 
1/*
2 * Copyright (C) 2013 DASISH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18package eu.dasish.annotation.backend.rest;
19
20/**
21 *
22 * @author olhsha
23 */
24import java.io.IOException;
25import java.security.Principal;
26import java.util.Arrays;
27import java.util.List;
28
29
30import javax.servlet.Filter;
31import javax.servlet.FilterChain;
32import javax.servlet.FilterConfig;
33import javax.servlet.ServletException;
34import javax.servlet.ServletRequest;
35import javax.servlet.ServletResponse;
36import javax.servlet.http.HttpServletRequest;
37import javax.servlet.http.HttpServletRequestWrapper;
38import javax.ws.rs.core.HttpHeaders;
39
40
41import org.glassfish.jersey.oauth1.signature.Base64;
42
43/**
44 * Dummy security filter, very handy for unit testing.
45 *
46 */
47public class DummySecurityFilter implements Filter {
48
49    private final List<String> ALLOWED_PRINCIPALS = Arrays.asList(DummyPrincipal.DUMMY_PRINCIPAL.getName());
50
51    /**
52     * Dummy validation for unit tests
53     *
54     * @param principalname
55     * @param password
56     * @return
57     */
58    private boolean isValid(String principalname, String password) {
59        return ALLOWED_PRINCIPALS.contains(principalname) && password.equals("olhapassword");
60    }
61
62    @Override
63    public void destroy() {
64    }
65
66    @Override
67    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
68        Principal principalResult = null;
69        HttpServletRequest req = (HttpServletRequest) request;
70        String authentication = req.getHeader(HttpHeaders.AUTHORIZATION);
71
72        if (authentication != null) { //if no authentication then do nothing
73            if (!authentication.startsWith("Basic ")) {
74                throw new ServletException("Only HTTP Basic authentication is supported");
75            }
76            authentication = authentication.substring("Basic ".length());
77            String base64Decode = new String(Base64.decode(authentication));
78            String[] values = base64Decode.split(":");
79            if (values.length < 2) {
80                throw new ServletException("Invalid syntax for principalname and password");
81            }
82            final String principalname = values[0];
83            String password = values[1];
84            if ((principalname == null) || (password == null)) {
85                throw new ServletException("Missing principalname or password");
86            }
87            if (!isValid(principalname, password)) {
88                throw new ServletException("Invalid principal/password");
89            }
90            principalResult = new DummyPrincipal(principalname);
91        }
92        final Principal principal = principalResult;
93        HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(req) {
94            public boolean isPrincipalInRole(String role) {
95                return true;
96            }
97
98            public boolean isSecure() {
99                return false;
100            }
101
102            public Principal getPrincipalPrincipal() {
103                return principal;
104            }
105
106            @Override
107            public String getAuthType() {
108                return HttpServletRequest.BASIC_AUTH;
109            }
110
111            @Override
112            public String getRemoteUser() {
113                return principal.getName();
114            }
115        };
116
117        chain.doFilter(wrapper, response);
118    }
119
120    @Override
121    public void init(FilterConfig filterConfig) throws ServletException {
122    }
123}
Note: See TracBrowser for help on using the repository browser.