Skip to content
Snippets Groups Projects
Commit 656e1fea authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Autoformatted

parent bcca20a4
Branches
No related tags found
No related merge requests found
/* /*
* Copyright (c) 2022 NIBIO <http://www.nibio.no/>. * Copyright (c) 2022 NIBIO <http://www.nibio.no/>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
* it under the terms of the GNU Affero General Public License as published by * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* the Free Software Foundation, either version 3 of the License, or * later version.
* (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* but WITHOUT ANY WARRANTY; without even the implied warranty of * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * details.
* GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License along with this program. If not, see
* along with this program. If not, see <https://www.gnu.org/licenses/>. * <https://www.gnu.org/licenses/>.
* *
*/ */
...@@ -23,7 +21,12 @@ import java.io.IOException; ...@@ -23,7 +21,12 @@ import java.io.IOException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.UUID; import java.util.UUID;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.servlet.*; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -34,79 +37,71 @@ import no.nibio.vips.util.ServletUtil; ...@@ -34,79 +37,71 @@ import no.nibio.vips.util.ServletUtil;
/** /**
* Ensures that user accessing a restricted resource is actually logged in. Redirects to login page if not * Ensures that user accessing a restricted resource is actually logged in. Redirects to login page if not
*
* @copyright 2013-2022 <a href="http://www.nibio.no">NIBIO</a> * @copyright 2013-2022 <a href="http://www.nibio.no">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no> * @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/ */
public class AuthenticationFilter implements Filter{ public class AuthenticationFilter implements Filter {
@EJB @EJB
UserBean userBean; UserBean userBean;
// The URLs that do not require login // The URLs that do not require login
private String[] unprivilegedURLs; private String[] unprivilegedURLs;
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) throws ServletException {
this.setUnprivilegedURLs(Globals.UNPRIVILEGED_URLS); this.setUnprivilegedURLs(Globals.UNPRIVILEGED_URLS);
} }
@Override @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request; HttpServletRequest httpRequest = (HttpServletRequest) request;
/* /*
// For debugging * // For debugging BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); * String line; while((line = reader.readLine()) != null) { System.out.println(line); }
String line; */
while((line = reader.readLine()) != null) if (isUnprivilegedURL(httpRequest)) {
{
System.out.println(line);
}*/
if(isUnprivilegedURL(httpRequest))
{
chain.doFilter(request, response); chain.doFilter(request, response);
//return; System.out.println("Uprivileged URL: " + httpRequest.getRequestURI());
} // return;
else } else {
{ System.out.println("Privileged URL: " + httpRequest.getRequestURI());
// First: Check for session variable // First: Check for session variable
boolean clientAuthenticated = (httpRequest.getSession().getAttribute("user") != null && httpRequest.getSession().getAttribute("user") instanceof VipsLogicUser); boolean clientAuthenticated = (httpRequest.getSession().getAttribute("user") != null
&& httpRequest.getSession().getAttribute("user") instanceof VipsLogicUser);
// Then for UUID cookie that has not expired // Then for UUID cookie that has not expired
boolean clientRemembered = false; boolean clientRemembered = false;
Cookie remembered = ServletUtil.getCookie(httpRequest, "rememberedUser"); Cookie remembered = ServletUtil.getCookie(httpRequest, "rememberedUser");
if(remembered != null) if (remembered != null) {
{
VipsLogicUser user = userBean.findVipsLogicUser(UUID.fromString(remembered.getValue())); VipsLogicUser user = userBean.findVipsLogicUser(UUID.fromString(remembered.getValue()));
if(user != null) if (user != null) {
{
httpRequest.getSession().setAttribute("user", user); httpRequest.getSession().setAttribute("user", user);
clientRemembered = true; clientRemembered = true;
} }
} }
if(! clientAuthenticated && ! clientRemembered) if (!clientAuthenticated && !clientRemembered) {
{
String nextPageDirective = ""; String nextPageDirective = "";
if(!httpRequest.getServletPath().equals("/login")) if (!httpRequest.getServletPath().equals("/login")) {
{
String nextPage = ServletUtil.getFullRequestURI(httpRequest); String nextPage = ServletUtil.getFullRequestURI(httpRequest);
nextPageDirective= "?nextPage=" + URLEncoder.encode(nextPage, "UTF-8"); nextPageDirective = "?nextPage=" + URLEncoder.encode(nextPage, "UTF-8");
} }
((HttpServletResponse)response).sendRedirect(Globals.PROTOCOL + "://" + ServletUtil.getServerName(httpRequest) + "/login" + nextPageDirective); ((HttpServletResponse) response).sendRedirect(Globals.PROTOCOL + "://"
} + ServletUtil.getServerName(httpRequest) + "/login" + nextPageDirective);
else } else {
{
chain.doFilter(request, response); chain.doFilter(request, response);
} }
//return; // return;
} }
} }
private boolean isUnprivilegedURL(HttpServletRequest request) { private boolean isUnprivilegedURL(HttpServletRequest request) {
String path = request.getServletPath(); String path = request.getServletPath();
for (String unprivilegedURL : this.getUnprivilegedURLs()) for (String unprivilegedURL : this.getUnprivilegedURLs()) {
{ if (path.contains(unprivilegedURL)) {
if (path.contains(unprivilegedURL))
{
return true; return true;
} }
} }
...@@ -115,12 +110,10 @@ public class AuthenticationFilter implements Filter{ ...@@ -115,12 +110,10 @@ public class AuthenticationFilter implements Filter{
@Override @Override
public void destroy() { public void destroy() {
} }
/** /**
* @return the upriviligerteURLer * @return the upriviligerteURLer
...@@ -136,6 +129,6 @@ public class AuthenticationFilter implements Filter{ ...@@ -136,6 +129,6 @@ public class AuthenticationFilter implements Filter{
this.unprivilegedURLs = unprivilegedURLs; this.unprivilegedURLs = unprivilegedURLs;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment