<commit_before>package controllers;

import static models.utils.constants.AuthentificationConstants.*;
import static models.utils.constants.UserConstants.*;

import models.User;
import models.Workshop;
import models.WorkshopSession;

import org.codehaus.jackson.JsonNode;

import play.Play;
import play.cache.Cache;
import play.db.jpa.JPA;
import play.db.jpa.Transactional;
import play.i18n.Messages;
import play.libs.WS;
import play.libs.WS.Response;
import play.mvc.Controller;
import play.mvc.Result;
import services.UserService;
import views.html.errors.error;
import views.html.welcome.charter;

/**
 * Ce controller regroupe toutes les actions qui sont liées à l'authentification via Google API
 * 
 * @author ychartois
 */
public class AuthentificationController extends Controller {
	
	
	//<--------------------------------------------------------------------------->
	//-							 Constructeur(s)	        
	//<--------------------------------------------------------------------------->
	/**
	 * Constructeur par defaut
	 */
	public AuthentificationController() {
		super();
	}
	
	
	//<--------------------------------------------------------------------------->
	//-							 Actions(s)	        
	//<--------------------------------------------------------------------------->	
	/**
	 * Action which handle the authentification by Google and put the authentificated user
	 * in the session
	 * 
	 * @return to welcome page
	 */
	@Transactional
	public static Result callback() {
		
		// On récupère le code fourni par Google
		String[]	codeRetour 				= 	request().queryString().get(CALLBACK_GOOGLE_CODE);
		
		// Si le code n'existe pas c'est qu'il y a une erreur
		if ( codeRetour == null ) {
			String 		errorCode 			= 	request().queryString().get(CALLBACK_GOOGLE_ERROR)[CALLBACK_GOOGLE_INFO_INDEX];

			return ok ( error.render( Messages.get("error.google.authentification.refused", errorCode)) );
		}
		
		// Request an Access tokenarg0 
		Response 	responseAccessToken 	= 	WS.url(GOOGLE_ACCESS_TOKEN_URL)
													.setHeader("Content-Type", "application/x-www-form-urlencoded")
													.post( getAccessTokenParams( codeRetour[CALLBACK_GOOGLE_INFO_INDEX] ) )
													.get();
		
		JsonNode 	resultAccessToken		= 	responseAccessToken.asJson();
		
		// Get the user information
		Response 	response 				= 	WS.url(GOOGLE_USER_INFO_URL)
													.setQueryParameter(GOOGLE_ACCESS_TOKEN, resultAccessToken.get(GOOGLE_ACCESS_TOKEN).asText())
													.get().get();
		
		JsonNode 	result 					= 	response.asJson();
		
		// Call the service that handle the user
		User 		user 					= 	new UserService().handleUserFromGoogleResponse( result );
		
		if ( user.isCharterAgree() ) {
			// We save the new instance and save it in cache and redirect to welcome page
			JPA.em().merge( user );
			Cache.set( Application.getUuid() + "connectedUser", user );
			
			return redirect(routes.Application.welcome());
		} 
		else {
			// We save the new user in cache and redirect to charter page
			Cache.set( Application.getUuid() + "newUser", user );
			return ok( charter.render(true) );
		}
	}
	
