<commit_before>package gov.nih.nci.cabig.caaers.service.security;

import gov.nih.nci.cabig.caaers.CaaersNoSuchUserException;
import gov.nih.nci.cabig.caaers.CaaersSystemException;
import gov.nih.nci.cabig.caaers.dao.UserDao;
import gov.nih.nci.cabig.caaers.domain.User;
import gov.nih.nci.cabig.caaers.domain.repository.CSMUserRepository;
import gov.nih.nci.cabig.caaers.service.security.passwordpolicy.PasswordPolicyService;
import gov.nih.nci.cabig.caaers.service.security.user.Credential;

import java.sql.Timestamp;
import java.util.Date;

import org.springframework.beans.factory.annotation.Required;

/**
 * @author <NAME>
 */
public class PasswordManagerServiceImpl implements PasswordManagerService {
	
    private PasswordPolicyService passwordPolicyService;

    private CSMUserRepository csmUserRepository;
    
    private UserDao userDao;

    public User requestToken(String userName) throws CaaersSystemException {
    	User user = userDao.getByLoginId(userName);
    	if(user == null){
    		throw new CaaersNoSuchUserException("User with login Id :" + userName + " unknowon");
    	}
        //get the token
    	csmUserRepository.userCreateToken(user);
    	userDao.save(user);
    	return user;
    }

    public void setPassword(String userName, String password, String token)
            throws CaaersSystemException {
    	User user = userDao.getByLoginId(userName);
    	
    	if(user == null){
    		new CaaersNoSuchUserException("User with login Id :" + userName + " unknowon");
    	}
        validateToken(user, token);
        validateAndSetPassword(user, password);
        userDao.save(user);
        
    }

    private boolean validateToken(User user, String token) throws CaaersSystemException {
        if (user.getTokenTime().after(
                new Timestamp(new Date().getTime()
                        - passwordPolicyService.getPasswordPolicy()
                        .getTokenTimeout()))
                && token.equals(user.getToken())) return true;
        throw new CaaersSystemException("Invalid token.");
    }

    private boolean validateAndSetPassword(User user, String password)
            throws CaaersSystemException {
        passwordPolicyService.validatePasswordAgainstCreationPolicy(new Credential(user.getLoginId(),
                password));
        csmUserRepository.userChangePassword(user, password, passwordPolicyService
                .getPasswordPolicy().getPasswordCreationPolicy().getPasswordHistorySize());
        return true;<commit_msg>CAAERS-3478<commit_after>package gov.nih.nci.cabig.caaers.service.security;

import gov.nih.nci.cabig.caaers.CaaersNoSuchUserException;
import gov.nih.nci.cabig.caaers.CaaersSystemException;
import gov.nih.nci.cabig.caaers.dao.UserDao;
import gov.nih.nci.cabig.caaers.domain.User;
import gov.nih.nci.cabig.caaers.domain.repository.CSMUserRepository;
import gov.nih.nci.cabig.caaers.service.security.passwordpolicy.PasswordPolicyService;
import gov.nih.nci.cabig.caaers.service.security.user.Credential;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;

/**
 * @author <NAME>
 */
public class PasswordManagerServiceImpl implements PasswordManagerService {
	
    private PasswordPolicyService passwordPolicyService;

    private CSMUserRepository csmUserRepository;
    
    private UserDao userDao;

    public User requestToken(String userName) throws CaaersSystemException {
    	User user = userDao.getByLoginId(userName);
    	if(user == null){
    		throw new CaaersNoSuchUserException("User with login Id :" + userName + " unknowon");
    	}
        //get the token
    	csmUserRepository.userCreateToken(user);
    	userDao.save(user);
    	return user;
    }

    public void setPassword(String userName, String password, String token)
            throws CaaersSystemException {
    	User user = userDao.getByLoginId(userName);
    	
    	if(user == null){
    		new CaaersNoSuchUserException("User with login Id :" + userName + " unknowon");
    	}
        validateToken(user, token);
        validateAndSetPassword(user, password);
        userDao.save(user);
        
    }

    private boolean validateToken(User user, String token) throws CaaersSystemException {
        if (user.getTokenTime().after(
                new Timestamp(new Date().getTime()
                        - passwordPolicyService.getPasswordPolicy()
                        .getTokenTimeout())) 
                &&  StringUtils.equals(user.getToken(), token)) return true;
        throw new CaaersSystemException("Invalid token.");
    }

    private boolean validateAndSetPassword(User user, String password)
            throws CaaersSystemException {
        passwordPolicyService.validatePasswordAgainstCreationPolicy(new Credential(user.getLoginId(),
                password));
        csmUserRepository.userChangePassword(user, password, passwordPolicyService
                .getPasswordPolicy().getPasswordCreationPolicy().getPasswordHistorySize());
        return true;
{'fraction_non_alphanumeric': 0.0934487507377533, 'fraction_numerical': 0.0007869368483179224, 'mean_word_length': 3.3564695801199655, 'pattern_counts': {'":': 0, '<': 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': '1095518', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1471, 'n_words': 262}