Added basic auth

This commit is contained in:
MrFry 2020-04-03 17:14:34 +02:00
parent cc8de2e15f
commit daeb08341b

View file

@ -61,8 +61,8 @@
var addEventListener // add event listener function var addEventListener // add event listener function
const serverAdress = 'https://qmining.frylabs.net/' const serverAdress = 'https://qmining.frylabs.net/'
// const serverAdress = 'http://localhost:8080/' // const serverAdress = 'http://localhost:8080/'
const apiAdress = 'https://api.frylabs.net/' // const apiAdress = 'https://api.frylabs.net/'
// const apiAdress = 'http://localhost:8080/' const apiAdress = 'http://localhost:8080/'
const ircAddress = 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining' const ircAddress = 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining'
// forcing pages for testing. unless you test, do not set these to true! // forcing pages for testing. unless you test, do not set these to true!
@ -74,7 +74,8 @@
const log = true const log = true
const motdShowCount = 3 /* Ammount of times to show motd */ const motdShowCount = 3 /* Ammount of times to show motd */
const infoExpireTime = 60 // Every n seconds basic info should be loaded from server // TODO: set to 60
const infoExpireTime = 1 // Every n seconds basic info should be loaded from server
var motd = '' var motd = ''
var lastestVersion = '' var lastestVersion = ''
var subjInfo var subjInfo
@ -96,9 +97,11 @@
passiveTooltip: 'Ha erre kattintasz akkor a script átálítja a neptunban a hallgatói státuszod passzívra', passiveTooltip: 'Ha erre kattintasz akkor a script átálítja a neptunban a hallgatói státuszod passzívra',
retry: 'Újrapróbálás', retry: 'Újrapróbálás',
ircButton: 'IRC', ircButton: 'IRC',
invalidPW: 'Hibás jelszó: ',
search: 'Keresés ...', search: 'Keresés ...',
loading: 'Betöltés ...', loading: 'Betöltés ...',
noServer: 'Nem elérhető a szerver!', noServer: 'Nem elérhető a szerver!',
noUser: 'Nem vagy bejelentkezve!',
passiveModeActivated: 'Passzív mód bekapcsolva, nem lesz mostantól a szerver piszkálva', passiveModeActivated: 'Passzív mód bekapcsolva, nem lesz mostantól a szerver piszkálva',
passiveModeDeactivated: 'Passzív mód kikapcsolva, frissíts az érvénybe lépéshez!', passiveModeDeactivated: 'Passzív mód kikapcsolva, frissíts az érvénybe lépéshez!',
passiveMode: 'Passzív mód', passiveMode: 'Passzív mód',
@ -728,9 +731,30 @@
} }
} }
function Auth (pw) {
SendXHRMessage('login', { pw: pw, script: true })
.then((res) => {
if (res.result === 'success') {
setVal('sessionID', res.sessionID)
ConnectToServer(AfterLoad)
ClearAllMessages()
setPassiveButtonState(false)
} else {
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = texts.invalidPW + pw
})
}
})
}
function ConnectToServer (cwith) { function ConnectToServer (cwith) {
setVal('sessionID', 'gfdgkldf')
ClearAllMessages() ClearAllMessages()
GetXHRInfos().then((inf) => { GetXHRInfos().then((inf) => {
if (inf.result === 'nouser') {
NoUserAction()
return
}
lastestVersion = inf.version lastestVersion = inf.version
motd = inf.motd motd = inf.motd
subjInfo = inf.subjinfo subjInfo = inf.subjinfo
@ -742,6 +766,18 @@
}) })
} }
function NoUserAction () {
SafeGetElementById('menuButtonDiv', (elem) => {
elem.style.backgroundColor = 'red'
})
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = texts.noUser
})
SafeGetElementById('loginDiv', (elem) => {
elem.style.display = ''
})
}
function NoServerAction () { function NoServerAction () {
SafeGetElementById('menuButtonDiv', (elem) => { SafeGetElementById('menuButtonDiv', (elem) => {
elem.style.backgroundColor = 'red' elem.style.backgroundColor = 'red'
@ -1019,7 +1055,7 @@
sentData.id = GetId() sentData.id = GetId()
sentData.quiz = quiz sentData.quiz = quiz
console.log('SENT DATA', sentData) console.log('SENT DATA', sentData)
SendXHRMessage(JSON.stringify(sentData)).then((res) => { SendXHRMessage('isAdding', sentData).then((res) => {
next(res.success, sentData, res.newQuestions) next(res.success, sentData, res.newQuestions)
}) })
} catch (e) { } catch (e) {
@ -1427,7 +1463,7 @@
SetStyle(menuButtonDiv, { SetStyle(menuButtonDiv, {
// width: buttonWidth + 'px', // width: buttonWidth + 'px',
// height: buttonHeight + 'px', // height: buttonHeight + 'px',
top: (window.innerHeight - 90) + 'px', top: (window.innerHeight - 120) + 'px',
left: '10px', left: '10px',
zIndex: 999999, zIndex: 999999,
position: 'fixed', position: 'fixed',
@ -1506,6 +1542,26 @@
margin: '5px' margin: '5px'
}) })
// login div ----------------------------------------------------------------------------------------------------------------
const loginDiv = document.createElement('div')
loginDiv.style.display = 'none'
loginDiv.setAttribute('id', 'loginDiv')
const loginButton = document.createElement('button')
loginButton.innerText = 'Login'
const loginInput = document.createElement('input')
loginInput.type = 'text'
loginDiv.appendChild(loginInput)
loginDiv.appendChild(loginButton)
SetStyle(loginButton, buttonStyle)
loginButton.addEventListener('click', function () {
Auth(loginInput.value)
})
ibuttonCell.appendChild(loginDiv)
// irc button ---------------------------------------------------------------------------------------------------------------- // irc button ----------------------------------------------------------------------------------------------------------------
let ircButton = CreateNodeWithText(ibuttonCell, texts.ircButton, 'button') let ircButton = CreateNodeWithText(ibuttonCell, texts.ircButton, 'button')
SetStyle(ircButton, buttonStyle) SetStyle(ircButton, buttonStyle)
@ -1542,16 +1598,9 @@
passiveButton.title = texts.passiveTooltip passiveButton.title = texts.passiveTooltip
SetStyle(passiveButton, buttonStyle) SetStyle(passiveButton, buttonStyle)
let setPassiveButtonState = (isPassive) => { // APPEND EVERYTHING
menuButtonDiv.style.background = '#262626' appedtTo.appendChild(menuButtonDiv)
retryButton.style.display = 'none'
ircButton.style.display = 'none'
if (!isPassive) {
infoDiv.innerText = texts.loading
} else {
infoDiv.innerText = texts.passiveModeMenuBoxText
}
}
setPassiveButtonState(getVal('skipLoad')) setPassiveButtonState(getVal('skipLoad'))
passiveButton.addEventListener('click', function () { passiveButton.addEventListener('click', function () {
@ -1569,13 +1618,35 @@
ConnectToServer(AfterLoad) ConnectToServer(AfterLoad)
} }
}) })
appedtTo.appendChild(menuButtonDiv)
} catch (e) { } catch (e) {
Exception(e, 'script error at showing menu:') Exception(e, 'script error at showing menu:')
} }
} }
function setPassiveButtonState (isPassive) {
SafeGetElementById('menuButtonDiv', (elem) => {
elem.style.backgroundColor = '#262626'
})
SafeGetElementById('ircButton', (elem) => {
elem.style.display = 'none'
})
SafeGetElementById('retryButton', (elem) => {
elem.style.display = 'none'
})
SafeGetElementById('loginDiv', (elem) => {
elem.style.display = 'none'
})
if (!isPassive) {
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = texts.loading
})
} else {
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = texts.passiveModeMenuBoxText
})
}
}
// : }}} // : }}}
// : Generic utils {{{ // : Generic utils {{{
@ -1628,9 +1699,14 @@
info().script.version + info().script.version +
'&cid=' + GetId() '&cid=' + GetId()
console.log(getVal('sessionID'))
xmlhttpRequest({ xmlhttpRequest({
method: 'GET', method: 'GET',
url: url, url: url,
headers: {
'Cookie': `sessionID=${getVal('sessionID')}`,
'Content-Type': 'application/json'
},
onload: function (response) { onload: function (response) {
try { try {
setVal('lastInfoCheckTime', now) setVal('lastInfoCheckTime', now)
@ -1710,15 +1786,21 @@
}) })
} }
function SendXHRMessage (message) { function SendXHRMessage (path, message) {
message = SUtils.RemoveSpecialChars(message) // message = SUtils.RemoveSpecialChars(message) // TODO: check this
var url = apiAdress + 'isAdding' if (typeof message === 'object') {
message = JSON.stringify(message)
}
const url = apiAdress + path
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log(getVal('sessionID'))
xmlhttpRequest({ xmlhttpRequest({
method: 'POST', method: 'POST',
url: url, url: url,
xhrFields: { withCredentials: true },
data: message, data: message,
headers: { headers: {
'Cookie': `sessionID=${getVal('sessionID')}`,
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
onerror: function (e) { onerror: function (e) {
@ -1730,7 +1812,7 @@
const res = JSON.parse(resp.responseText) const res = JSON.parse(resp.responseText)
resolve(res) resolve(res)
} catch (e) { } catch (e) {
Log('Errro paring JSON in SendXHRMessage') Log('Error paring JSON in SendXHRMessage')
Log(e) Log(e)
reject(e) reject(e)
} }