<commit_before>package halleck.webserver.routs;

import com.google.inject.Inject;
import halleck.api.Settings;
import halleck.webserver.*;
import spark.ModelAndView;
import spark.Request;
import spark.Response;

import java.util.Map;
import java.util.Objects;

import static halleck.webserver.MapMaker.map;

public class AuthenticationRouts extends SparkRoutCollector {

    private final Settings settings;
    private final Authenticator auth;


    @Inject
    public AuthenticationRouts(Settings settings,
                               Authenticator auth) {

        this.settings = settings;
        this.auth = auth;
    }

    public void init(){
        get(new FullPage("/login") {
            @Override
            public ModelMapView action(Request request, Response response) {
                return new ModelMapView(getSettings(),"login.mustache");
            }

            private Map getSettings() {
                return map("showpass", !Objects.equals(settings.getAuthenticationType(), "fake"));
            }
        });

        post(new FullPage("/login") {
            @Override
            public ModelMapView action(Request request, Response response) {
                String username = request.queryParams("username");
                String password = request.queryParams("password");

                if(auth.authenticate(username, password)){
                    response.cookie(SecurityFilter.USERNAME_COOKIE, username);
                    return new ModelMapView(map("url", "/"), "redirect.mustache");
                }
                return new ModelMapView(map("wrong", true), "login.mustache");
            }
        });

        get(new FullPage("/logout") {
            @Override
            public ModelMapView action(Request request, Response response) {
                response.removeCookie(SecurityFilter.USERNAME_COOKIE);
                response.redirect("/login");
                return null;
            }
        });
    }<commit_msg>add lambdas for authentication<commit_after>package halleck.webserver.routs;

import com.google.inject.Inject;
import halleck.api.Settings;
import halleck.webserver.*;
import spark.ModelAndView;
import spark.Request;
import spark.Response;

import java.util.Map;
import java.util.Objects;

import static halleck.webserver.MapMaker.map;

public class AuthenticationRouts extends SparkRoutCollector {

    private final Settings settings;
    private final Authenticator auth;


    @Inject
    public AuthenticationRouts(Settings settings,
                               Authenticator auth) {

        this.settings = settings;
        this.auth = auth;
    }

    public void init(){
        get( "/login", (q,p) -> new ModelMapView(getSettings(),"login.mustache"));
        post("/login", (q,p) -> loginAction(q, p));
        get("/logout", (q,p) -> logoutAction(p));
    }

    private ModelMapView logoutAction(Response response) {
        response.removeCookie(SecurityFilter.USERNAME_COOKIE);
        response.redirect("/login");
        return null;
    }

    private ModelMapView loginAction(Request request, Response response) {
        String username = request.queryParams("username");
        String password = request.queryParams("password");

        if(auth.authenticate(username, password)){
            response.cookie(SecurityFilter.USERNAME_COOKIE, username);
            return new ModelMapView(map("url", "/"), "redirect.mustache");
        }
        return new ModelMapView(map("wrong", true), "login.mustache");
    }

    private Map getSettings() {
        return map("showpass", !Objects.equals(settings.getAuthenticationType(), "fake"));
    }
{'fraction_non_alphanumeric': 0.10801203171998906, 'fraction_numerical': 0.0, 'mean_word_length': 2.544573643410853, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2403158', 'n_tokens_mistral': 963, 'n_tokens_neox': 926, 'n_words': 209}