<commit_before>package com.coreoz.plume.admin.webservices;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.coreoz.plume.admin.security.login.LoginFailAttemptsManager;
import com.coreoz.plume.admin.services.configuration.AdminConfigurationService;
import com.coreoz.plume.admin.services.user.AdminUserService;
import com.coreoz.plume.admin.webservices.data.session.AdminCredentials;
import com.coreoz.plume.admin.webservices.errors.AdminWsError;
import com.coreoz.plume.admin.webservices.security.WebSessionProvider;
import com.coreoz.plume.admin.websession.WebSessionSigner;
import com.coreoz.plume.jersey.errors.WsException;
import com.google.common.collect.ImmutableList;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Path("/admin/session")
@Api(value = "Manage the administration session")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Singleton
public class SessionWs {

	private final AdminUserService adminUserService;
	private final WebSessionSigner sessionSigner;
	private final WebSessionProvider webSessionProvider;

	private final LoginFailAttemptsManager failAttemptsManager;

	private final long blockedDurationInSeconds;

	@Inject
	public SessionWs(AdminUserService adminUserService,
			WebSessionSigner sessionSigner,
			AdminConfigurationService configurationService,
			WebSessionProvider webSessionProvider) {
		this.adminUserService = adminUserService;
		this.sessionSigner = sessionSigner;
		this.webSessionProvider = webSessionProvider;

		this.failAttemptsManager = new LoginFailAttemptsManager(
			configurationService.loginMaxAttempts(),
			configurationService.loginBlockedDuration()
		);
		this.blockedDurationInSeconds = configurationService.loginBlockedDuration().getSeconds();
	}

	@POST
	@ApiOperation(value = "Authenticate a user and create a session token")
	public String authenticate(AdminCredentials credentials) {
		if(credentials.getUserName() != null && failAttemptsManager.isBlocked(credentials.getUserName())) {
			throw new WsException(
				AdminWsError.TOO_MANY_WRONG_ATTEMPS,
				ImmutableList.of(String.valueOf(blockedDurationInSeconds))
			);
		}

		return adminUserService
			.authenticate(credentials.getUserName(), credentials.getPassword())
			.map(user ->
				sessionSigner.serializeSession(webSessionProvider.toWebSession(user))
			)
			.orElseThrow(() -> {
				failAttemptsManager.addAttempt(credentials.getUserName());
				return new WsException(AdminWsError.WRONG_LOGIN_OR_PASSWORD);
			});
	}

}<commit_msg>SessionWs returns a text response<commit_after>package com.coreoz.plume.admin.webservices;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.coreoz.plume.admin.security.login.LoginFailAttemptsManager;
import com.coreoz.plume.admin.services.configuration.AdminConfigurationService;
import com.coreoz.plume.admin.services.user.AdminUserService;
import com.coreoz.plume.admin.webservices.data.session.AdminCredentials;
import com.coreoz.plume.admin.webservices.errors.AdminWsError;
import com.coreoz.plume.admin.webservices.security.WebSessionProvider;
import com.coreoz.plume.admin.websession.WebSessionSigner;
import com.coreoz.plume.jersey.errors.Validators;
import com.coreoz.plume.jersey.errors.WsException;
import com.google.common.collect.ImmutableList;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Path("/admin/session")
@Api(value = "Manage the administration session")
@Consumes(MediaType.APPLICATION_JSON)
@Singleton
public class SessionWs {

	private final AdminUserService adminUserService;
	private final WebSessionSigner sessionSigner;
	private final WebSessionProvider webSessionProvider;

	private final LoginFailAttemptsManager failAttemptsManager;

	private final long blockedDurationInSeconds;

	@Inject
	public SessionWs(AdminUserService adminUserService,
			WebSessionSigner sessionSigner,
			AdminConfigurationService configurationService,
			WebSessionProvider webSessionProvider) {
		this.adminUserService = adminUserService;
		this.sessionSigner = sessionSigner;
		this.webSessionProvider = webSessionProvider;

		this.failAttemptsManager = new LoginFailAttemptsManager(
			configurationService.loginMaxAttempts(),
			configurationService.loginBlockedDuration()
		);
		this.blockedDurationInSeconds = configurationService.loginBlockedDuration().getSeconds();
	}

	@POST
	@Produces(MediaType.TEXT_PLAIN)
	@ApiOperation(value = "Authenticate a user and create a session token")
	public String authenticate(AdminCredentials credentials) {
		Validators.checkRequired("users.USERNAME", credentials.getUserName());
		Validators.checkRequired("users.PASSWORD", credentials.getPassword());

		if(credentials.getUserName() != null && failAttemptsManager.isBlocked(credentials.getUserName())) {
			throw new WsException(
				AdminWsError.TOO_MANY_WRONG_ATTEMPS,
				ImmutableList.of(String.valueOf(blockedDurationInSeconds))
			);
		}

		return adminUserService
			.authenticate(credentials.getUserName(), credentials.getPassword())
			.map(user ->
				sessionSigner.serializeSession(webSessionProvider.toWebSession(user))
			)
			.orElseThrow(() -> {
				failAttemptsManager.addAttempt(credentials.getUserName());
				return new WsException(AdminWsError.WRONG_LOGIN_OR_PASSWORD);
			});
	}

}
{'fraction_non_alphanumeric': 0.0945753033547466, 'fraction_numerical': 0.0, 'mean_word_length': 6.513404825737266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2275590', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1646, 'n_words': 262}