From 291a571b228871bb971e4385944d028f8cc36c6d Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Tue, 9 Jan 2024 17:44:56 +0300 Subject: [PATCH] work on auth --- app/app.ejs | 1 + app/assets/js/authmanager.js | 50 +++++++- app/assets/js/configmanager.js | 11 ++ app/assets/js/distromanager.js | 4 +- app/assets/js/scripts/loginOptions.js | 9 ++ app/assets/js/scripts/loginSkirdaDiscord.js | 125 ++++++++++++++++++++ app/assets/js/scripts/settings.js | 102 +++++++++++----- app/assets/js/scripts/uibinder.js | 1 + app/assets/js/skirda-auth/auth.js | 0 app/loginOptions.ejs | 11 ++ app/loginSkirdaDiscord.ejs | 28 +++++ app/settings.ejs | 41 +++++++ package-lock.json | 38 ++++++ package.json | 1 + 14 files changed, 392 insertions(+), 30 deletions(-) create mode 100644 app/assets/js/scripts/loginSkirdaDiscord.js create mode 100644 app/assets/js/skirda-auth/auth.js create mode 100644 app/loginSkirdaDiscord.ejs diff --git a/app/app.ejs b/app/app.ejs index edac3f9..679f6f0 100644 --- a/app/app.ejs +++ b/app/app.ejs @@ -34,6 +34,7 @@ <%- include('waiting') %> <%- include('loginOptions') %> <%- include('loginOffline') %> + <%- include('loginSkirdaDiscord') %> <%- include('settings') %> <%- include('landing') %> diff --git a/app/assets/js/authmanager.js b/app/assets/js/authmanager.js index f9acc6c..9515fd2 100644 --- a/app/assets/js/authmanager.js +++ b/app/assets/js/authmanager.js @@ -42,7 +42,7 @@ exports.addMojangAccount = async function(username, password){ clientToken: 'asdasldkjalskdj', } if(session.selectedProfile != null){ - console.log("here") + // console.log("here") const ret = ConfigManager.addMojangAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name) if(ConfigManager.getClientToken() == null){ ConfigManager.setClientToken(session.clientToken) @@ -63,6 +63,18 @@ exports.addMojangAccount = async function(username, password){ } } + +/** + * @param {string} uuid + * @param {string} accessToken + * @param {string} username + * @param {string} displayName + * */ +exports.addSkirdaAccount = async function(uuid, accessToken, username, displayName){ + ConfigManager.addSkirdaAccount(uuid, accessToken, username, displayName) + ConfigManager.save() +} + exports.addOfflineAccount = async function(usernameOffline){ //TODO: check for forbidden symbols and lenght!! ConfigManager.addOfflineAccount(usernameOffline) @@ -211,6 +223,42 @@ exports.removeMicrosoftAccount = async function(uuid){ } } +/** + * Remove a Offline account. It is expected that the caller will invoke the OAuth logout + * through the ipc renderer. + * + * @param {string} uuid The UUID of the account to be removed. + * @returns {Promise.} Promise which resolves to void when the action is complete. + */ +exports.removeOfflineAccount = async function(uuid){ + try { + ConfigManager.removeAuthAccount(uuid) + ConfigManager.save() + return Promise.resolve() + } catch (err){ + log.error('Error while removing account', err) + return Promise.reject(err) + } +} + +/** + * Remove a Skirda account. It is expected that the caller will invoke the OAuth logout + * through the ipc renderer. + * + * @param {string} uuid The UUID of the account to be removed. + * @returns {Promise.} Promise which resolves to void when the action is complete. + */ +exports.removeSkirdaAccount = async function(uuid){ + try { + ConfigManager.removeAuthAccount(uuid) + ConfigManager.save() + return Promise.resolve() + } catch (err){ + log.error('Error while removing account', err) + return Promise.reject(err) + } +} + /** * Validate the selected account with Mojang's authserver. If the account is not valid, * we will attempt to refresh the access token and update that value. If that fails, a diff --git a/app/assets/js/configmanager.js b/app/assets/js/configmanager.js index 6299ff5..0cbfce9 100644 --- a/app/assets/js/configmanager.js +++ b/app/assets/js/configmanager.js @@ -364,6 +364,17 @@ exports.addOfflineAccount = function(usernameOffline){ return config.authenticationDatabase[fake_uuid] } +exports.addSkirdaAccount = function(uuid, accessToken, username, displayName){ + config.selectedAccount = uuid + config.authenticationDatabase[uuid] = { + type: 'skirda', + accessToken, + username: username.trim(), + uuid: uuid.trim(), + displayName: displayName.trim() + } + return config.authenticationDatabase[uuid] +} /** * Update the tokens of an authenticated microsoft account. diff --git a/app/assets/js/distromanager.js b/app/assets/js/distromanager.js index 8d690d2..3e16daa 100644 --- a/app/assets/js/distromanager.js +++ b/app/assets/js/distromanager.js @@ -5,8 +5,8 @@ const ConfigManager = require('./configmanager') // Old WesterosCraft url. // exports.REMOTE_DISTRO_URL = 'http://mc.westeroscraft.com/WesterosCraftLauncher/distribution.json' // exports.REMOTE_DISTRO_URL = 'https://skirda-minecraft-distribution.brzezinski.ru/distribution.json' -exports.REMOTE_DISTRO_URL = 'https://helios-files.geekcorner.eu.org/distribution.json' -// exports.REMOTE_DISTRO_URL = 'http://192.168.88.10:8080/distribution/manifest' +// exports.REMOTE_DISTRO_URL = 'https://helios-files.geekcorner.eu.org/distribution.json' +exports.REMOTE_DISTRO_URL = 'http://192.168.88.10:8080/distribution/manifest' // exports.REMOTE_DISTRO_URL = 'http://gregbrzezinski:8080/distribution/manifest' // exports.REMOTE_DISTRO_URL = 'http://localhost:8080/distribution/files/files/distribution.json' diff --git a/app/assets/js/scripts/loginOptions.js b/app/assets/js/scripts/loginOptions.js index 678f158..ef7a528 100644 --- a/app/assets/js/scripts/loginOptions.js +++ b/app/assets/js/scripts/loginOptions.js @@ -2,6 +2,7 @@ const loginOptionsCancelContainer = document.getElementById('loginOptionCancelCo const loginOptionMicrosoft = document.getElementById('loginOptionMicrosoft') const loginOptionMojang = document.getElementById('loginOptionMojang') const loginOptionOffline = document.getElementById('loginOptionOffline') +const loginOptionSkirdaDiskord = document.getElementById('loginOptionSkirdaDiscord') const loginOptionsCancelButton = document.getElementById('loginOptionCancelButton') let loginOptionsCancellable = false @@ -46,6 +47,14 @@ loginOptionOffline.onclick = (e) => { }) } +loginOptionSkirdaDiscord.onclick = (e) => { + switchView(getCurrentView(), VIEWS.loginSkirdaDiscord, 500, 500, () => { + loginViewOnSuccess = loginOptionsViewOnLoginSuccess + loginViewOnCancel = loginOptionsViewOnLoginCancel + loginCancelEnabled(true) + }) +} + loginOptionsCancelButton.onclick = (e) => { switchView(getCurrentView(), loginOptionsViewOnCancel, 500, 500, () => { // Clear login values (Mojang login) diff --git a/app/assets/js/scripts/loginSkirdaDiscord.js b/app/assets/js/scripts/loginSkirdaDiscord.js new file mode 100644 index 0000000..57c4060 --- /dev/null +++ b/app/assets/js/scripts/loginSkirdaDiscord.js @@ -0,0 +1,125 @@ +// const http = require('http') +// const fs = require('fs') +// const {addSkirdaAccount} = require('configmanager') +const auth_api_url = 'http://192.168.88.10:8083' + +class SkirdaDiscordAuth{ + /** + * @typedef {Object} DiscordRedirectAuth + * @property {string} redirectUrl how the person is called + * @property {string} requestId how many years the person lived + * + * @return {Promise} + * */ + temp_installId = '26c175e2-71bd-4d80-9553-cc1575fc8ef9' + async Init(){ + const resp = await fetch(`${auth_api_url}/v1/auth/discord/init?installId=${this.temp_installId}`) + + if (resp.statusCode !== 200){ + return await resp.text() + } + + return await resp.json() + } + + /** + * @param requestId {string} Request id from init route + // * @param timeout {number} Timeout to poll in seconds + * @return {SkirdaUserResp | string} Result of OAuth login + */ + async CyclePolling(requestId){ + for (let i = 0; i < 15; i++) { + const resp = await this._poll(requestId) + + // console.log(resp) + + switch (resp.status){ + case 204: + await this._sleep(4000) + continue + case 200: + return await resp.json() + default: + console.error(await resp.text()) + return 'TODO error' + } + + + + + } + } + + + /** + * @typedef {Object} SkirdaUserResp + * @property skirdaUserId {string} + * @property username {string} + * @property token {string} + * + * @param requestId {string} + * @return {Response} + * */ + async _poll(requestId){ + const resp = await fetch(`${auth_api_url}/v1/auth/discord/periodicPolling?installId=${this.temp_installId}&requestId=${requestId}`) + return resp + } + + _sleepSetTimeout_ctrl + /** + * @param ms {number} + * @return {boolean} + */ + async _sleep(ms) { + clearInterval(this._sleepSetTimeout_ctrl) + return new Promise(resolve => this._sleepSetTimeout_ctrl = setTimeout(resolve, ms)) + } +} + + + + +const auth = new SkirdaDiscordAuth() +const loginSkirdaDiscordButton = document.getElementById('loginSkirdaDiscordInitAuth') + +loginSkirdaDiscordButton.addEventListener('click', async () =>{ + const res = await auth.Init() + + const redir = JSON.parse(res) //FIXME + console.log(redir) + const resOpenUrl = await shell.openExternal(redir.redirectUrl) + + const skirdaAuth = await auth.CyclePolling(redir.requestId) + // console.log(skirdaAuth) + // skirdaAuth.skirdaUserId = '2a5fd868-1ac5-4ccf-a22f-183822de2d61' + + let account = { + uuid: '2a5fd868-1ac5-4ccf-a22f-183822de2d61', + token: '', + username: skirdaAuth.skirdaUserId, + displayName: skirdaAuth.username + } + + AuthManager.addSkirdaAccount(account.uuid, account.token, account.username, account.displayName) + + setTimeout(() => { + switchView(VIEWS.landing, VIEWS.landing, 500, 500, async () => { + console.log('kek') + // // Temporary workaround + // if(loginViewOnSuccess === VIEWS.settings){ + // await prepareSettings() + // } + // // loginViewOnSuccess = VIEWS.landing // Reset this for good measure. + // // loginCancelEnabled(false) // Reset this for good measure. + // // loginViewCancelHandler = null // Reset this for good measure. + // // loginUsername.value = '' + // // loginPassword.value = '' + // $('.circle-loader').toggleClass('load-complete') + // $('.checkmark').toggle() + // loginLoading(false) + // loginButton.innerHTML = loginButton.innerHTML.replace(Lang.queryJS('login.success'), Lang.queryJS('login.login')) + // formDisabled(false) + }) + }, 1000) +}) + diff --git a/app/assets/js/scripts/settings.js b/app/assets/js/scripts/settings.js index 81a65a7..34d1f69 100644 --- a/app/assets/js/scripts/settings.js +++ b/app/assets/js/scripts/settings.js @@ -351,6 +351,12 @@ document.getElementById('settingsAddMojangAccount').onclick = (e) => { }) } +document.getElementById('settingsAddSkirdaDiscord').onclick = (e) => { + // console.log("kekekekekek") + switchView(getCurrentView(), VIEWS.loginSkirdaDiscord, 500, 500, () => { + }) +} + // Bind the add microsoft account button. document.getElementById('settingsAddMicrosoftAccount').onclick = (e) => { switchView(getCurrentView(), VIEWS.waiting, 500, 500, () => { @@ -513,30 +519,50 @@ function processLogOut(val, isLastAccount){ const uuid = parent.getAttribute('uuid') const prevSelAcc = ConfigManager.getSelectedAccount() const targetAcc = ConfigManager.getAuthAccount(uuid) - if(targetAcc.type === 'microsoft') { - msAccDomElementCache = parent - switchView(getCurrentView(), VIEWS.waiting, 500, 500, () => { - ipcRenderer.send(MSFT_OPCODE.OPEN_LOGOUT, uuid, isLastAccount) - }) - } else { - AuthManager.removeMojangAccount(uuid).then(() => { - if(!isLastAccount && uuid === prevSelAcc.uuid){ - const selAcc = ConfigManager.getSelectedAccount() - refreshAuthAccountSelected(selAcc.uuid) - updateSelectedAccount(selAcc) - validateSelectedAccount() - } - if(isLastAccount) { - loginOptionsCancelEnabled(false) - loginOptionsViewOnLoginSuccess = VIEWS.settings - loginOptionsViewOnLoginCancel = VIEWS.loginOptions - switchView(getCurrentView(), VIEWS.loginOptions) - } - }) - $(parent).fadeOut(250, () => { - parent.remove() - }) + switch (targetAcc.type){ + case 'microsoft': + msAccDomElementCache = parent + switchView(getCurrentView(), VIEWS.waiting, 500, 500, () => { + ipcRenderer.send(MSFT_OPCODE.OPEN_LOGOUT, uuid, isLastAccount) + }) + break + case 'mojang': + AuthManager.removeMojangAccount(uuid).then(() => { + if(!isLastAccount && uuid === prevSelAcc.uuid){ + const selAcc = ConfigManager.getSelectedAccount() + refreshAuthAccountSelected(selAcc.uuid) + updateSelectedAccount(selAcc) + validateSelectedAccount() + } + if(isLastAccount) { + loginOptionsCancelEnabled(false) + loginOptionsViewOnLoginSuccess = VIEWS.settings + loginOptionsViewOnLoginCancel = VIEWS.loginOptions + switchView(getCurrentView(), VIEWS.loginOptions) + } + }) + $(parent).fadeOut(250, () => { + parent.remove() + }) + break + case 'offline': + AuthManager.removeOfflineAccount(uuid) + break + case 'skirda': + AuthManager.removeSkirdaAccount(uuid) + break } + if(isLastAccount) { + loginOptionsCancelEnabled(false) + loginOptionsViewOnLoginSuccess = VIEWS.settings + loginOptionsViewOnLoginCancel = VIEWS.loginOptions + switchView(getCurrentView(), VIEWS.loginOptions) + } + // if(targetAcc.type === 'microsoft') { + // + // } else { + // + // } } // Bind reply for Microsoft Logout. @@ -620,6 +646,8 @@ function refreshAuthAccountSelected(uuid){ const settingsCurrentMicrosoftAccounts = document.getElementById('settingsCurrentMicrosoftAccounts') const settingsCurrentMojangAccounts = document.getElementById('settingsCurrentMojangAccounts') +const settingsCurrentSkirdaDiscord = document.getElementById('settingsCurrentSkirdaDiscord') +const settingsCurrentOfflineDiscord = document.getElementById('settingsCurrentOfflineDiscord') /** * Add auth account elements for each one stored in the authentication database. @@ -634,9 +662,12 @@ function populateAuthAccounts(){ let microsoftAuthAccountStr = '' let mojangAuthAccountStr = '' + let skirdaAuthAccountStr = '' + let offlineAuthAccountStr = '' authKeys.forEach((val) => { const acc = authAccounts[val] + console.log(acc) const accHtml = `
@@ -662,16 +693,33 @@ function populateAuthAccounts(){
` - if(acc.type === 'microsoft') { - microsoftAuthAccountStr += accHtml - } else { - mojangAuthAccountStr += accHtml + + switch (acc.type){ + case 'microsoft': + microsoftAuthAccountStr += accHtml + break + case 'mojang': + mojangAuthAccountStr += accHtml + break + case 'offline': + offlineAuthAccountStr += accHtml + break + case 'skirda': + skirdaAuthAccountStr += accHtml + break } + // if(acc.type === 'microsoft') { + // microsoftAuthAccountStr += accHtml + // } else { + // mojangAuthAccountStr += accHtml + // } }) settingsCurrentMicrosoftAccounts.innerHTML = microsoftAuthAccountStr settingsCurrentMojangAccounts.innerHTML = mojangAuthAccountStr + settingsCurrentSkirdaAccounts.innerHTML = skirdaAuthAccountStr + settingsCurrentOfflineAccounts.innerHTML = offlineAuthAccountStr } /** diff --git a/app/assets/js/scripts/uibinder.js b/app/assets/js/scripts/uibinder.js index 9f37281..9a4a27c 100644 --- a/app/assets/js/scripts/uibinder.js +++ b/app/assets/js/scripts/uibinder.js @@ -18,6 +18,7 @@ const VIEWS = { landing: '#landingContainer', loginOptions: '#loginOptionsContainer', login: '#loginContainer', + loginSkirdaDiscord: '#loginSkirdaDiscordContainer', loginOffline: '#loginOfflineContainer', settings: '#settingsContainer', welcome: '#welcomeContainer', diff --git a/app/assets/js/skirda-auth/auth.js b/app/assets/js/skirda-auth/auth.js new file mode 100644 index 0000000..e69de29 diff --git a/app/loginOptions.ejs b/app/loginOptions.ejs index 6f258af..ced9f4a 100644 --- a/app/loginOptions.ejs +++ b/app/loginOptions.ejs @@ -35,6 +35,17 @@ Offline +
+ +
+ +
+
+
+ + + + + + Offline Accounts +
+
+ +
+
+ +
+ +
+
+ +
+
+
+ + + + + + + Skirda +
+
+ +
+
+ +
+ +
+