node-steamcommunity/components/twofactor.js

155 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-02-14 13:29:21 +08:00
const SteamTotp = require('steam-totp');
2015-11-27 14:45:58 +08:00
2019-02-14 13:29:21 +08:00
const SteamCommunity = require('../index.js');
2021-07-22 15:59:55 +08:00
const Helpers = require('./helpers.js');
2019-02-14 13:29:21 +08:00
const ETwoFactorTokenType = {
2021-07-22 15:59:55 +08:00
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) {
2021-07-22 15:59:55 +08:00
this.getWebApiOauthToken((err, token) => {
if (err) {
callback(err);
return;
}
2021-07-22 15:59:55 +08:00
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'
},
2021-07-22 15:59:55 +08:00
json: true
}, (err, response, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 15:59:55 +08:00
if (!body.response) {
callback(new Error('Malformed response'));
return;
}
2021-07-22 15:59:55 +08:00
let err2 = Helpers.eresultError(body.response.status);
if (err2) {
return callback(err2);
}
callback(null, body.response);
2021-07-22 15:59:55 +08:00
}, 'steamcommunity');
});
};
SteamCommunity.prototype.finalizeTwoFactor = function(secret, activationCode, callback) {
2021-07-22 15:59:55 +08:00
let attemptsLeft = 30;
let diff = 0;
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
},
2021-07-22 15:59:55 +08:00
json: true
}, (err, response, body) => {
if (err) {
callback(err);
return;
}
2021-07-22 15:59:55 +08:00
if (!body.response) {
callback(new Error('Malformed response'));
return;
}
body = body.response;
2021-07-22 15:59:55 +08:00
if (body.server_time) {
diff = body.server_time - Math.floor(Date.now() / 1000);
}
2021-07-22 15:59:55 +08:00
if (body.status == SteamCommunity.EResult.TwoFactorActivationCodeMismatch) {
callback(new Error('Invalid activation code'));
} else if (body.want_more) {
attemptsLeft--;
diff += 30;
finalize(token);
2021-07-22 15:59:55 +08:00
} else if (!body.success) {
callback(Helpers.eresultError(body.status));
} else {
callback(null);
}
2021-07-22 15:59:55 +08:00
}, 'steamcommunity');
}
2021-07-22 15:59:55 +08:00
this.getWebApiOauthToken((err, token) => {
if (err) {
callback(err);
return;
}
SteamTotp.getTimeOffset((err, offset, latency) => {
if (err) {
callback(err);
return;
}
diff = offset;
finalize(token);
});
});
};
2015-11-27 14:45:58 +08:00
SteamCommunity.prototype.disableTwoFactor = function(revocationCode, callback) {
2021-07-22 15:59:55 +08:00
this.getWebApiOauthToken((err, token) => {
if (err) {
2015-11-27 14:45:58 +08:00
callback(err);
return;
}
2021-07-22 15:59:55 +08:00
this.httpRequestPost({
uri: 'https://api.steampowered.com/ITwoFactorService/RemoveAuthenticator/v1/',
form: {
steamid: this.steamID.getSteamID64(),
access_token: token,
revocation_code: revocationCode,
steamguard_scheme: 1
2015-11-27 14:45:58 +08:00
},
2021-07-22 15:59:55 +08:00
json: true
}, (err, response, body) => {
if (err) {
callback(err);
2015-11-27 14:45:58 +08:00
return;
}
2021-07-22 15:59:55 +08:00
if (!body.response) {
callback(new Error('Malformed response'));
2015-11-27 14:45:58 +08:00
return;
}
2021-07-22 15:59:55 +08:00
if (!body.response.success) {
callback(new Error('Request failed'));
2015-11-27 14:45:58 +08:00
return;
}
// success = true means it worked
callback(null);
2021-07-22 15:59:55 +08:00
}, 'steamcommunity');
2015-11-27 14:45:58 +08:00
});
};