70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
const Cryptr = require('cryptr');
|
|
const cryptr = new Cryptr('BBLBTV-DNS-PASSWORDS');
|
|
|
|
|
|
const sql = require('./mysql')
|
|
|
|
function storeAccountToDB(accountDetails) {
|
|
const encryptedPassword = cryptr.encrypt(accountDetails.password);
|
|
const result = sql.query(`INSERT userAccounts (username, password, stream, userID, expiaryDate, streamURL, maxConnections) VALUES ("${accountDetails.username}", "${encryptedPassword}", "${accountDetails.streamName}", ${accountDetails.userId}, ${accountDetails.expiryDate}, "${accountDetails.streamURL}", ${accountDetails.maxConnections})`);
|
|
return result
|
|
}
|
|
|
|
function removeAccountFromDB(accountDetails) {
|
|
const result = sql.query(`DELETE FROM userAccounts WHERE
|
|
username = '${accountDetails.user}' AND
|
|
stream = '${accountDetails.stream}' AND
|
|
userId = ${accountDetails.userId} `);
|
|
return result
|
|
}
|
|
|
|
|
|
function retrievePasswordFromDB(user, userAccUser) {
|
|
let userId
|
|
try {
|
|
userId = sql.query(`SELECT u.id FROM users u WHERE u.userName = '${user}'`);
|
|
userId = userId[0].id
|
|
} catch (error) {
|
|
console.log('User not found')
|
|
return (error)
|
|
}
|
|
|
|
let accountPassword
|
|
|
|
accountPassword = sql.query(`SELECT DISTINCT
|
|
userAccounts.username,
|
|
userAccounts.password,
|
|
userAccounts.userID
|
|
FROM users,
|
|
userAccounts,
|
|
streams
|
|
WHERE userAccounts.userID = ${userId} AND userAccounts.username = '${userAccUser}'`)
|
|
|
|
if (accountPassword == 0) {
|
|
throw new Error('Account ' + userAccUser + ' not found for user ' + user)
|
|
} else {
|
|
const decryptedString = cryptr.decrypt(accountPassword[0].password);
|
|
console.log(decryptedString)
|
|
return decryptedString
|
|
}
|
|
}
|
|
|
|
// accountDetails = {
|
|
// "username": "Darren110921",
|
|
// "password" :"saD2V2kjyNqH",
|
|
// "stream": "Badger",
|
|
// "userId" : "2"
|
|
// }
|
|
|
|
|
|
|
|
// retrievePasswordFromDB('Karl', 'Maxine2206')
|
|
// storeAccountToDB(accountDetails)
|
|
|
|
module.exports = {
|
|
removeAccountFromDB,
|
|
storeAccountToDB,
|
|
retrievePasswordFromDB
|
|
}
|
|
|