From 0e09106643787d93c245a87121471894bb380f8a Mon Sep 17 00:00:00 2001 From: Pierce Date: Fri, 30 Nov 2018 19:33:08 -0500 Subject: [PATCH] added validate and refresh endpoints to logic --- README.md | 29 +++++++++++++++++++- components/authenticator.js | 54 ++++++++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 00e613f..818a0b2 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,40 @@ A script that launches Minecraft using NodeJS. #### Usage +##### Basic Login ```javascript const launcher = require('./pathtomodule'); launcher.authenticator("email", "password").then(auth => { + // Save the auth to a file so it can be used later on! launcher.core({ authorization: auth, - // All of the following is required + // All of the following is required + root: "directory", // C:/Users/user/AppData/Roaming/.mc + os: "windows", // windows, osx, linux + version: { + number: "1.13.2", // Minecraft version you want to launch + type: "MCC-Launcher" // Type. Can be anything + }, + memory: { + max: "500" + } + }); +}); +``` + +##### Using Validate and Refresh + +```javascript + let auth = require("pathToUserAuthJson.json"); + + const validateCheck = await launcher.authenticator.validate(auth.access_token); // required arguments. + if(!validateCheck) { + auth = await launcher.authenticator.refreshAuth(auth.access_token, auth.client_token, auth.selected_profile); // required arguments. + } + launcher.core({ + authorization: auth, + // All of the following is required root: "directory", // C:/Users/user/AppData/Roaming/.mc os: "windows", // windows, osx, linux version: { diff --git a/components/authenticator.js b/components/authenticator.js index e30253c..6716d8f 100644 --- a/components/authenticator.js +++ b/components/authenticator.js @@ -41,6 +41,7 @@ function getAuth(username, password) { client_token: uuid(), uuid: body.selectedProfile.id, name: body.selectedProfile.name, + selected_profile: body.selectedProfile, user_properties: JSON.stringify((body.user || {}).properties || {}) }; @@ -49,6 +50,53 @@ function getAuth(username, password) { }); } -module.exports = async function(username, password) { - return await getAuth(username, password); -}; \ No newline at end of file +function validate(access_token) { + return new Promise(resolve => { + const requestObject = { + url: api_url + "/validate", + json: { + "accessToken": access_token + } + }; + + request.post(requestObject, async function(error, response, body) { + if (error) resolve(error); + + if(!body) resolve(true); else resolve(false); + }); + }); +} + +function refreshAuth(accessToken, clientToken, selectedProfile) { + return new Promise(resolve => { + const requestObject = { + url: api_url + "/refresh", + json: { + "accessToken": accessToken, + "clientToken": clientToken, + "selectedProfile": selectedProfile, + "requestUser": true + } + }; + + request.post(requestObject, function(error, response, body) { + if (error) resolve(error); + console.log(body); + if(!body.selectedProfile) { + throw new Error("Validation error: " + response.statusMessage); + } + + const userProfile = { + access_token: body.accessToken, + client_token: uuid(), + uuid: body.selectedProfile.id, + name: body.selectedProfile.name, + user_properties: JSON.stringify((body.user || {}).properties || {}) + }; + + resolve(userProfile); + }); + }); +} + +module.exports = {getAuth, validate, refreshAuth}; \ No newline at end of file