package com.quizdeck.filters;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureException;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Brandon on 2/10/2016.
 */
public class AuthFilter implements Filter {
    public static final String CLAIMS_ATTRIBUTE = "claims";

    public AuthFilter(String secretKey) {
        this.secretKey = secretKey;
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;

        String header = request.getHeader("Authorization");
        if(header == null || !header.startsWith("Bearer ")) {
            throw new ServletException("Missing or invalid Authorization header.");
        }

        try {
            String token = header.substring(7); // The part after "Bearer "

            Claims claims = Jwts
                    .parser()
                    .setSigningKey(this.secretKey)
                    .parseClaimsJws(token)
                    .getBody();

            request.setAttribute(CLAIMS_ATTRIBUTE, claims);
        }
        catch(SignatureException e) {
            throw new ServletException("Invalid token.");
        }

        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }

    private final String secretKey;
}

@@ -107,34 +107,226 @@
 ebtoken.
-SignatureException
+MalformedJwtException;%0Aimport io.jsonwebtoken.SignatureException;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport org.springframework.http.HttpHeaders;%0Aimport org.springframework.http.HttpStatus
 ;%0A%0Aimpor
@@ -390,16 +390,63 @@
 equest;%0A
+import javax.servlet.http.HttpServletResponse;%0A
 import j
@@ -900,16 +900,82 @@
 st) req;
+%0A        HttpServletResponse response = (HttpServletResponse) res;
 %0A%0A      
@@ -1014,26 +1014,37 @@
 der(
-%22Authorization%22
+HttpHeaders.AUTHORIZATION
 );%0A
+%0A
     
@@ -1117,78 +1117,282 @@
     
-throw new ServletException(%22Missing or invalid Authorization header.%22)
+response.setStatus(HttpStatus.UNAUTHORIZED.value());%0A            response.getWriter().write(%22You are not authenticated, please log in and try again.%22);%0A%0A            log.warn(%22Unauthorized request to: %22 + request.getRequestURL() + %22 Cause: no auth token%22);%0A            return
 ;%0A  
@@ -1765,16 +1765,40 @@
   catch(
+MalformedJwtException %7C 
 Signatur
@@ -1829,37 +1829,250 @@
     
-throw new ServletException(%22I
+response.setStatus(HttpStatus.UNAUTHORIZED.value());%0A            response.getWriter().write(%22Your login token is invalid, please log in and try again.%22);%0A%0A            log.warn(%22Unauthorized request to: %22 + request.getRequestURL() + %22 Cause: i
 nval
@@ -2079,21 +2079,40 @@
 id token
-.
 %22);%0A
+            return;%0A
         
@@ -2311,10 +2311,91 @@
 retKey;%0A
+    private static final Logger log = LoggerFactory.getLogger(AuthFilter.class);%0A
 %7D%0A

Make Auth filter smarter