<commit_before>package com.gentics.mesh.auth.provider;

import static com.gentics.mesh.core.rest.error.Errors.error;
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static io.netty.handler.codec.http.HttpResponseStatus.UNAUTHORIZED;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import com.gentics.mesh.auth.AuthenticationResult;
import com.gentics.mesh.context.InternalActionContext;
import com.gentics.mesh.core.data.user.HibUser;
import com.gentics.mesh.core.data.user.MeshAuthUser;
import com.gentics.mesh.core.db.Database;
import com.gentics.mesh.core.rest.auth.TokenResponse;
import com.gentics.mesh.etc.config.AuthenticationOptions;
import com.gentics.mesh.etc.config.MeshOptions;
import com.gentics.mesh.shared.SharedKeys;
import com.gentics.mesh.util.JWTUtil;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.JWTOptions;
import io.vertx.ext.auth.KeyStoreOptions;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.jwt.JWTAuth;
import io.vertx.ext.auth.jwt.JWTAuthOptions;
import io.vertx.ext.web.Cookie;

/**
 * Central mesh authentication provider which will handle JWT.
 *
 * Note that the auth process starts at {@link com.gentics.mesh.auth.handler.MeshJWTAuthHandler#handle(io.vertx.ext.web.RoutingContext)}.
 * The handler will extract the JWT values and this provider will authenticate the data and load the user.
 *
 */
@Singleton
public class MeshJWTAuthProvider implements AuthProvider, JWTAuth {

	private static final Logger log = LoggerFactory.getLogger(MeshJWTAuthProvider.class);

	public static final String TOKEN_COOKIE_KEY = "mesh.token";
	private static final String USERID_FIELD_NAME = "userUuid";
	private static final String API_KEY_TOKEN_CODE_FIELD_NAME = "jti";

	private final Vertx vertx;
	protected final Database db;
	private final BCryptPasswordEncoder passwordEncoder;
	private final MeshOptions meshOptions;

	private JWTAuth jwtProvider;

	@Inject
	public MeshJWTAuthProvider(
		Vertx vertx,
		MeshOptions meshOptions,
		BCryptPasswordEncoder passwordEncoder,
		Database database,
	) {
		this.vertx = vertx;
		this.meshOptions = meshOptions;
		this.passwordEncoder = passwordEncoder;
		this.db = database;
	}

	/**
	 * Delayed initialize function, which is only executed once this provider is actually needed
	 * ({@link #authenticateJWT(JsonObject, Handler) and {@link #generateToken(String, String, String)}}.
	 * This is done, to make testing possible, as otherwise the provider would be initialized with the
	 * settings, before the tests could change them (and therefore make it impossible to test different configurations).
	 */
	public void initialize() {
		if (this.jwtProvider != null) {
			return;
		}

		// Use the mesh JWT options in order to setup the JWTAuth provider
		AuthenticationOptions options = meshOptions.getAuthenticationOptions();
		String keystorePassword = options.getKeystorePassword();
		if (keystorePassword == null) {
			throw new RuntimeException("The keystore password could not be found within the authentication options.");
		}<commit_msg>Removed trailing comma which caused a SyntaxError<commit_after>package com.gentics.mesh.auth.provider;

import static com.gentics.mesh.core.rest.error.Errors.error;
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static io.netty.handler.codec.http.HttpResponseStatus.UNAUTHORIZED;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import com.gentics.mesh.auth.AuthenticationResult;
import com.gentics.mesh.context.InternalActionContext;
import com.gentics.mesh.core.data.user.HibUser;
import com.gentics.mesh.core.data.user.MeshAuthUser;
import com.gentics.mesh.core.db.Database;
import com.gentics.mesh.core.rest.auth.TokenResponse;
import com.gentics.mesh.etc.config.AuthenticationOptions;
import com.gentics.mesh.etc.config.MeshOptions;
import com.gentics.mesh.shared.SharedKeys;
import com.gentics.mesh.util.JWTUtil;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.JWTOptions;
import io.vertx.ext.auth.KeyStoreOptions;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.jwt.JWTAuth;
import io.vertx.ext.auth.jwt.JWTAuthOptions;
import io.vertx.ext.web.Cookie;

/**
 * Central mesh authentication provider which will handle JWT.
 *
 * Note that the auth process starts at {@link com.gentics.mesh.auth.handler.MeshJWTAuthHandler#handle(io.vertx.ext.web.RoutingContext)}.
 * The handler will extract the JWT values and this provider will authenticate the data and load the user.
 *
 */
@Singleton
public class MeshJWTAuthProvider implements AuthProvider, JWTAuth {

	private static final Logger log = LoggerFactory.getLogger(MeshJWTAuthProvider.class);

	public static final String TOKEN_COOKIE_KEY = "mesh.token";
	private static final String USERID_FIELD_NAME = "userUuid";
	private static final String API_KEY_TOKEN_CODE_FIELD_NAME = "jti";

	private final Vertx vertx;
	protected final Database db;
	private final BCryptPasswordEncoder passwordEncoder;
	private final MeshOptions meshOptions;

	private JWTAuth jwtProvider;

	@Inject
	public MeshJWTAuthProvider(
		Vertx vertx,
		MeshOptions meshOptions,
		BCryptPasswordEncoder passwordEncoder,
		Database database
	) {
		this.vertx = vertx;
		this.meshOptions = meshOptions;
		this.passwordEncoder = passwordEncoder;
		this.db = database;
	}

	/**
	 * Delayed initialize function, which is only executed once this provider is actually needed
	 * ({@link #authenticateJWT(JsonObject, Handler) and {@link #generateToken(String, String, String)}}.
	 * This is done, to make testing possible, as otherwise the provider would be initialized with the
	 * settings, before the tests could change them (and therefore make it impossible to test different configurations).
	 */
	public void initialize() {
		if (this.jwtProvider != null) {
			return;
		}

		// Use the mesh JWT options in order to setup the JWTAuth provider
		AuthenticationOptions options = meshOptions.getAuthenticationOptions();
		String keystorePassword = options.getKeystorePassword();
		if (keystorePassword == null) {
			throw new RuntimeException("The keystore password could not be found within the authentication options.");
		}
{'fraction_non_alphanumeric': 0.09154049173496319, 'fraction_numerical': 0.0002778163633838033, 'mean_word_length': 5.343612334801762, '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': '1466310', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2155, 'n_words': 546}