// const http = require('http') // const fs = require('fs') // const {addSkirdaAccount} = require('configmanager') // const auth_api_url = 'http://192.168.88.10:8083' const auth_api_url = 'http://skirda-auth.brzezinski.ru' 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} 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 * @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 skAuth = new SkirdaDiscordAuth() const loginSkirdaDiscordButton = document.getElementById('loginSkirdaDiscordInitAuth') async function InitSkirdaDiscordLogin (){ const res = await skAuth.Init() const redir = JSON.parse(res) //FIXME console.log(redir) const resOpenUrl = await shell.openExternal(redir.redirectUrl) const skirdaAuth = await skAuth.CyclePolling(redir.requestId) //TODO validate resp console.log(skirdaAuth) // skirdaAuth.skirdaUserId = '2a5fd868-1ac5-4ccf-a22f-183822de2d61' const yggAuth = await skAuth.YggdrasilAuth(skirdaAuth.token) //TODO validate resp const yggAuthRes = JSON.parse(yggAuth) console.log(yggAuthRes) AuthManager.addSkirdaAccount(yggAuthRes.profile.id, skirdaAuth.token, skirdaAuth.skirdaUserId, yggAuthRes.profile.name, skirdaAuth.token) setTimeout(() => { switchView(VIEWS.loginSkirdaDiscord, VIEWS.landing, 500, 500) }, 1000) }