diff --git a/components/market.js b/components/market.js index 15b1632..25049a4 100644 --- a/components/market.js +++ b/components/market.js @@ -14,7 +14,7 @@ SteamCommunity.prototype.getMarketApps = function(callback) { return; } - var $ = Cheerio.load(body); + let $ = Cheerio.load(body); if ($('.market_search_game_button_group')) { let apps = {}; $('.market_search_game_button_group a.game_button').each(function (i, element) { diff --git a/components/profile.js b/components/profile.js index 67b15b3..793af24 100644 --- a/components/profile.js +++ b/components/profile.js @@ -61,7 +61,7 @@ SteamCommunity.prototype.editProfile = function(settings, callback) { } let values = { - sessionID: self.getSessionID(), + sessionID: this.getSessionID(), type: 'profileSave', weblink_1_title: '', weblink_1_url: '', diff --git a/components/twofactor.js b/components/twofactor.js index c02828d..490de26 100644 --- a/components/twofactor.js +++ b/components/twofactor.js @@ -1,68 +1,109 @@ const SteamTotp = require('steam-totp'); const SteamCommunity = require('../index.js'); +const Helpers = require('./helpers.js'); const ETwoFactorTokenType = { - "None": 0, // No token-based two-factor authentication - "ValveMobileApp": 1, // Tokens generated using Valve's special charset (5 digits, alphanumeric) - "ThirdParty": 2 // Tokens generated using literally everyone else's standard charset (6 digits, numeric). This is disabled. + None: 0, // No token-based two-factor authentication + ValveMobileApp: 1, // Tokens generated using Valve's special charset (5 digits, alphanumeric) + ThirdParty: 2 // Tokens generated using literally everyone else's standard charset (6 digits, numeric). This is disabled. }; SteamCommunity.prototype.enableTwoFactor = function(callback) { - var self = this; - - this.getWebApiOauthToken(function(err, token) { - if(err) { + this.getWebApiOauthToken((err, token) => { + if (err) { callback(err); return; } - self.httpRequestPost({ - "uri": "https://api.steampowered.com/ITwoFactorService/AddAuthenticator/v1/", - "form": { - "steamid": self.steamID.getSteamID64(), - "access_token": token, - "authenticator_time": Math.floor(Date.now() / 1000), - "authenticator_type": ETwoFactorTokenType.ValveMobileApp, - "device_identifier": SteamTotp.getDeviceID(self.steamID), - "sms_phone_id": "1" + this.httpRequestPost({ + uri: 'https://api.steampowered.com/ITwoFactorService/AddAuthenticator/v1/', + form: { + steamid: this.steamID.getSteamID64(), + access_token: token, + authenticator_time: Math.floor(Date.now() / 1000), + authenticator_type: ETwoFactorTokenType.ValveMobileApp, + device_identifier: SteamTotp.getDeviceID(this.steamID), + sms_phone_id: '1' }, - "json": true - }, function(err, response, body) { + json: true + }, (err, response, body) => { if (err) { callback(err); return; } - if(!body.response) { - callback(new Error("Malformed response")); + if (!body.response) { + callback(new Error('Malformed response')); return; } - if(body.response.status != 1) { - var error = new Error("Error " + body.response.status); - error.eresult = body.response.status; - callback(error); - return; + let err2 = Helpers.eresultError(body.response.status); + if (err2) { + return callback(err2); } callback(null, body.response); - }, "steamcommunity"); + }, 'steamcommunity'); }); }; SteamCommunity.prototype.finalizeTwoFactor = function(secret, activationCode, callback) { - var attemptsLeft = 30; - var diff = 0; + let attemptsLeft = 30; + let diff = 0; - var self = this; - this.getWebApiOauthToken(function(err, token) { - if(err) { + const finalize = (token) => { + let code = SteamTotp.generateAuthCode(secret, diff); + + this.httpRequestPost({ + uri: 'https://api.steampowered.com/ITwoFactorService/FinalizeAddAuthenticator/v1/', + form: { + steamid: this.steamID.getSteamID64(), + access_token: token, + authenticator_code: code, + authenticator_time: Math.floor(Date.now() / 1000), + activation_code: activationCode + }, + json: true + }, (err, response, body) => { + if (err) { + callback(err); + return; + } + + if (!body.response) { + callback(new Error('Malformed response')); + return; + } + + body = body.response; + + if (body.server_time) { + diff = body.server_time - Math.floor(Date.now() / 1000); + } + + if (body.status == SteamCommunity.EResult.TwoFactorActivationCodeMismatch) { + callback(new Error('Invalid activation code')); + } else if (body.want_more) { + attemptsLeft--; + diff += 30; + + finalize(token); + } else if (!body.success) { + callback(Helpers.eresultError(body.status)); + } else { + callback(null); + } + }, 'steamcommunity'); + } + + this.getWebApiOauthToken((err, token) => { + if (err) { callback(err); return; } - SteamTotp.getTimeOffset(function(err, offset, latency) { + SteamTotp.getTimeOffset((err, offset, latency) => { if (err) { callback(err); return; @@ -72,89 +113,42 @@ SteamCommunity.prototype.finalizeTwoFactor = function(secret, activationCode, ca finalize(token); }); }); - - function finalize(token) { - var code = SteamTotp.generateAuthCode(secret, diff); - - self.httpRequestPost({ - "uri": "https://api.steampowered.com/ITwoFactorService/FinalizeAddAuthenticator/v1/", - "form": { - "steamid": self.steamID.getSteamID64(), - "access_token": token, - "authenticator_code": code, - "authenticator_time": Math.floor(Date.now() / 1000), - "activation_code": activationCode - }, - "json": true - }, function(err, response, body) { - if (err) { - callback(err); - return; - } - - if(!body.response) { - callback(new Error("Malformed response")); - return; - } - - body = body.response; - - if(body.server_time) { - diff = body.server_time - Math.floor(Date.now() / 1000); - } - - if(body.status == 89) { - callback(new Error("Invalid activation code")); - } else if(body.want_more) { - attemptsLeft--; - diff += 30; - - finalize(token); - } else if(!body.success) { - callback(new Error("Error " + body.status)); - } else { - callback(null); - } - }, "steamcommunity"); - } }; SteamCommunity.prototype.disableTwoFactor = function(revocationCode, callback) { - var self = this; - - this.getWebApiOauthToken(function(err, token) { - if(err) { + this.getWebApiOauthToken((err, token) => { + if (err) { callback(err); return; } - self.httpRequestPost({ - "uri": "https://api.steampowered.com/ITwoFactorService/RemoveAuthenticator/v1/", - "form": { - "steamid": self.steamID.getSteamID64(), - "access_token": token, - "revocation_code": revocationCode, - "steamguard_scheme": 1 + this.httpRequestPost({ + uri: 'https://api.steampowered.com/ITwoFactorService/RemoveAuthenticator/v1/', + form: { + steamid: this.steamID.getSteamID64(), + access_token: token, + revocation_code: revocationCode, + steamguard_scheme: 1 }, - "json": true - }, function(err, response, body) { + json: true + }, (err, response, body) => { if (err) { callback(err); return; } - if(!body.response) { - callback(new Error("Malformed response")); + if (!body.response) { + callback(new Error('Malformed response')); return; } - if(!body.response.success) { - callback(new Error("Request failed")); + if (!body.response.success) { + callback(new Error('Request failed')); return; } // success = true means it worked callback(null); - }, "steamcommunity"); + }, 'steamcommunity'); }); };