<commit_before>package be.plutus.api.security;

import be.plutus.core.model.account.Account;
import be.plutus.core.model.token.Token;
import be.plutus.core.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;

@Component
public class TokenAuthenticationFilter extends GenericFilterBean{

    private static String PARAMETER_SECURITY_TOKEN = "token";
    private static String HEADER_SECURITY_TOKEN = "X-Auth-Token";

    @Autowired
    TokenService tokenService;

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

        String tokenHeader = request.getHeader( HEADER_SECURITY_TOKEN );
        String tokenParameter = request.getParameter( PARAMETER_SECURITY_TOKEN );

        if ( SecurityContextHolder.getContext().getAuthentication() == null) {

            Token token = null;

            if (tokenHeader != null)
                token = getToken(tokenHeader);

            if (tokenParameter != null)
                token = getToken(tokenParameter);

            if (isValid( token )) {
                tokenService.extendToken( token );
                tokenService.createRequest( request.getServletPath(), request.getMethod(), request.getRemoteAddr(), token );
                Account account = token.getAccount();
                SecurityContextHolder
                        .getContext()
                        .setAuthentication(
                                new UsernamePasswordAuthenticationToken(
                                        account.getEmail(),
                                        account.getPassword(),
                                        Collections.singletonList( ( () -> "ROLE_BASIC" ) )
                                )
                        );
            }
        }

        chain.doFilter(request, response);
    }

    private Token getToken( String tokenString ){
        return tokenService.getToken( tokenString );
    }

    private boolean isValid( Token token ){
        return token != null && token.isActive() && token.getExpiryDate().getTime() > new Date().getTime();
    }

}<commit_msg>Account has to be active to succeed authentication<commit_after>package be.plutus.api.security;

import be.plutus.core.model.account.Account;
import be.plutus.core.model.account.AccountStatus;
import be.plutus.core.model.token.Token;
import be.plutus.core.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;

@Component
public class TokenAuthenticationFilter extends GenericFilterBean{

    private static String PARAMETER_SECURITY_TOKEN = "token";
    private static String HEADER_SECURITY_TOKEN = "X-Auth-Token";

    @Autowired
    TokenService tokenService;

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

        String tokenHeader = request.getHeader( HEADER_SECURITY_TOKEN );
        String tokenParameter = request.getParameter( PARAMETER_SECURITY_TOKEN );

        if ( SecurityContextHolder.getContext().getAuthentication() == null) {

            Token token = null;

            if (tokenHeader != null)
                token = getToken(tokenHeader);

            if (tokenParameter != null)
                token = getToken(tokenParameter);

            if (isValid( token )) {
                tokenService.extendToken( token );
                tokenService.createRequest( request.getServletPath(), request.getMethod(), request.getRemoteAddr(), token );

                Account account = token.getAccount();

                if (isValid( account )) {
                    SecurityContextHolder
                            .getContext()
                            .setAuthentication(
                                    new UsernamePasswordAuthenticationToken(
                                            account.getEmail(),
                                            account.getPassword(),
                                            Collections.singletonList( ( () -> "ROLE_BASIC" ) )
                                    )
                            );
                }
            }
        }

        chain.doFilter(request, response);
    }

    private Token getToken( String tokenString ){
        return tokenService.getToken( tokenString );
    }

    private boolean isValid( Token token ){
        return token != null && token.isActive() && token.getExpiryDate().getTime() > new Date().getTime();
    }

    private boolean isValid( Account account ){
        return account != null && account.getStatus() == AccountStatus.ACTIVE;
    }
}
{'fraction_non_alphanumeric': 0.08598574821852732, 'fraction_numerical': 0.0, 'mean_word_length': 2.568361581920904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6203099', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1474, 'n_words': 318}