<commit_before>'use strict';

let credentials = {
    key: null,
    cert: null
};

let fs = require('fs');
let execSync = require('child_process').execSync;

class SecurityHelper {
    /**
     * Loads private key and certificate
     *
     * @param {String} siteName
     *
     * @returns {Promise} Result
     */
    static loadCredentials(siteName = 'localhost') {
        if(!fs.existsSync(appRoot + '/ssl')) {
            fs.mkdirSync(appRoot + '/ssl');
        }
        
        execSync('openssl genrsa -out ' + appRoot + '/ssl/server.key');
        execSync('openssl req -new -key ' + appRoot + '/ssl/server.key -out ' + appRoot + '/ssl/server.csr -subj "/C=DK/ST=Copenhagen/L=Copenhagen/O=Putaitu Productions/OU=Development/CN=' + siteName + '"');
        execSync('openssl x509 -req -in ' + appRoot + '/ssl/server.csr -signkey ' + appRoot + '/ssl/server.key -out ' + appRoot + '/ssl/server.crt');
        
        credentials.key = fs.readFileSync(appRoot + '/ssl/server.key', 'UTF-8');
        credentials.cert = fs.readFileSync(appRoot + '/ssl/server.crt', 'UTF-8');
    }

    /**
     * Gets the private key and certificate
     *
     * @returns {Object} Key and cert
     */
    static getCredentials() {
        if(!credentials.key || !credentials.cert) {
            this.loadCredentials();
        }

        return credentials;
    }

    /**
     * Starts the letsencrypt handler
     *
     * @returns {Object} Let's encrypt handler
     */
    static startLetsEncrypt() {
        let sslConfigPath = appRoot + '/config/mail.cfg';
        
        if(!fs.existsSync(sslConfigPath) { return null; }

        let config = JSON.parse(fs.readFileSync(sslConfigPath)); 

        // Config sanity check
        if(!config.domain) {
            throw new Error('Variable "domain" not set in /config/ssl.cfg');
        }
        
        if(!config.email) {
            throw new Error('Variable "email" not set in /config/ssl.cfg');
        }
        
        // Storage backend
        let leStore = require('le-store-certbot').create({
            configDir: appRoot + '/certs',
            debug: false<commit_msg>Implementing SSL<commit_after>'use strict';

let credentials = {
    key: null,
    cert: null
};

let fs = require('fs');
let execSync = require('child_process').execSync;

class SecurityHelper {
    /**
     * Loads private key and certificate
     *
     * @param {String} siteName
     *
     * @returns {Promise} Result
     */
    static loadCredentials(siteName = 'localhost') {
        if(!fs.existsSync(appRoot + '/ssl')) {
            fs.mkdirSync(appRoot + '/ssl');
        }
        
        execSync('openssl genrsa -out ' + appRoot + '/ssl/server.key');
        execSync('openssl req -new -key ' + appRoot + '/ssl/server.key -out ' + appRoot + '/ssl/server.csr -subj "/C=DK/ST=Copenhagen/L=Copenhagen/O=Putaitu Productions/OU=Development/CN=' + siteName + '"');
        execSync('openssl x509 -req -in ' + appRoot + '/ssl/server.csr -signkey ' + appRoot + '/ssl/server.key -out ' + appRoot + '/ssl/server.crt');
        
        credentials.key = fs.readFileSync(appRoot + '/ssl/server.key', 'UTF-8');
        credentials.cert = fs.readFileSync(appRoot + '/ssl/server.crt', 'UTF-8');
    }

    /**
     * Gets the private key and certificate
     *
     * @returns {Object} Key and cert
     */
    static getCredentials() {
        if(!credentials.key || !credentials.cert) {
            this.loadCredentials();
        }

        return credentials;
    }

    /**
     * Starts the letsencrypt handler
     *
     * @returns {Object} Let's encrypt handler
     */
    static startLetsEncrypt() {
        let sslConfigPath = appRoot + '/config/mail.cfg';
        
        if(!fs.existsSync(sslConfigPath)) { return null; }

        let config = JSON.parse(fs.readFileSync(sslConfigPath)); 

        // Config sanity check
        if(!config.domain) {
            throw new Error('Variable "domain" not set in /config/ssl.cfg');
        }
        
        if(!config.email) {
            throw new Error('Variable "email" not set in /config/ssl.cfg');
        }
        
        // Storage backend
        let leStore = require('le-store-certbot').create({
            configDir: appRoot + '/certs',
            debug: false
{'fraction_non_alphanumeric': 0.1442937324602432, 'fraction_numerical': 0.0023386342376052385, 'mean_word_length': 2.085858585858586, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '112749', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1285, 'n_words': 284}