Modernized enable_twofactor example script

This commit is contained in:
Alex Corn 2020-04-04 21:48:38 -04:00
parent f5da011cde
commit b92c5d852d
No known key found for this signature in database
GPG Key ID: E51989A3E7A27FDF

View File

@ -1,45 +1,49 @@
var SteamCommunity = require('../index.js');
var ReadLine = require('readline');
var fs = require('fs');
// If you aren't running this script inside of the repository, replace the following line with:
// const SteamCommunity = require('steamcommunity');
const SteamCommunity = require('../index.js');
const ReadLine = require('readline');
const FS = require('fs');
var community = new SteamCommunity();
var rl = ReadLine.createInterface({
"input": process.stdin,
"output": process.stdout
const EResult = SteamCommunity.EResult;
let community = new SteamCommunity();
let rl = ReadLine.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Username: ", function(accountName) {
rl.question("Password: ", function(password) {
rl.question('Username: ', (accountName) => {
rl.question('Password: ', (password) => {
doLogin(accountName, password);
});
});
function doLogin(accountName, password, authCode, captcha) {
community.login({
"accountName": accountName,
"password": password,
"authCode": authCode,
"captcha": captcha
}, function(err, sessionID, cookies, steamguard) {
if(err) {
if(err.message == 'SteamGuardMobile') {
console.log("This account already has two-factor authentication enabled.");
accountName: accountName,
password: password,
authCode: authCode,
captcha: captcha
}, (err, sessionID, cookies, steamguard) => {
if (err) {
if (err.message == 'SteamGuardMobile') {
console.log('This account already has two-factor authentication enabled.');
process.exit();
return;
}
if(err.message == 'SteamGuard') {
console.log("An email has been sent to your address at " + err.emaildomain);
rl.question("Steam Guard Code: ", function (code) {
if (err.message == 'SteamGuard') {
console.log(`An email has been sent to your address at ${err.emaildomain}`);
rl.question('Steam Guard Code: ', (code) => {
doLogin(accountName, password, code);
});
return;
}
if(err.message == 'CAPTCHA') {
if (err.message == 'CAPTCHA') {
console.log(err.captchaurl);
rl.question("CAPTCHA: ", function(captchaInput) {
rl.question('CAPTCHA: ', (captchaInput) => {
doLogin(accountName, password, authCode, captchaInput);
});
@ -51,17 +55,17 @@ function doLogin(accountName, password, authCode, captcha) {
return;
}
console.log("Logged on!");
community.enableTwoFactor(function(err, response) {
if(err) {
if(err.eresult == 2) {
console.log("Error: Failed to enable two-factor authentication. Do you have a phone number attached to your account?");
console.log('Logged on!');
community.enableTwoFactor((err, response) => {
if (err) {
if (err.eresult == EResult.Fail) {
console.log('Error: Failed to enable two-factor authentication. Do you have a phone number attached to your account?');
process.exit();
return;
}
if(err.eresult == 84) {
console.log("Error: RateLimitExceeded. Try again later.");
if (err.eresult == EResult.RateLimitExceeded) {
console.log('Error: RateLimitExceeded. Try again later.');
process.exit();
return;
}
@ -71,15 +75,16 @@ function doLogin(accountName, password, authCode, captcha) {
return;
}
if(response.status != 1) {
console.log("Error: Status " + response.status);
if (response.status != EResult.OK) {
console.log(`Error: Status ${response.status}`);
process.exit();
return;
}
console.log("Writing secrets to twofactor_" + community.steamID.getSteamID64() + ".json");
console.log("Revocation code: " + response.revocation_code);
fs.writeFileSync("twofactor_" + community.steamID.getSteamID64() + ".json", JSON.stringify(response, null, "\t"));
let filename = `twofactor_${community.steamID.getSteamID64()}.json`;
console.log(`Writing secrets to ${filename}`);
console.log(`Revocation code: ${response.revocation_code}`);
FS.writeFileSync(filename, JSON.stringify(response, null, '\t'));
promptActivationCode(response);
});
@ -87,10 +92,10 @@ function doLogin(accountName, password, authCode, captcha) {
}
function promptActivationCode(response) {
rl.question("SMS Code: ", function(smsCode) {
community.finalizeTwoFactor(response.shared_secret, smsCode, function(err) {
if(err) {
if(err.message == "Invalid activation code") {
rl.question('SMS Code: ', (smsCode) => {
community.finalizeTwoFactor(response.shared_secret, smsCode, (err) => {
if (err) {
if (err.message == 'Invalid activation code') {
console.log(err);
promptActivationCode(response);
return;
@ -98,7 +103,7 @@ function promptActivationCode(response) {
console.log(err);
} else {
console.log("Two-factor authentication enabled!");
console.log('Two-factor authentication enabled!');
}
process.exit();