diff --git a/app/assets/js/authmanager.js b/app/assets/js/authmanager.js index 9515fd2..1cf85d2 100644 --- a/app/assets/js/authmanager.js +++ b/app/assets/js/authmanager.js @@ -69,9 +69,10 @@ exports.addMojangAccount = async function(username, password){ * @param {string} accessToken * @param {string} username * @param {string} displayName + * @param {string} skirdaToken * */ -exports.addSkirdaAccount = async function(uuid, accessToken, username, displayName){ - ConfigManager.addSkirdaAccount(uuid, accessToken, username, displayName) +exports.addSkirdaAccount = async function(uuid, accessToken, username, displayName, skirdaToken){ + ConfigManager.addSkirdaAccount(uuid, accessToken, username, displayName, skirdaToken) ConfigManager.save() } diff --git a/app/assets/js/configmanager.js b/app/assets/js/configmanager.js index 0cbfce9..24f317b 100644 --- a/app/assets/js/configmanager.js +++ b/app/assets/js/configmanager.js @@ -364,14 +364,15 @@ exports.addOfflineAccount = function(usernameOffline){ return config.authenticationDatabase[fake_uuid] } -exports.addSkirdaAccount = function(uuid, accessToken, username, displayName){ +exports.addSkirdaAccount = function(uuid, accessToken, username, displayName, skirdaToken){ config.selectedAccount = uuid config.authenticationDatabase[uuid] = { type: 'skirda', accessToken, username: username.trim(), uuid: uuid.trim(), - displayName: displayName.trim() + displayName: displayName.trim(), + skirdaJWT: skirdaToken } return config.authenticationDatabase[uuid] } diff --git a/app/assets/js/scripts/loginSkirdaDiscord.js b/app/assets/js/scripts/loginSkirdaDiscord.js index 3ec956c..46b5d2b 100644 --- a/app/assets/js/scripts/loginSkirdaDiscord.js +++ b/app/assets/js/scripts/loginSkirdaDiscord.js @@ -47,6 +47,34 @@ class SkirdaDiscordAuth{ } } + /** + * @typedef {Object} SkYggAuthResponse + * @property {string} ClientToken + * @property {string} AccessToken + * @property {YggProfile} Profile + * + * @typedef {Object} YggProfile + * @property {string} name + * @property {string} UUID + * + * @param jwtToken {string} JWT Token for auth + * @return {YggProfile | string} Result of auth + */ + async YggdrasilAuth(jwtToken){ + const resp = await fetch(`${auth_api_url}/yggdrasil/skirda/authenticate`, { + headers:{ + 'Content-Type': 'application/json', + 'Authorization': jwtToken + } + }) + + if (resp.statusCode !== 200){ + return await resp.text() + } + + return await resp.json() + } + /** * @typedef {Object} SkirdaUserResp @@ -76,26 +104,28 @@ class SkirdaDiscordAuth{ -const auth = new SkirdaDiscordAuth() +const skAuth = new SkirdaDiscordAuth() const loginSkirdaDiscordButton = document.getElementById('loginSkirdaDiscordInitAuth') loginSkirdaDiscordButton.addEventListener('click', async () =>{ - const res = await auth.Init() + const res = await skAuth.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) + const skirdaAuth = await skAuth.CyclePolling(redir.requestId) + //TODO validate resp + 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 - } + const yggAuth = await skAuth.YggdrasilAuth(skirdaAuth.token) + //TODO validate resp + const yggAuthRes = JSON.parse(yggAuth) + console.log(yggAuthRes) + + + AuthManager.addSkirdaAccount(yggAuthRes.profile.id, yggAuthRes.accessToken, skirdaAuth.skirdaUserId, yggAuthRes.profile.name, skirdaAuth.token) // AuthManager.addSkirdaAccount(account.uuid, account.token, account.username, account.displayName) AuthManager.addSkirdaAccount(account.uuid, account.token, account.username, account.displayName) @@ -104,4 +134,3 @@ loginSkirdaDiscordButton.addEventListener('click', async () =>{ switchView(VIEWS.loginSkirdaDiscord, VIEWS.landing, 500, 500) }, 1000) }) -