<commit_before>package org.xcolab.view.pages.loginregister;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.xcolab.client.members.MembersClient;
import org.xcolab.client.members.pojo.Member;
import org.xcolab.view.auth.AuthenticationContext;
import org.xcolab.view.util.entity.flash.AlertMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    private static final String LOGIN_VIEW_NAME = "loginregister/login";
    private static final String LOGIN_TOKEN_VIEW_NAME = "loginregister/loginWithToken";

    private final AuthenticationContext authenticationContext;

    @Autowired
    public LoginController(AuthenticationContext authenticationContext) {
        this.authenticationContext = authenticationContext;
    }

    @GetMapping("/login")
    public String login() {
        return LOGIN_VIEW_NAME;
    }

    @GetMapping("/login/token/{tokenId}")
    public String loginWithToken(HttpServletRequest request, HttpServletResponse response,
            Model model, @PathVariable String tokenId, @RequestParam String tokenKey) {
        model.addAttribute("tokenId", tokenId);
        model.addAttribute("tokenKey", tokenKey);
        return LOGIN_TOKEN_VIEW_NAME;
    }

    @PostMapping("/login/token/{tokenId}")
    public String doLoginWithToken(HttpServletRequest request, HttpServletResponse response,
            Model model, @PathVariable String tokenId, @RequestParam String tokenKey) {
        if (MembersClient.validateLoginToken(tokenId, tokenKey)) {
            AlertMessage.success("Login successful!").flash(request);
            final Member member = MembersClient.getMemberForLoginToken(tokenId);
            authenticationContext.authenticate(request, member);
            MembersClient.invalidateLoginToken(tokenId);<commit_msg>[COLAB-1937] Made login links re-usable<commit_after>package org.xcolab.view.pages.loginregister;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.xcolab.client.members.MembersClient;
import org.xcolab.client.members.pojo.Member;
import org.xcolab.view.auth.AuthenticationContext;
import org.xcolab.view.util.entity.flash.AlertMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    private static final String LOGIN_VIEW_NAME = "loginregister/login";
    private static final String LOGIN_TOKEN_VIEW_NAME = "loginregister/loginWithToken";

    private final AuthenticationContext authenticationContext;

    @Autowired
    public LoginController(AuthenticationContext authenticationContext) {
        this.authenticationContext = authenticationContext;
    }

    @GetMapping("/login")
    public String login() {
        return LOGIN_VIEW_NAME;
    }

    @GetMapping("/login/token/{tokenId}")
    public String loginWithToken(HttpServletRequest request, HttpServletResponse response,
            Model model, @PathVariable String tokenId, @RequestParam String tokenKey) {
        model.addAttribute("tokenId", tokenId);
        model.addAttribute("tokenKey", tokenKey);
        return LOGIN_TOKEN_VIEW_NAME;
    }

    @PostMapping("/login/token/{tokenId}")
    public String doLoginWithToken(HttpServletRequest request, HttpServletResponse response,
            Model model, @PathVariable String tokenId, @RequestParam String tokenKey) {
        if (MembersClient.validateLoginToken(tokenId, tokenKey)) {
            AlertMessage.success("Login successful!").flash(request);
            final Member member = MembersClient.getMemberForLoginToken(tokenId);
            authenticationContext.authenticate(request, member);
            //TODO: potentially make the links single-use
//            MembersClient.invalidateLoginToken(tokenId);
{'fraction_non_alphanumeric': 0.08848377056469542, 'fraction_numerical': 0.0008892841262783459, 'mean_word_length': 4.637844611528822, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3023026', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1144, 'n_words': 225}