<commit_before>package nuclibook.server;

import nuclibook.constants.C;
import nuclibook.constants.RequestType;
import nuclibook.entity_utils.SecurityUtils;
import nuclibook.models.Staff;
import nuclibook.routes.*;
import org.apache.commons.configuration.ConfigurationException;
import spark.Session;
import spark.Spark;

import java.awt.*;
import java.math.BigInteger;
import java.security.SecureRandom;

/**
 * This class creates and manages the server for the entire system.
 * Its main jobs are to set up the server, route pages, and handle top-level security.
 */
public class LocalServer {

	/**
	 * Create the server and perform initial configuration.
	 * @param args Any command line arguments; ignored in this application.
	 */
	public static void main(String... args) {
		/*
		SERVER SETTINGS
		 */

		//initialise constants
		try {
			C.initConstants();
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}

		// static files folder
		Spark.staticFileLocation("/static");

		// page security
		Spark.before((request, response) -> {
			// get path
			String path = request.pathInfo();

			// get current session and user
			Session session = request.session();
			if (request.queryParams("token") != null) {
				session = SecurityUtils.checkOneOffSession(request.queryParams("token"));
			}
			Staff user = SecurityUtils.getCurrentUser(session);

			// require all POST requests to be authenticated with a CSRF token
			if (request.requestMethod().toLowerCase().equals("post") && !SecurityUtils.checkCsrfToken(session, request.queryParams("csrf-token"))) {
				Spark.halt(403, "Security token invalid.");
			}

			// check if they are accessing non-page
			if (path.startsWith("/css")
					|| path.startsWith("/images")
					|| path.startsWith("/js")
					|| path.startsWith("/font-awesome")) {
				// nothing more to do - everything is fine
				return;
			}

			// check for a password force-change
			if (SecurityUtils.checkLoggedIn(session)
					&& user != null
					&& user.getDaysRemainingToPasswordChange() < 1
					&& !path.startsWith("/profile")
					&& !(path.equals("/entity-update") && request.queryParams("entity-type").equals("staff-password-change"))) {
				response.redirect("/profile?changepw=1&force=1");
				Spark.halt("Redirecting.");
			}

			// check if they are accessing a non-secure page
			if (path.startsWith("/login")) {
				// nothing more to do - everything is fine
				return;
			}

			// not authenticated?
			if (!SecurityUtils.checkLoggedIn(session)) {
				// send them back to the login page
				response.redirect("/login");
				Spark.halt("Redirecting.");
			}

			// CSV page
			if (path.endsWith(".goto-csv")) {
				if (Desktop.isDesktopSupported()) {
					// generate token
					SecureRandom random = new SecureRandom();
					String token = new BigInteger(130, random).toString(32);
					SecurityUtils.setOneOffToken(token, session);<commit_msg>Update LocalServer.java<commit_after>package nuclibook.server;

import nuclibook.constants.C;
import nuclibook.constants.RequestType;
import nuclibook.entity_utils.SecurityUtils;
import nuclibook.models.Staff;
import nuclibook.routes.*;
import org.apache.commons.configuration.ConfigurationException;
import spark.Session;
import spark.Spark;

import java.awt.*;
import java.math.BigInteger;
import java.security.SecureRandom;

/**
 * This class creates and manages the server for the entire system.
 * Its main jobs are to set up the server, route pages, and handle top-level security.
 */
public class LocalServer {

	/**
	 * Create the server and perform initial configuration.
	 * @param args Any command line arguments; ignored in this application.
	 */
	public static void main(String... args) {
		/*
		SERVER SETTINGS
		 */

		//initialise constants
		try {
			C.initConstants();
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}

		// static files folder
		Spark.staticFileLocation("/static");

		// page security
		Spark.before((request, response) -> {
			// get path
			String path = request.pathInfo();

			// get current session and user
			Session session = request.session();
			if (request.queryParams("token") != null) {
				session = SecurityUtils.checkOneOffSession(request.queryParams("token"));
			}
			Staff user = SecurityUtils.getCurrentUser(session);

			// require all POST requests to be authenticated with a CSRF token
			if (request.requestMethod().toLowerCase().equals("post") && !SecurityUtils.checkCsrfToken(session, request.queryParams("csrf-token"))) {
				Spark.halt(403, "Security token invalid.");
			}

			// check if they are accessing non-page
			if (path.startsWith("/css")
					|| path.startsWith("/images")
					|| path.startsWith("/js")
					|| path.startsWith("/font-awesome")) {
				// nothing more to do - everything is fine
				return;
			}

			// check for a password force-change
			if (SecurityUtils.checkLoggedIn(session)
					&& user != null
					&& user.getDaysRemainingToPasswordChange() < 1
					&& !path.startsWith("/profile")
					&& !(path.equals("/entity-update") && request.queryParams("entity-type") != null && request.queryParams("entity-type").equals("staff-password-change"))) {
				response.redirect("/profile?changepw=1&force=1");
				Spark.halt("Redirecting.");
			}

			// check if they are accessing a non-secure page
			if (path.startsWith("/login")) {
				// nothing more to do - everything is fine
				return;
			}

			// not authenticated?
			if (!SecurityUtils.checkLoggedIn(session)) {
				// send them back to the login page
				response.redirect("/login");
				Spark.halt("Redirecting.");
			}

			// CSV page
			if (path.endsWith(".goto-csv")) {
				if (Desktop.isDesktopSupported()) {
					// generate token
					SecureRandom random = new SecureRandom();
					String token = new BigInteger(130, random).toString(32);
					SecurityUtils.setOneOffToken(token, session);
{'fraction_non_alphanumeric': 0.13270868824531515, 'fraction_numerical': 0.003747870528109029, 'mean_word_length': 3.5406032482598606, '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': '363568', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1676, 'n_words': 438}