<commit_before>package outout.controller;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import outout.model.User;
import outout.view.AccountCredentials;
import outout.view.AuthenticationToken;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

@Controller
@RequestMapping("/authenticate")
public class AuthenticationController {

    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Value("${token.secret}")
    private String tokenSecret;

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<AuthenticationToken> authenticate(@RequestBody AccountCredentials accountCredentials) {
        if(areCredentialsValid(accountCredentials)) {
            AuthenticationToken authenticationToken = new AuthenticationToken();
            authenticationToken.setToken(buildToken(accountCredentials));
            return new ResponseEntity<>(authenticationToken, HttpStatus.OK);
        }
        else {
            return new ResponseEntity<AuthenticationToken>(HttpStatus.NOT_FOUND);
        }
    }

    private boolean areCredentialsValid(final AccountCredentials accountCredentials) {
        User user = findUserByUsername(accountCredentials.getUsername());
        return user != null && passwordEncoder.matches(accountCredentials.getPassword(), user.getPassword());
    }

    private User findUserByUsername(String username) {
        Query query = entityManager.createQuery("select u from User u where u.username = :username");
        query.setParameter("username", username);
        query.setMaxResults(1);<commit_msg>Cleaning up some generic usage.<commit_after>package outout.controller;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import outout.model.User;
import outout.view.AccountCredentials;
import outout.view.AuthenticationToken;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

@Controller
@RequestMapping("/authenticate")
public class AuthenticationController {

    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Value("${token.secret}")
    private String tokenSecret;

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<AuthenticationToken> authenticate(@RequestBody AccountCredentials accountCredentials) {
        if(areCredentialsValid(accountCredentials)) {
            AuthenticationToken authenticationToken = new AuthenticationToken();
            authenticationToken.setToken(buildToken(accountCredentials));
            return new ResponseEntity<>(authenticationToken, HttpStatus.OK);
        }
        else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    private boolean areCredentialsValid(final AccountCredentials accountCredentials) {
        User user = findUserByUsername(accountCredentials.getUsername());
        return user != null && passwordEncoder.matches(accountCredentials.getPassword(), user.getPassword());
    }

    private User findUserByUsername(String username) {
        Query query = entityManager.createQuery("select u from User u where u.username = :username");
        query.setParameter("username", username);
        query.setMaxResults(1);
{'fraction_non_alphanumeric': 0.08670520231213873, 'fraction_numerical': 0.00042817383857846286, 'mean_word_length': 5.051813471502591, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5999009', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1129, 'n_words': 221}