<commit_before>package com.wemater.util;

import org.apache.commons.codec.binary.Base64;
import org.hibernate.HibernateException;

import com.wemater.dto.User;
import com.wemater.exception.AuthException;
import com.wemater.exception.DataForbiddenException;
import com.wemater.exception.EvaluateException;



public class AuthUtil {
	
private final SessionUtil su;
    
public AuthUtil(SessionUtil su) { 
	this.su = su;
}  
	
	
	
public boolean isUserAuthenticated(String authString, String username){
	
	if(Util.IsEmptyOrNull(authString)) throw new AuthException("Authentication Required");
        
if (validateUserCredentials(DecodeAuthString(authString),username)) return true;
return false;
}
	
public boolean isUserAuthenticatedGET(String authString){
    
if(Util.IsEmptyOrNull(authString)) throw new AuthException("Authentication Required");
	    
		
if (validateUserCredentials(DecodeAuthString(authString))) return true;
return false;
}
	


private  boolean validateUserCredentials(String decodedAuthString, String username){

      String[] params = getParamArray(decodedAuthString);
      System.out.println(params[0]+" "+params[1]);
    	 
	   User AuthUser = null;
	   Boolean isValidationSuccessfull = false;
	
		try {
			   	su.beginSessionWithTransaction();
			
			   	AuthUser = (User) su.getSession().getNamedQuery("user.IsUserAvailable")
		               .setParameter("username", params[0])
		               .setParameter("password", params[1])
		               .uniqueResult();
			
			   	su.CommitCurrentTransaction();
			
			   	if(AuthUser == null ){ //either username doesnt match or no user present
			   		isValidationSuccessfull = false;
			   		throw new AuthException("User credentials are invalid");
			   	}
			   	if(!username.equals(params[0])){ //either username doesnt match or no user present
			   		isValidationSuccessfull = false;
			   		throw new DataForbiddenException("Private Resouce!!");
			   	}
			 
				if(AuthUser != null && username.equals(params[0])){ //user present and username matches to current
			   	isValidationSuccessfull =true;
				}
				
		}catch (HibernateException e) {
		  su.rollBackCurrentTransaction();
		  throw new EvaluateException(e);
	   }
		   return isValidationSuccessfull;
    }	
	

private  boolean validateUserCredentials(String decodedAuthString){

    String[] params = getParamArray(decodedAuthString);
    System.out.println(params[0]+" "+params[1]);
  	 
	   User AuthUser = null;
	   Boolean isValidationSuccessfull = false;
	
		try {
			   	su.beginSessionWithTransaction();
			
			   	AuthUser = (User) su.getSession().getNamedQuery("user.IsUserAvailable")
		               .setParameter("username", params[0])
		               .setParameter("password", params[1])
		               .uniqueResult();
			
			   	su.CommitCurrentTransaction();
			
			   	if(AuthUser == null ){ //either username doesnt match or no user present
			   		isValidationSuccessfull = false;
			   		throw new AuthException( "User credentials are invalid");
			   	}
			 
				if(AuthUser != null ){ //user present and username matches to current
			   	isValidationSuccessfull =true;
				}
				
		}catch (HibernateException e) {
		  su.rollBackCurrentTransaction();
		  throw new EvaluateException(e);
	   }
		   return isValidationSuccessfull;
  }	
	
 
 private String DecodeAuthString(String authString){
	 
	 String[] decodedAuth = authString.split("\\s+");
	 if(decodedAuth.length != 2 ) throw new AuthException("Invalid User Credentials");
	  byte[] decodedAuthParam = Base64.decodeBase64(decodedAuth[1]);
	  String authparam =  new String(decodedAuthParam);
	  if(Util.IsEmptyOrNull(authparam)) throw new AuthException("Invalid User Credentials");
	  return authparam;
	  
 }
 
 private String[] getParamArray(String authString){
	   String[] params = authString.split(":");
	   if(params.length != 2 ) throw new AuthException( "Invalid User Credentials");
	   return params;
 }
 
 public String getLoggedInUser(String authString){
	return getParamArray(DecodeAuthString(authString))[0];
	  
 }


}
<commit_msg>added auth util again after pull[2]<commit_after>package com.wemater.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.hibernate.HibernateException;

import com.wemater.dto.User;
import com.wemater.exception.AuthException;
import com.wemater.exception.DataForbiddenException;
import com.wemater.exception.EvaluateException;



public class AuthUtil {
	
		private final SessionUtil su;
		private final static Map<String, String> Authmapper = new HashMap<String, String>();
		    
		public AuthUtil(SessionUtil su) { 
			this.su = su;
			
		}  
			
			
		/*
		 * //Authentication for the private resources which only a user can see.
		 * User can see his Own Information or the comments he has put
		 * Other users cant see the information about other user such as email address or username 
		 * 
		 * This is meant to keep the user Info private
		 * however the protected resources such as the articles written by a user are not controlled by it
		 * 
		 */
		
		/**
		 * @param authString
		 * @param username
		 * @return boolean. true if the authentication is success 
		 * username should match the user who has logged in
		 */
		public boolean isUserAuthenticated(String authString, String username){
			
			if(Util.IsEmptyOrNull(authString)) throw new AuthException("Authentication Required--NULL AUTH");
		        
		       if(IsUserAvailableInAuthMap(authString)) return true;
		       else if(isUserAvailableInDatabase(authString, username)){return true;}
		       else return false;
		}
			
		
		
