<commit_before>/**
 * Two Step Authentication
 * Gold Server - http://gold.psim.us/
 *
 * This provides a more secure means of authenticating a
 * login, as another layer of security
 *
 * Credits: panpawn, jd
 *
 * @license MIT license
 */

'use strict';

const CONFIRMATION_CODE_TIMEOUT_MINUTES = 2;
const TWO_STEP_CMD = '/twostep login ';
const nodemailer = require('nodemailer');
const EMAIL_OPTIONS = Config.twostep.options;

const transporter = nodemailer.createTransport(EMAIL_OPTIONS);

Gold.TwoStepAuth = {
	codes: {},
	generateCode: function (userid) {
		let randCode = Math.floor(Math.random() * 90000) + 10000;
		this.codes[userid] = randCode;
		setTimeout(() => {
			delete this.codes[userid];
		}, (CONFIRMATION_CODE_TIMEOUT_MINUTES * 60000));
	},
	verifyEmail: function (user, verification) {
		return this.sendEmail(user.userid, verification);
	},
	verifyCode: function (userid, userObj, input, connection) {
		let userCode = this.codes[userid];
		if (!userCode) return this.failLogin(connection, "No code was given");
		if (userCode === input) {
			this.passLogin(userObj, connection);
		} else {
			this.failLogin(connection, `You entered the wrong verification pin. <br /><button class="button" name="send" value="${TWO_STEP_CMD}restart">Try again</button>`);
		}
	},
	sendEmail: function (userid, verification, connection) {
		this.generateCode(userid);
		let data = Gold.userData[userid];
		let sendTo = (verification ? verification.email : data.email);
		if (!sendTo && connection) return this.failLogin(connection, "Your account does not have an email associated with it. (Weird?)");
		let userCode = this.codes[userid];
		if (!userCode && connection) return this.failLogin(connection, "Your passcode has expired.");
		let description = (verification ? verification.message :
		`Hello, ${userid}:\n\nYour two-step authentication code to login to Gold is: ${userCode}\n\nReminder that this code will expire in ${CONFIRMATION_CODE_TIMEOUT_MINUTES} minutes from the moment this email sent.\n\nThanks for using Gold's two step authentication,\n— Gold Administration`);
		transporter.sendMail({
			from: EMAIL_OPTIONS.from,
			to: sendTo,
			subject: "Gold 2-Step Authentication",
			text: description,
		}, err => {
			if (err) console.error(`Error sending email: ${err}`);<commit_msg>Two step: Increase timeout & give IP of login<commit_after>/**
 * Two Step Authentication
 * Gold Server - http://gold.psim.us/
 *
 * This provides a more secure means of authenticating a
 * login, as another layer of security
 *
 * Credits: panpawn, jd
 *
 * @license MIT license
 */

'use strict';

const CONFIRMATION_CODE_TIMEOUT_MINUTES = 3;
const TWO_STEP_CMD = '/twostep login ';
const nodemailer = require('nodemailer');
const EMAIL_OPTIONS = Config.twostep.options;

const transporter = nodemailer.createTransport(EMAIL_OPTIONS);

Gold.TwoStepAuth = {
	codes: {},
	generateCode: function (userid) {
		let randCode = Math.floor(Math.random() * 90000) + 10000;
		this.codes[userid] = randCode;
		setTimeout(() => {
			delete this.codes[userid];
		}, (CONFIRMATION_CODE_TIMEOUT_MINUTES * 60000));
	},
	verifyEmail: function (user, verification) {
		return this.sendEmail(user.userid, verification);
	},
	verifyCode: function (userid, userObj, input, connection) {
		let userCode = this.codes[userid];
		if (!userCode) return this.failLogin(connection, "No code was given");
		if (userCode === input) {
			this.passLogin(userObj, connection);
		} else {
			this.failLogin(connection, `You entered the wrong verification pin. <br /><button class="button" name="send" value="${TWO_STEP_CMD}restart">Try again</button>`);
		}
	},
	sendEmail: function (userid, verification, connection) {
		this.generateCode(userid);
		let data = Gold.userData[userid];
		let sendTo = (verification ? verification.email : data.email);
		if (!sendTo && connection) return this.failLogin(connection, "Your account does not have an email associated with it. (Weird?)");
		let userCode = this.codes[userid];
		if (!userCode && connection) return this.failLogin(connection, "Your passcode has expired.");
		let description = (verification ? verification.message :
		`Hello, ${userid}:\n\nYour two-step authentication code to login to Gold is: ${userCode}\n\nReminder that this code will expire in ${CONFIRMATION_CODE_TIMEOUT_MINUTES} minutes from the moment this email sent... Did you not attempt to login to Gold a few minutes ago? A user with the IP of ${connection.ip} tried to access your account with your username and password.\n\nThanks for using Gold's two step authentication,\n— Gold Administration`);
		transporter.sendMail({
			from: EMAIL_OPTIONS.from,
			to: sendTo,
			subject: "Gold 2-Step Authentication",
			text: description,
		}, err => {
			if (err) console.error(`Error sending email: ${err}`);
{'fraction_non_alphanumeric': 0.12669306105438632, 'fraction_numerical': 0.007084809335278183, 'mean_word_length': 4.825242718446602, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '4013475', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1482, 'n_words': 441}