<commit_before>package m11.mib.paf.quiz;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;

import m11.mib.paf.quiz.user.User;
import m11.mib.paf.quiz.user.UserRepository;

/**
 * MT \ 22.01.2017 \ AuthenticationController
 * 
 *
 * @author M11
 * @version 1.0
 */
@RestController
public class AuthenticationController {

    private UserRepository userRepository;
    
    /**
     * @param userRepository 
     */
    @Autowired
    public AuthenticationController(UserRepository userRepository) {
	this.userRepository = userRepository;
    }
    
    /**
     * REST-Endpoint for logging into the quiz.
     * Takes the user credentials and tries to verify, that the user exists and the password is correct.
     * At the end a JSON-Web-Token is generated with the users legitimate claim and then served as response.
     * 
     * @param userId
     * @param password
     * @return the JSON-Web-Token packed into a JSON
     */
    @RequestMapping(value = "/api/auth/login")
    public HttpEntity<User> login(@RequestParam("user") String userId, @RequestParam("password") String password) {
	User user = this.userRepository.findOne(userId);

	// ~~~ Login failed due to wrong password
	if ( !user.logIn(password) ) {
	    return new ResponseEntity<User>(HttpStatus.UNAUTHORIZED);
	}

	// ~~~ Try to create a JWT-Token
	try {
	    user.token = JWT.create()
			.withIssuer("paf-quiz")
			.withSubject(userId)
			.sign(Algorithm.HMAC256(password));
	} catch (IllegalArgumentException | JWTCreationException | UnsupportedEncodingException e) {
	    e.printStackTrace();
	    return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
	}
	
	// ~~~ Enhance resource with additional links
	user.add(linkTo(methodOn(UserRepository.class).findOne(userId)).withSelfRel());
	return new ResponseEntity<User>(user, HttpStatus.OK);
    }
}<commit_msg>REST-Endpoint for Login and Response of User with JWT<commit_after>package m11.mib.paf.quiz;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;

import m11.mib.paf.quiz.user.User;
import m11.mib.paf.quiz.user.UserRepository;

/**
 * MT \ 22.01.2017 \ AuthenticationController
 * 
 *
 * @author M11
 * @version 1.0
 */
@RestController
public class AuthenticationController {

    private UserRepository userRepository;
    
    /**
     * @param userRepository 
     */
    @Autowired
    public AuthenticationController(UserRepository userRepository) {
	this.userRepository = userRepository;
    }
    
    /**
     * REST-Endpoint for logging into the quiz.
     * Takes the user credentials and tries to verify, that the user exists and the password is correct.
     * At the end a JSON-Web-Token is generated with the users legitimate claim and then served as response.
     * 
     * @param userId
     * @param password
     * @return the JSON-Web-Token packed into a JSON
     */
    @RequestMapping(value = "/api/auth/login")
    public HttpEntity<User> login(@RequestParam("user") String userId, @RequestParam("password") String password) {
	User user = this.userRepository.findOne(userId);

	// ~~~ Login failed due to wrong password
	if ( !user.logIn(password) ) {
	    return new ResponseEntity<User>(HttpStatus.UNAUTHORIZED);
	}

	// ~~~ Try to create a JWT-Token
	try {
	    user.token = JWT.create()
			.withIssuer("paf-quiz")
			.withSubject(userId)
			.sign(Algorithm.HMAC256(password));
	} catch (IllegalArgumentException | JWTCreationException | UnsupportedEncodingException e) {
	    e.printStackTrace();
	    return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
	}
	
	// ~~~ Enhance resource with additional links
	//user.add(linkTo(methodOn(UserRepository.class).findOne(userId)).withSelfRel());
	return new ResponseEntity<User>(user, HttpStatus.OK);
    }
}
{'fraction_non_alphanumeric': 0.10755102040816326, 'fraction_numerical': 0.009795918367346938, 'mean_word_length': 4.158947368421052, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6887479', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1443, 'n_words': 324}