<commit_before>package org.sagebionetworks.repo.manager;

import org.sagebionetworks.repo.model.AuthenticationDAO;
import org.sagebionetworks.repo.model.AuthorizationConstants;
import org.sagebionetworks.repo.model.DomainType;
import org.sagebionetworks.repo.model.LockedException;
import org.sagebionetworks.repo.model.TermsOfUseException;
import org.sagebionetworks.repo.model.UnauthenticatedException;
import org.sagebionetworks.repo.model.UserGroup;
import org.sagebionetworks.repo.model.UserGroupDAO;
import org.sagebionetworks.repo.model.auth.Session;
import org.sagebionetworks.repo.model.semaphore.MemoryCountingSemaphore;
import org.sagebionetworks.repo.web.NotFoundException;
import org.sagebionetworks.securitytools.PBKDF2Utils;
import org.sagebionetworks.util.ValidateArgument;
import org.springframework.beans.factory.annotation.Autowired;

import org.sagebionetworks.repo.transactions.WriteTransaction;

public class AuthenticationManagerImpl implements AuthenticationManager {

	public static final int PASSWORD_MIN_LENGTH = 8;

	public static final long LOCK_TIMOUTE_SEC = 60*60*1000;

	public static final int MAX_CONCURRENT_LOCKS = 10;

	@Autowired
	private AuthenticationDAO authDAO;
	@Autowired
	private UserGroupDAO userGroupDAO;
	@Autowired
	private MemoryCountingSemaphore usernameThrottleGate;
	
	public AuthenticationManagerImpl() { }

	@Override
	@WriteTransaction
	public Session authenticate(long principalId, String password, DomainType domain) throws NotFoundException {
		// acquire a lock for throttling password attacks
		String lockToken = usernameThrottleGate.attemptToAcquireLock(""+principalId, LOCK_TIMOUTE_SEC, MAX_CONCURRENT_LOCKS);
		if (lockToken != null) {
			// Check the username password combination
			// This will throw an UnauthorizedException if invalid
			if (password != null) {
				byte[] salt = authDAO.getPasswordSalt(principalId);
				String passHash = PBKDF2Utils.hashPassword(password, salt);
				authDAO.checkUserCredentials(principalId, passHash);
			}
			usernameThrottleGate.releaseLock(""+principalId, lockToken);
			return getSessionToken(principalId, domain);
		} else {
			throw new LockedException(AuthorizationConstants.REASON_TOO_MANY_CONCURRENT_REQUESTS);
		}
	}
	
	@Override
	public Long getPrincipalId(String sessionToken) {
		Long principalId = authDAO.getPrincipal(sessionToken);
		if (principalId == null) {
			throw new UnauthenticatedException("The session token (" + sessionToken + ") has expired");
		}
		return principalId;
	}
	<commit_msg>Change the message<commit_after>package org.sagebionetworks.repo.manager;

import org.sagebionetworks.repo.model.AuthenticationDAO;
import org.sagebionetworks.repo.model.AuthorizationConstants;
import org.sagebionetworks.repo.model.DomainType;
import org.sagebionetworks.repo.model.LockedException;
import org.sagebionetworks.repo.model.TermsOfUseException;
import org.sagebionetworks.repo.model.UnauthenticatedException;
import org.sagebionetworks.repo.model.UserGroup;
import org.sagebionetworks.repo.model.UserGroupDAO;
import org.sagebionetworks.repo.model.auth.Session;
import org.sagebionetworks.repo.model.semaphore.MemoryCountingSemaphore;
import org.sagebionetworks.repo.web.NotFoundException;
import org.sagebionetworks.securitytools.PBKDF2Utils;
import org.sagebionetworks.util.ValidateArgument;
import org.springframework.beans.factory.annotation.Autowired;

import org.sagebionetworks.repo.transactions.WriteTransaction;

public class AuthenticationManagerImpl implements AuthenticationManager {

	public static final int PASSWORD_MIN_LENGTH = 8;

	public static final long LOCK_TIMOUTE_SEC = 60*60*1000;

	public static final int MAX_CONCURRENT_LOCKS = 10;

	public static final String ACCOUNT_LOCKED_MESSAGE = "This account has been locked. Reason: too many requests. Please try again in an hour.";

	@Autowired
	private AuthenticationDAO authDAO;
	@Autowired
	private UserGroupDAO userGroupDAO;
	@Autowired
	private MemoryCountingSemaphore usernameThrottleGate;
	
	public AuthenticationManagerImpl() { }

	@Override
	@WriteTransaction
	public Session authenticate(long principalId, String password, DomainType domain) throws NotFoundException {
		// acquire a lock for throttling password attacks
		String lockToken = usernameThrottleGate.attemptToAcquireLock(""+principalId, LOCK_TIMOUTE_SEC, MAX_CONCURRENT_LOCKS);
		if (lockToken != null) {
			// Check the username password combination
			// This will throw an UnauthorizedException if invalid
			if (password != null) {
				byte[] salt = authDAO.getPasswordSalt(principalId);
				String passHash = PBKDF2Utils.hashPassword(password, salt);
				authDAO.checkUserCredentials(principalId, passHash);
			}
			usernameThrottleGate.releaseLock(""+principalId, lockToken);
			return getSessionToken(principalId, domain);
		} else {
			throw new LockedException(ACCOUNT_LOCKED_MESSAGE);
		}
	}
	
	@Override
	public Long getPrincipalId(String sessionToken) {
		Long principalId = authDAO.getPrincipal(sessionToken);
		if (principalId == null) {
			throw new UnauthenticatedException("The session token (" + sessionToken + ") has expired");
		}
		return principalId;
	}
	
{'fraction_non_alphanumeric': 0.0797748447204969, 'fraction_numerical': 0.005046583850931677, 'mean_word_length': 6.217086834733894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6083195', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1581, 'n_words': 322}