/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2025 Rainbownapkin and the TTN Community

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.*/

//Config
const config = require('../../config.json');

//Node Imports
const crypto = require('node:crypto');

//NPM Imports
const argon2 = require('argon2');
const bcrypt = require('bcrypt');

/**
 * Sitewide function for hashing passwords
 * @param {String} pass - Password to hash
 * @returns {String} Hashed/Salted password
 */
module.exports.hashPassword = async function(pass){
    //Hash password with argon2id
    return await argon2.hash(pass, {secret: Buffer.from(config.secrets.passwordSecret)});
}

/**
 * Sitewide method for authenticating/comparing passwords agianst hashes
 * @param {String} pass - Plaintext Password
 * @param {String} hash - Salty Hash
 * @returns {Boolean} True if authentication success
 */
module.exports.comparePassword = async function(pass, hash){
    //Verify password against argon2 hash
    return await argon2.verify(hash, pass, {secret: Buffer.from(config.secrets.passwordSecret)});
}

/**
 * Sitewide method for authenticating/comparing passwords agianst hashes for legacy profiles
 * @param {String} pass - Plaintext Password
 * @param {String} hash - Salty Hash
 * @returns {Boolean} True if authentication success
 */
module.exports.compareLegacyPassword = function(pass, hash){
    return bcrypt.compareSync(pass, hash);
}

/**
 * Site-wide IP hashing/salting function
 * 
 * Provides a basic level of privacy by only logging salted hashes of IP's
 * @param {String} ip - IP to hash
 * @returns {String} Hashed/Peppered IP Adress
 */
module.exports.hashIP = function(ip){
    //Create hash object
    const hashObj = crypto.createHash('sha512');

    //add IP and pepper to the hash
    hashObj.update(`${ip}${config.secrets.ipSecret}`);

    //return the IP hash as a string
    return hashObj.digest('hex');
}

/**
 * Site-wide remember-me token hashing function
 * @param {String} token - Token to hash
 * @returns {String} - Hashed token
 */
module.exports.hashRememberMeToken = async function(token){
    //hash token with argon2id
    return await argon2.hash(token, {secret: Buffer.from(config.secrets.rememberMeSecret)});
}

/**
 * Site-wide remember-me token hash comparison function
 * @param {String} token - Token to compare
 * @param {String} hash - Hash to compare
 * @returns {String} - Comparison results
 */
module.exports.compareRememberMeToken = async function(token, hash){
    //Compare hash and return result
    return await argon2.verify(hash, token, {secret: Buffer.from(config.secrets.rememberMeSecret)});
}