<commit_before>package picoded.RESTBuilder.template.social;

import picoded.RESTBuilder.RESTBuilder;
import picoded.RESTBuilder.RESTFunction;
import picoded.enums.HttpRequestType;
import picoded.servlet.CommonsPage;
import picoded.conv.ConvertJSON;

import java.util.Base64;
import java.util.Map;
import java.util.HashMap;
import java.util.Formatter;

import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Mac;

// import org.codehaus.jackson.map.ObjectMapper;

public class DisqusIntegration {

	public DisqusIntegration(){

	}

	///
	/// Generates disqus SSO information
	///
	/// ## HTTP Request Parameters
	///
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | Parameter Name  | Variable Type	   | Description                                                                   |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | _oid            | String             | object ID used to generate the string. If no oid is given, return null.       |
	/// | username        | String             | Username used to generate the string. If no username is given, return null.   |
	/// | email           | String             | Email used to generate the string. If no email is given, return null.         |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	///
	/// ## JSON Object Output Parameters
	///
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | Parameter Name  | Variable Type	   | Description                                                                   |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | hmac_sha1       | String             | The generated hmac_sha1 string for SSO.                                       |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | error           | String (Optional)  | Errors encounted if any                                                       |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	///
	public RESTFunction sso_hmac_sha1 = (req, res) -> {
		res.put("error", null);
		res.put("hmac_sha1", null);
		res.put("restLog", "");

		String oid = req.getString("_oid", "");
		if(oid.isEmpty()){
			res.put("error", "No _oid specified, unable to generate hmac_sha1");
			return res;
		}

		String username = req.getString("username", "");
		if(username.isEmpty()){
			res.put("error", "No username specified, unable to generate hmac_sha1");
			return res;
		}

		String email = req.getString("email", "");
		if(email.isEmpty()){
			res.put("error", "No email specified, unable to generate hmac_sha1");
			return res;
		}

		CommonsPage corePageObj = (CommonsPage)(req.requestPage());
		String secretKey = corePageObj.JConfig().getString("site.apiKey.disqus.secretKey", "");
		if(secretKey.isEmpty()){
			res.put("error", "No secret key found, unable to generate hmac_sha1");
			return res;
		}

		String publicKey = corePageObj.JConfig().getString("site.apiKey.disqus.publicKey", "");
		if(publicKey.isEmpty()){
			res.put("error", "No public key found, unable to generate hmac_sha1");
			return res;
		}
		res.put("publicKey", publicKey);

		Map<String, String> message = new HashMap<String, String>();
		message.put("id", oid);
		message.put("username", username);
		message.put("email", email);

		//convert message object to json string, then base64 encode it
		String jsonMessage = ConvertJSON.fromObject(message);
		String base64EncodedStr = new String(Base64.getEncoder().encodeToString(jsonMessage.getBytes()));

		long timestamp = System.currentTimeMillis()/1000;

		// Assemble the HMAC-SHA1 signature
		String signature = "";
		try{
			signature = calculateRFC2104HMAC(base64EncodedStr + " " + timestamp, secretKey);
		}catch(Exception e){
			res.put("error", e.getMessage());
			return res;
		}

		res.put("hmac_sha1", signature);
		return res;
	};

	private static String toHexString(byte[] bytes)
	{
		Formatter formatter = new Formatter();
		for (byte b : bytes)
		{
			formatter.format("%02x", b);
		}

		return formatter.toString();
	}

	private static String calculateRFC2104HMAC(String data, String key) throws Exception
	{
		String HMAC_SHA1_ALGORITHM = "HmacSHA1";
		SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
		Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
		mac.init(signingKey);
		return toHexString(mac.doFinal(data.getBytes()));
	}

	public RESTBuilder setupRestBuilder(RESTBuilder rbObj, String setPrefix) {
		rbObj.getNamespace(setPrefix + "sso_hmac_sha1").put(HttpRequestType.GET, sso_hmac_sha1);

		return rbObj;
	}
}<commit_msg>Updated disqus rest to have login and logout<commit_after>package picoded.RESTBuilder.template.social;

import picoded.RESTBuilder.RESTBuilder;
import picoded.RESTBuilder.RESTFunction;
import picoded.enums.HttpRequestType;
import picoded.servlet.CommonsPage;
import picoded.servlet.BasePage;
import picoded.conv.ConvertJSON;
import picoded.JStruct.AccountTable;
import picoded.JStruct.AccountObject;

import java.util.Base64;
import java.util.Map;
import java.util.HashMap;
import java.util.Formatter;
import java.util.List;
import java.util.ArrayList;

import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Mac;

public class DisqusIntegration {

	public DisqusIntegration(){

	}