	/**
	 * Create a new user after his acceptance to the charter
	 * 
	 * @return the welcome page
	 */
	@Transactional
	public static Result createNewUser() {
		
		// We retreive the new user from cache and persist it
		User user = (User) Cache.get( Application.getUuid() + "newUser" );
		JPA.em().persist( user );
		
		// The new user is now connected
		Cache.set( Application.getUuid() + "connectedUser", user );
		
		return redirect(routes.Application.welcome());
	}<commit_msg>Un utilisateur hors SQLI ne peut pas accéder à l'appli<commit_after>package controllers;

import static models.utils.constants.AuthentificationConstants.*;
import static models.utils.constants.UserConstants.*;

import models.User;
import models.Workshop;
import models.WorkshopSession;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;

import play.Play;
import play.cache.Cache;
import play.db.jpa.JPA;
import play.db.jpa.Transactional;
import play.i18n.Messages;
import play.libs.WS;
import play.libs.WS.Response;
import play.mvc.Controller;
import play.mvc.Result;
import services.UserService;
import views.html.errors.error;
import views.html.welcome.charter;

/**
 * Ce controller regroupe toutes les actions qui sont liées à l'authentification via Google API
 * 
 * @author ychartois
 */
public class AuthentificationController extends Controller {
	
	
	//<--------------------------------------------------------------------------->
	//-							 Constructeur(s)	        
	//<--------------------------------------------------------------------------->
	/**
	 * Constructeur par defaut
	 */
	public AuthentificationController() {
		super();
	}
	
	
	//<--------------------------------------------------------------------------->
	//-							 Actions(s)	        
	//<--------------------------------------------------------------------------->	
	/**
	 * Action which handle the authentification by Google and put the authentificated user
	 * in the session
	 * 
	 * @return to welcome page
	 */
	@Transactional
	public static Result callback() {
		
		// On récupère le code fourni par Google
		String[]	codeRetour 				= 	request().queryString().get(CALLBACK_GOOGLE_CODE);
		
		// Si le code n'existe pas c'est qu'il y a une erreur
		if ( codeRetour == null ) {
			String 		errorCode 			= 	request().queryString().get(CALLBACK_GOOGLE_ERROR)[CALLBACK_GOOGLE_INFO_INDEX];

			return ok ( error.render( Messages.get("error.google.authentification.refused", errorCode)) );
		}
		
		// Request an Access tokenarg0 
		Response 	responseAccessToken 	= 	WS.url(GOOGLE_ACCESS_TOKEN_URL)
													.setHeader("Content-Type", "application/x-www-form-urlencoded")
													.post( getAccessTokenParams( codeRetour[CALLBACK_GOOGLE_INFO_INDEX] ) )
													.get();
		
		JsonNode 	resultAccessToken		= 	responseAccessToken.asJson();
		
		// Get the user information
		Response 	response 				= 	WS.url(GOOGLE_USER_INFO_URL)
													.setQueryParameter(GOOGLE_ACCESS_TOKEN, resultAccessToken.get(GOOGLE_ACCESS_TOKEN).asText())
													.get().get();
		
		JsonNode 	result 					= 	response.asJson();
		
		// Call the service that handle the user
		User 		user 					= 	new UserService().handleUserFromGoogleResponse( result );
		
		// If the user is not from sqli he can't connect
		if ( !StringUtils.endsWith(user.getEmail(), "@<EMAIL>") ) {
			//TODO un pop-up pour expliquer pourquoi çà serai sympa
			return forbidden();
		}
		
		
		if ( user.isCharterAgree()  ) {
			// We save the new instance and save it in cache and redirect to welcome page
			JPA.em().merge( user );
			Cache.set( Application.getUuid() + "connectedUser", user );
			
			return redirect(routes.Application.welcome());
		} 
		else {
			// We save the new user in cache and redirect to charter page
			Cache.set( Application.getUuid() + "newUser", user );
			return ok( charter.render(true) );
		}
	}
	
	/**
	 * Create a new user after his acceptance to the charter
	 * 
	 * @return the welcome page
	 */
	@Transactional
	public static Result createNewUser() {
		
		// We retreive the new user from cache and persist it
		User user = (User) Cache.get( Application.getUuid() + "newUser" );
		JPA.em().persist( user );
		
		// The new user is now connected
		Cache.set( Application.getUuid() + "connectedUser", user );
		
		return redirect(routes.Application.welcome());
	}
{'fraction_non_alphanumeric': 0.18240257131378063, 'fraction_numerical': 0.0008035355564483728, 'mean_word_length': 2.3685160126296796, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2359656', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2150, 'n_words': 555}