瀏覽代碼

Switches to use internal mojang accessors and removed 'mojang' as a dependency. First attempt at authentication storage, system will be refinded as UI is implemented further. All auth interactions should be made throught authmanager.js

Daniel Scalzi 8 年之前
父節點
當前提交
5afd9815a5
共有 6 個文件被更改,包括 102 次插入17 次删除
  1. 12 8
      app/assets/js/actionbinder.js
  2. 25 0
      app/assets/js/authmanager.js
  3. 63 2
      app/assets/js/configmanager.js
  4. 2 2
      app/assets/js/processbuilder.js
  5. 0 4
      package-lock.json
  6. 0 1
      package.json

+ 12 - 8
app/assets/js/actionbinder.js

@@ -1,10 +1,10 @@
-const mojang = require('mojang')
 const path = require('path')
 const {AssetGuard} = require(path.join(__dirname, 'assets', 'js', 'assetguard.js'))
 const ProcessBuilder = require(path.join(__dirname, 'assets', 'js', 'processbuilder.js'))
 const ConfigManager = require(path.join(__dirname, 'assets', 'js', 'configmanager.js'))
 const DiscordWrapper = require(path.join(__dirname, 'assets', 'js', 'discordwrapper.js'))
-const mojang2 = require(path.join(__dirname, 'assets', 'js', 'mojang.js'))
+const Mojang = require(path.join(__dirname, 'assets', 'js', 'mojang.js'))
+const AuthManager = require(path.join(__dirname, 'assets', 'js', 'authmanager.js'))
 
 let mojangStatusListener
 