	/// Generates disqus SSO information
	///
	/// ## HTTP Request Parameters
	///
	/// ## JSON Object Output Parameters
	///
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | Parameter Name  | Variable Type	   | Description                                                                   |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | hmac_sha1       | String             | The generated hmac_sha1 string for SSO.                                       |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | error           | String (Optional)  | Errors encounted if any                                                       |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	///
	public RESTFunction sso_login = (req, res) -> {
		res.put("error", null);
		res.put("hmac_sha1", null);
		res.put("restLog", "");

		List<String> restLog = new ArrayList<String>();

		BasePage bp = (BasePage)req.requestPage();
		AccountTable at = bp.accountAuthTable();
		AccountObject ao = at.getRequestUser(bp.getHttpServletRequest());

		if(ao == null){
			res.put("restLog", "User is not logged in.");
		}else{
			String oid = ao._oid();
			if(oid == null || oid.isEmpty()){
				res.put("error", "No oid found for user.");
				return res;
			}

			String username = ao.getString("username", "test_user"); //change me
			String email = ao.getString("email", "<EMAIL>"); //change me

			String secretKey = bp.JConfig().getString("site.apiKey.disqus.secretKey", "");
			if(secretKey.isEmpty()){
				res.put("error", "No secret key found, unable to generate hmac_sha1 for disqus login");
				return res;
			}

			String publicKey = bp.JConfig().getString("site.apiKey.disqus.publicKey", "");
			if(publicKey.isEmpty()){
				res.put("error", "No public key found, unable to generate hmac_sha1 for disqus login");
				return res;
			}
			res.put("publicKey", publicKey);

			Map<String, String> message = new HashMap<String, String>();
			message.put("id", oid);
			message.put("username", username);
			message.put("email", email);

			String signature = "";
			try{
				signature = sso_hmac_sha1(message, secretKey);
			}catch(Exception e){
				res.put("error", e.getMessage());
				return res;
			}
			res.put("hmac_sha1", signature);
		}

		res.put("restLog", restLog);
		return res;
	};

	/// Generates disqus SSO information
	///
	/// ## HTTP Request Parameters
	///
	/// ## JSON Object Output Parameters
	///
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | Parameter Name  | Variable Type	   | Description                                                                   |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | hmac_sha1       | String             | The generated hmac_sha1 string for SSO.                                       |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	/// | error           | String (Optional)  | Errors encounted if any                                                       |
	/// +-----------------+--------------------+-------------------------------------------------------------------------------+
	///
	public RESTFunction sso_logout = (req, res) -> {
		res.put("error", null);
		res.put("hmac_sha1", null);
		res.put("restLog", "");

		BasePage bp = (BasePage)req.requestPage();

		String secretKey = bp.JConfig().getString("site.apiKey.disqus.secretKey", "");
		if(secretKey.isEmpty()){
			res.put("error", "No secret key found, unable to generate hmac_sha1 for disqus logout");
			return res;
		}

		String publicKey = bp.JConfig().getString("site.apiKey.disqus.publicKey", "");
		if(publicKey.isEmpty()){
			res.put("error", "No public key found, unable to generate hmac_sha1 for disqus logout");
			return res;
		}
		res.put("publicKey", publicKey);

		Map<String, String> message = new HashMap<String, String>();
		String signature = "";
		try{
			signature = sso_hmac_sha1(message, secretKey);
		}catch(Exception e){
			res.put("error", e.getMessage());
			return res;
		}
		res.put("hmac_sha1", signature);

		return res;
	};

	private static String sso_hmac_sha1(Map<String, String> message, String secretKey) throws Exception{
		//convert message object to json string, then base64 encode it
		String jsonMessage = ConvertJSON.fromObject(message);
		String base64EncodedStr = new String(Base64.getEncoder().encodeToString(jsonMessage.getBytes()));

		long timestamp = System.currentTimeMillis()/1000;

		// Assemble the HMAC-SHA1 signature
		String signature = calculateRFC2104HMAC(base64EncodedStr + " " + timestamp, secretKey);
		return base64EncodedStr + " " + signature + " " + timestamp;
	}

	private static String toHexString(byte[] bytes)
	{
		Formatter formatter = new Formatter();
		for (byte b : bytes)
		{
			formatter.format("%02x", b);
		}

		return formatter.toString();
	}

	private static String calculateRFC2104HMAC(String data, String key) throws Exception
	{
		String HMAC_SHA1_ALGORITHM = "HmacSHA1";
		SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
		Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
		mac.init(signingKey);
		return toHexString(mac.doFinal(data.getBytes()));
	}

	public RESTBuilder setupRestBuilder(RESTBuilder rbObj, String setPrefix) {
		rbObj.getNamespace(setPrefix + "sso_login").put(HttpRequestType.GET, sso_login);
		rbObj.getNamespace(setPrefix + "sso_login").put(HttpRequestType.POST, sso_login);

		rbObj.getNamespace(setPrefix + "sso_logout").put(HttpRequestType.GET, sso_logout);
		rbObj.getNamespace(setPrefix + "sso_logout").put(HttpRequestType.POST, sso_logout);

		return rbObj;
	}
}
{'fraction_non_alphanumeric': 0.2656170644997461, 'fraction_numerical': 0.007364144235652616, 'mean_word_length': 1.7381228273464657, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5830679', 'n_tokens_mistral': 3435, 'n_tokens_neox': 3118, 'n_words': 714}