		/**
		 * @param authString
		 * @return boolean /Return true if the user has already registered
		 * 
		 * This method is used to authenticate a user for protected resources
		 */
		public boolean isUserAuthenticatedGET(String authString){
		    
			if(Util.IsEmptyOrNull(authString)) throw new AuthException("Authentication Required - NULL AUTH GET");
 
	       if(IsUserAvailableInAuthMap(authString)) return true;
	       else if(isUserAvaliableInDatabaseGET(authString)){return true;}
	       else return false;

		}
	

private  boolean  isUserAvailableInDatabase(String encodedAuthString, String username){

	  
      String[] params = getParamArray(encodedAuthString);
      System.out.println(params[0]+" "+params[1]);
    	 
	   User AuthUser = null;
	   Boolean isValidationSuccessfull = false;
	
		try {
			   	AuthUser = findRegisteredUser(params);
			
			   	if(AuthUser == null ){ //either username doesnt match or no user present
			   		isValidationSuccessfull = false;
			   		throw new AuthException("User credentials are invalid -NOT FOUND IN DATABASE");
			   	}
			   	if(!username.equals(params[0])){ //either username doesnt match or no user present
			   		isValidationSuccessfull = false;
			   		throw new DataForbiddenException("Private Resouce!!");
			   	}
			 
				if(AuthUser != null && username.equals(params[0])){ //user present and username matches to current
			    	isValidationSuccessfull =true;
			    	System.out.println("FOUND IN DATABASE ");
				   addToAuthMap(encodedAuthString); //User validated so put it in map
				}
				
		}catch (HibernateException e) {
		  su.rollBackCurrentTransaction();
		  throw new EvaluateException(e);
	   }
		   return isValidationSuccessfull;
    }	
	

private  boolean isUserAvaliableInDatabaseGET(String encodedAuthString){

    String[] params = getParamArray(encodedAuthString);
    System.out.println(params[0]+" "+params[1]);
  	 
	   User AuthUser = null;
	   Boolean isValidationSuccessfull = false;
	
		try {
			   	AuthUser = findRegisteredUser(params);
			
			   	if(AuthUser == null ){ //either username doesnt match or no user present
			   		isValidationSuccessfull = false;
			   		throw new AuthException( "User credentials are invalid -- NOT FOUND IN DATABASE GET");
			   	}
			 
				if(AuthUser != null ){ //user present and username matches to current
					System.out.println("FOUND IN DATABASE GET");
					addToAuthMap(encodedAuthString);
			   	isValidationSuccessfull =true;
				}
				
		}catch (HibernateException e) {
		  su.rollBackCurrentTransaction();
		  throw new EvaluateException(e);
	   }
		   return isValidationSuccessfull;
  }


private User findRegisteredUser(String[] params) {
	User AuthUser;
	su.beginSessionWithTransaction();

	AuthUser = (User) su.getSession().getNamedQuery("user.IsUserAvailable")
	       .setParameter("username", params[0])
	       .setParameter("password", params[1])
	       .uniqueResult();

	su.CommitCurrentTransaction();
	return AuthUser;
}	
	
private  void addToAuthMap(String encodedAuthString){
	
	 System.out.println(" Adding to auth map");
	 Authmapper.put(getLoggedInUser(encodedAuthString), encodedAuthString);
	 System.out.println("added to authmap "+Authmapper.get(getLoggedInUser(encodedAuthString)));
	
}

private void removeFromAuthMap(String username){
	Authmapper.remove(username);
}

private boolean IsUserAvailableInAuthMap(String authString){
	 System.out.println("checking in Map");
	    System.out.println(Authmapper.get(getLoggedInUser(authString)));
	  if(Authmapper.get(getLoggedInUser(authString)) == null) return false;
	  System.out.println("FOUND IN MAP");
    return true;
	
} 

 private String DecodeAuthString(String encodedAuthString){
	 
	 String[] decodedAuth = encodedAuthString.split("\\s+");
	 if(decodedAuth.length != 2 ) throw new AuthException("Invalid User Credentials _DECODE AUTH !=2");
	  byte[] decodedAuthParam = Base64.decodeBase64(decodedAuth[1]);
	  String authparam =  new String(decodedAuthParam);
	  if(Util.IsEmptyOrNull(authparam)) throw new AuthException("Invalid User Credentials EMPTY NULL");
	  return authparam;
	  
 }
 
 private String[] getParamArray(String encodedauthString){
	   String[] params = DecodeAuthString(encodedauthString).split(":");
	   if(params.length != 2 ) throw new AuthException( "Invalid User Credentials --PARAM ARRAY != 2");
	   return params;
 }
 
 public String getLoggedInUser(String decodedauthString){
	return getParamArray(decodedauthString)[0];
	  
 }


}

{'fraction_non_alphanumeric': 0.0973468783903738, 'fraction_numerical': 0.00404379130091725, 'mean_word_length': 3.6301369863013697, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '10273', 'n_tokens_mistral': 3251, 'n_tokens_neox': 2762, 'n_words': 723}