@@ -28,7 +28,7 @@ document.addEventListener('readystatechange', function(){
             console.log('Refreshing Mojang Statuses..')
             try {
                 let status = 'grey'
-                const statuses = await mojang2.status()
+                const statuses = await Mojang.status()
                 greenCount = 0
                 for(let i=0; i<statuses.length; i++){
                     if(statuses[i].status === 'yellow' && status !== 'red'){
@@ -44,7 +44,7 @@ document.addEventListener('readystatechange', function(){
                     status = 'green'
                 }
 
-                document.getElementById('mojang_status_icon').style.color = mojang2.statusToHex(status)
+                document.getElementById('mojang_status_icon').style.color = Mojang.statusToHex(status)
 
             } catch (err) {
                 console.error('Unable to refresh Mojang service status..', err)
@@ -62,6 +62,13 @@ document.addEventListener('readystatechange', function(){
 let tracker;
 
 testdownloads = async function(){
+
+    if(ConfigManager.getSelectedAccount() == null){
+        console.error('login first.')
+        //in devtools AuthManager.addAccount(username, pass)
+        return
+    }
+
     const content = document.getElementById('launch_content')
     const details = document.getElementById('launch_details')
     const progress = document.getElementById('launch_progress')
@@ -115,10 +122,7 @@ testdownloads = async function(){
 
         det_text.innerHTML = 'Preparing to launch..'
         const forgeData = await tracker.loadForgeData(serv.id)
-        const authUser = await mojang.auth('EMAIL', 'PASS', ConfigManager.getClientToken(), {
-            name: 'Minecraft',
-            version: 1
-        })
+        const authUser = await AuthManager.validateSelected()
         let pb = new ProcessBuilder(ConfigManager.getGameDirectory(), serv, versionData, forgeData, authUser)
         det_text.innerHTML = 'Launching game..'
         let proc;

+ 25 - 0
app/assets/js/authmanager.js

@@ -0,0 +1,25 @@
+const ConfigManager = require('./configmanager.js')
+const Mojang = require('./mojang.js')
+
+exports.addAccount = function(username, password){
+    return new Promise(async function(resolve, reject){
+        const session = await Mojang.authenticate(username, password, ConfigManager.getClientToken)
+        const ret = ConfigManager.addAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name)
+        ConfigManager.save()
+        resolve(ret)
+    })
+}
+
+exports.validateSelected = function(){
+    return new Promise(async function(resolve, reject){
+        const current = ConfigManager.getSelectedAccount()
+        if(!await Mojang.validate(current.accessToken, ConfigManager.getClientToken)){
+            const session = Mojang.refresh(current.accessToken, ConfigManager.getClientToken)
+            const ret = ConfigManager.updateAuthAccount(current.uuid, session.accessToken)
+            ConfigManager.save()
+            resolve(ret)
+        } else {
+            resolve(current)
+        }
+    })
+}

+ 63 - 2
app/assets/js/configmanager.js

@@ -42,7 +42,7 @@ const DEFAULT_CONFIG = {
     clientToken: uuidV4(),
     selectedServer: null, // Resolved
     selectedAccount: null,
-    authenticationDatabase: []
+    authenticationDatabase: {}
 }
 
 let config = null;
@@ -118,7 +118,68 @@ exports.setSelectedServer = function(serverID){
     config.selectedServer = serverID
 }
 
-//TODO Write Authentication Database/Selected Account accessors here
+/**
+ * Get an array of each account currently authenticated by the launcher.
+ * 
+ * @returns {Array.<Object>} - an array of each stored authenticated account.
+ */
+exports.getAuthAccounts = function(){
+    return config.authenticationDatabase
+}
+
+/**
+ * Returns the authenticated account with the given uuid. Value may
+ * be null.
+ * 
+ * @param {String} uuid - the uuid of the authenticated account.
+ * @returns {Object} - the authenticated account with the given uuid.
+ */
+exports.getAuthAccount = function(uuid){
+    return config.authenticationDatabase[uuid]
+}
+
+/**
+ * Update the access token of an authenticated account.
+ * 
+ * @param {String} uuid - uuid of the authenticated account.
+ * @param {String} accessToken - the new Access Token.
+ * 
+ * @returns {Object} - the authenticated account object created by this action.
+ */
+exports.updateAuthAccount = function(uuid, accessToken){
+    config.authenticationDatabase[uuid].accessToken = accessToken
+    return config.authenticationDatabase[uuid]
+}
+
+/**
+ * Adds an authenticated account to the database to be stored.
+ * 
+ * @param {String} uuid - uuid of the authenticated account.
+ * @param {String} accessToken - accessToken of the authenticated account.
+ * @param {String} username - username (usually email) of the authenticated account.
+ * @param {String} displayName - in game name of the authenticated account.
+ * 
+ * @returns {Object} - the authenticated account object created by this action.
+ */
+exports.addAuthAccount = function(uuid, accessToken, username, displayName){
+    config.selectedAccount = uuid
+    config.authenticationDatabase[uuid] = {
+        accessToken,
+        username,
+        uuid,
+        displayName
+    }
+    return config.authenticationDatabase[uuid]
+}
+
+/**
+ * Get the currently selected authenticated account.
+ * 
+ * @returns {Object} - the selected authenticated account.
+ */
+exports.getSelectedAccount = function(){
+    return config.authenticationDatabase[config.selectedAccount]
+}
 
 // User Configurable Settings
 

+ 2 - 2
app/assets/js/processbuilder.js

@@ -131,7 +131,7 @@ class ProcessBuilder {
                 let val = null;
                 switch(identifier){
                     case 'auth_player_name':
-                        val = this.authUser.selectedProfile.name
+                        val = this.authUser.displayName
                         break
                     case 'version_name':
                         //val = versionData.id
@@ -147,7 +147,7 @@ class ProcessBuilder {
                         val = this.versionData.assets
                         break
                     case 'auth_uuid':
-                        val = this.authUser.selectedProfile.id
+                        val = this.authUser.uuid
                         break
                     case 'auth_access_token':
                         val = this.authUser.accessToken

+ 0 - 4
package-lock.json

@@ -1237,10 +1237,6 @@
         }
       }
     },
-    "mojang": {
-      "version": "https://registry.npmjs.org/mojang/-/mojang-0.4.1.tgz",
-      "integrity": "sha1-aWPEL8RlDrwASCSGvdqpB2Mhmi4="
-    },
     "ms": {
       "version": "2.0.0",
       "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",

+ 0 - 1
package.json

@@ -32,7 +32,6 @@
     "ejs-electron": "^2.0.1",
     "find-java-home": "^0.2.0",
     "jquery": "^3.2.1",
-    "mojang": "^0.4.1",
     "request-promise-native": "^1.0.5",
     "uuid": "^3.1.0"
   },