<commit_before>package com.specmate.auth.internal;

import java.util.Date;
import java.util.List;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;

import com.specmate.auth.api.ISessionService;
import com.specmate.auth.config.SessionServiceConfig;
import com.specmate.common.SpecmateException;
import com.specmate.persistency.IPersistencyService;
import com.specmate.persistency.ITransaction;
import com.specmate.persistency.IView;
import com.specmate.usermodel.AccessRights;
import com.specmate.usermodel.UserSession;
import com.specmate.usermodel.UsermodelFactory;

@Component(service = ISessionService.class, configurationPid = SessionServiceConfig.PID, 
	configurationPolicy = ConfigurationPolicy.REQUIRE, property="impl=persistent")
public class PersistentSessionService extends BaseSessionService {
	private IPersistencyService persistencyService;

	@Override
	public String create(AccessRights accessRights, String projectName) throws SpecmateException {
		ITransaction transaction = persistencyService.openTransaction();
		UserSession session = createSession(accessRights, projectName);
		String token = session.getId(); 
		transaction.getResource().getContents().add(session);
		transaction.commit();
		transaction.close();
		return token;
	}

	@Override
	public String create() {
		// Don't create unauthenticated sessions in persistent storage.
		return "INVALID-SESSION";
	}

	@Override
	public boolean isExpired(String token) throws SpecmateException {
		UserSession session = getSession(token);
		return checkExpiration(session.getLastActive());
	}

	@Override
	public boolean isAuthorized(String token, String path) throws SpecmateException {
		UserSession session = getSession(token);
		return checkAuthorization(session.getAllowedPathPattern(), path);
	}

	@Override
	public void refresh(String token) throws SpecmateException {
		ITransaction transaction = persistencyService.openTransaction();
		UserSession session = (UserSession) transaction.getObjectById(getSessionID(token));
		if (session.getLastActive() - new Date().getTime() > 1000L * 60) {
			session.setLastActive(new Date().getTime());
			transaction.commit();
		}
		transaction.close();
	}

	@Override<commit_msg>Forgot to save...<commit_after>package com.specmate.auth.internal;

import java.util.Date;
import java.util.List;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;

import com.specmate.auth.api.ISessionService;
import com.specmate.auth.config.SessionServiceConfig;
import com.specmate.common.SpecmateException;
import com.specmate.persistency.IPersistencyService;
import com.specmate.persistency.ITransaction;
import com.specmate.persistency.IView;
import com.specmate.usermodel.AccessRights;
import com.specmate.usermodel.UserSession;
import com.specmate.usermodel.UsermodelFactory;

@Component(service = ISessionService.class, configurationPid = SessionServiceConfig.PID, 
	configurationPolicy = ConfigurationPolicy.REQUIRE, property="impl=persistent")
public class PersistentSessionService extends BaseSessionService {
	private static final long SESSION_REFRESH_LIMIT = 1000L * 60; // 60 seconds
	private IPersistencyService persistencyService;

	@Override
	public String create(AccessRights accessRights, String projectName) throws SpecmateException {
		ITransaction transaction = persistencyService.openTransaction();
		UserSession session = createSession(accessRights, projectName);
		String token = session.getId(); 
		transaction.getResource().getContents().add(session);
		transaction.commit();
		transaction.close();
		return token;
	}

	@Override
	public String create() {
		// Don't create unauthenticated sessions in persistent storage.
		return "INVALID-SESSION";
	}

	@Override
	public boolean isExpired(String token) throws SpecmateException {
		UserSession session = getSession(token);
		return checkExpiration(session.getLastActive());
	}

	@Override
	public boolean isAuthorized(String token, String path) throws SpecmateException {
		UserSession session = getSession(token);
		return checkAuthorization(session.getAllowedPathPattern(), path);
	}

	@Override
	public void refresh(String token) throws SpecmateException {
		ITransaction transaction = persistencyService.openTransaction();
		UserSession session = (UserSession) transaction.getObjectById(getSessionID(token));
		long now = new Date().getTime();
		// If we let each request refresh the session, we get errors from CDO regarding out-of-date revision changes.
		// Here we rate limit session refreshes. The better option would be to not store revisions of UserSession 
		// objects, but this is a setting than can be only applied on the whole repository, which we don't want.
		// A third option would be to update sessions with an SQL query, circumventing CDO and revisions altogether.
		if (session.getLastActive() - now > SESSION_REFRESH_LIMIT) {
			session.setLastActive(now);
			transaction.commit();
		}
		transaction.close();
	}

	@Override
{'fraction_non_alphanumeric': 0.0919605612438377, 'fraction_numerical': 0.0026545316647705727, 'mean_word_length': 6.014627659574468, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4233795', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1441, 'n_words': 353}