mirror of
https://github.com/DoctorMcKay/node-steamcommunity.git
synced 2025-03-14 23:10:08 +08:00
Updated twofactor.js methods to use new http interface
This commit is contained in:
parent
3a65768b3f
commit
e65d50b5f7
@ -1,3 +1,4 @@
|
|||||||
|
const StdLib = require('@doctormckay/stdlib');
|
||||||
const SteamTotp = require('steam-totp');
|
const SteamTotp = require('steam-totp');
|
||||||
|
|
||||||
const SteamCommunity = require('../index.js');
|
const SteamCommunity = require('../index.js');
|
||||||
@ -6,152 +7,158 @@ const Helpers = require('./helpers.js');
|
|||||||
const ETwoFactorTokenType = {
|
const ETwoFactorTokenType = {
|
||||||
None: 0, // No token-based two-factor authentication
|
None: 0, // No token-based two-factor authentication
|
||||||
ValveMobileApp: 1, // Tokens generated using Valve's special charset (5 digits, alphanumeric)
|
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.
|
ThirdParty: 2 // Tokens generated using literally everyone else's standard charset (6 digits, numeric). This is disabled on the backend.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {function} [callback]
|
||||||
|
* @return {Promise<object>}
|
||||||
|
*/
|
||||||
SteamCommunity.prototype.enableTwoFactor = function(callback) {
|
SteamCommunity.prototype.enableTwoFactor = function(callback) {
|
||||||
this._verifyMobileAccessToken();
|
return StdLib.Promises.callbackPromise(null, callback, false, async (resolve, reject) => {
|
||||||
|
this._verifyMobileAccessToken();
|
||||||
|
|
||||||
if (!this.mobileAccessToken) {
|
if (!this.mobileAccessToken) {
|
||||||
callback(new Error('No mobile access token available. Provide one by calling setMobileAppAccessToken()'));
|
return reject(new Error('No mobile access token available. Provide one by calling setMobileAppAccessToken()'));
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.httpRequestPost({
|
|
||||||
uri: "https://api.steampowered.com/ITwoFactorService/AddAuthenticator/v1/?access_token=" + this.mobileAccessToken,
|
|
||||||
// TODO: Send this as protobuf to more closely mimic official app behavior
|
|
||||||
form: {
|
|
||||||
steamid: this.steamID.getSteamID64(),
|
|
||||||
authenticator_time: Math.floor(Date.now() / 1000),
|
|
||||||
authenticator_type: ETwoFactorTokenType.ValveMobileApp,
|
|
||||||
device_identifier: SteamTotp.getDeviceID(this.steamID),
|
|
||||||
sms_phone_id: '1'
|
|
||||||
},
|
|
||||||
json: true
|
|
||||||
}, (err, response, body) => {
|
|
||||||
if (err) {
|
|
||||||
callback(err);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!body.response) {
|
let {jsonBody} = await this.httpRequest({
|
||||||
callback(new Error('Malformed response'));
|
method: 'POST',
|
||||||
return;
|
url: `https://api.steampowered.com/ITwoFactorService/AddAuthenticator/v1/?access_token=${this.mobileAccessToken}`,
|
||||||
}
|
// TODO: Send this as protobuf to more closely mimic official app behavior
|
||||||
|
|
||||||
if (body.response.status != 1) {
|
|
||||||
var error = new Error('Error ' + body.response.status);
|
|
||||||
error.eresult = body.response.status;
|
|
||||||
callback(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, body.response);
|
|
||||||
}, 'steamcommunity');
|
|
||||||
};
|
|
||||||
|
|
||||||
SteamCommunity.prototype.finalizeTwoFactor = function(secret, activationCode, callback) {
|
|
||||||
this._verifyMobileAccessToken();
|
|
||||||
|
|
||||||
if (!this.mobileAccessToken) {
|
|
||||||
callback(new Error('No mobile access token available. Provide one by calling setMobileAppAccessToken()'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let attemptsLeft = 30;
|
|
||||||
let diff = 0;
|
|
||||||
|
|
||||||
let finalize = () => {
|
|
||||||
let code = SteamTotp.generateAuthCode(secret, diff);
|
|
||||||
|
|
||||||
this.httpRequestPost({
|
|
||||||
uri: 'https://api.steampowered.com/ITwoFactorService/FinalizeAddAuthenticator/v1/?access_token=' + this.mobileAccessToken,
|
|
||||||
form: {
|
form: {
|
||||||
steamid: this.steamID.getSteamID64(),
|
steamid: this.steamID.getSteamID64(),
|
||||||
authenticator_code: code,
|
|
||||||
authenticator_time: Math.floor(Date.now() / 1000),
|
authenticator_time: Math.floor(Date.now() / 1000),
|
||||||
activation_code: activationCode
|
authenticator_type: ETwoFactorTokenType.ValveMobileApp,
|
||||||
|
device_identifier: SteamTotp.getDeviceID(this.steamID),
|
||||||
|
sms_phone_id: '1'
|
||||||
},
|
},
|
||||||
json: true
|
source: 'steamcommunity'
|
||||||
}, (err, response, body) => {
|
});
|
||||||
if (err) {
|
|
||||||
callback(err);
|
|
||||||
return;
|
if (!jsonBody.response) {
|
||||||
|
return reject(new Error('Malformed response'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonBody.response.status != 1) {
|
||||||
|
let error = new Error(`Error ${jsonBody.response.status}`);
|
||||||
|
error.eresult = jsonBody.response.status;
|
||||||
|
return reject(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(jsonBody.response);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} secret
|
||||||
|
* @param {string} activationCode
|
||||||
|
* @param {function} [callback]
|
||||||
|
* @return Promise<void>
|
||||||
|
*/
|
||||||
|
SteamCommunity.prototype.finalizeTwoFactor = function(secret, activationCode, callback) {
|
||||||
|
return StdLib.Promises.callbackPromise(null, callback, false, async (resolve, reject) => {
|
||||||
|
this._verifyMobileAccessToken();
|
||||||
|
|
||||||
|
if (!this.mobileAccessToken) {
|
||||||
|
return reject(new Error('No mobile access token available. Provide one by calling setMobileAppAccessToken()'));
|
||||||
|
}
|
||||||
|
|
||||||
|
let attemptsLeft = 30;
|
||||||
|
let diff = 0;
|
||||||
|
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
SteamTotp.getTimeOffset(function(err, offset, latency) {
|
||||||
|
if (err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
diff = offset;
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
let finalize = async () => {
|
||||||
|
let code = SteamTotp.generateAuthCode(secret, diff);
|
||||||
|
|
||||||
|
let {jsonBody} = this.httpRequest({
|
||||||
|
method: 'POST',
|
||||||
|
url: `https://api.steampowered.com/ITwoFactorService/FinalizeAddAuthenticator/v1/?access_token=${this.mobileAccessToken}`,
|
||||||
|
form: {
|
||||||
|
steamid: this.steamID.getSteamID64(),
|
||||||
|
authenticator_code: code,
|
||||||
|
authenticator_time: Math.floor(Date.now() / 1000),
|
||||||
|
activation_code: activationCode
|
||||||
|
},
|
||||||
|
source: 'steamcommunity'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!jsonBody.response) {
|
||||||
|
return reject(new Error('Malformed response'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!body.response) {
|
jsonBody = jsonBody.response;
|
||||||
callback(new Error('Malformed response'));
|
|
||||||
return;
|
if (jsonBody.server_time) {
|
||||||
|
diff = jsonBody.server_time - Math.floor(Date.now() / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
body = body.response;
|
if (jsonBody.status == SteamCommunity.EResult.TwoFactorActivationCodeMismatch) {
|
||||||
|
return reject(new Error('Invalid activation code'));
|
||||||
if (body.server_time) {
|
} else if (jsonBody.want_more) {
|
||||||
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) {
|
|
||||||
if (--attemptsLeft <= 0) {
|
if (--attemptsLeft <= 0) {
|
||||||
// We made more than 30 attempts, something must be wrong
|
// We made more than 30 attempts, something must be wrong
|
||||||
return callback(Helpers.eresultError(SteamCommunity.EResult.Fail));
|
return reject(Helpers.eresultError(SteamCommunity.EResult.Fail));
|
||||||
}
|
}
|
||||||
diff += 30;
|
diff += 30;
|
||||||
|
|
||||||
finalize();
|
finalize();
|
||||||
} else if(!body.success) {
|
} else if (!jsonBody.success) {
|
||||||
callback(new Error('Error ' + body.status));
|
return reject(new Error(`Error ${jsonBody.status}`));
|
||||||
} else {
|
} else {
|
||||||
callback(null);
|
resolve();
|
||||||
}
|
}
|
||||||
}, 'steamcommunity');
|
};
|
||||||
}
|
|
||||||
|
|
||||||
SteamTotp.getTimeOffset(function(err, offset, latency) {
|
|
||||||
if (err) {
|
|
||||||
callback(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
diff = offset;
|
|
||||||
finalize();
|
finalize();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} revocationCode
|
||||||
|
* @param {function} [callback]
|
||||||
|
* @return Promise<void>
|
||||||
|
*/
|
||||||
SteamCommunity.prototype.disableTwoFactor = function(revocationCode, callback) {
|
SteamCommunity.prototype.disableTwoFactor = function(revocationCode, callback) {
|
||||||
this._verifyMobileAccessToken();
|
return StdLib.Promises.callbackPromise(null, callback, false, async (resolve, reject) => {
|
||||||
|
this._verifyMobileAccessToken();
|
||||||
|
|
||||||
if (!this.mobileAccessToken) {
|
if (!this.mobileAccessToken) {
|
||||||
callback(new Error('No mobile access token available. Provide one by calling setMobileAppAccessToken()'));
|
callback(new Error('No mobile access token available. Provide one by calling setMobileAppAccessToken()'));
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.httpRequestPost({
|
|
||||||
uri: 'https://api.steampowered.com/ITwoFactorService/RemoveAuthenticator/v1/?access_token=' + this.mobileAccessToken,
|
|
||||||
form: {
|
|
||||||
steamid: this.steamID.getSteamID64(),
|
|
||||||
revocation_code: revocationCode,
|
|
||||||
steamguard_scheme: 1
|
|
||||||
},
|
|
||||||
json: true
|
|
||||||
}, function(err, response, body) {
|
|
||||||
if (err) {
|
|
||||||
callback(err);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!body.response) {
|
let {jsonBody} = await this.httpRequest({
|
||||||
callback(new Error('Malformed response'));
|
method: 'POST',
|
||||||
return;
|
url: `https://api.steampowered.com/ITwoFactorService/RemoveAuthenticator/v1/?access_token=${this.mobileAccessToken}`,
|
||||||
|
form: {
|
||||||
|
steamid: this.steamID.getSteamID64(),
|
||||||
|
revocation_code: revocationCode,
|
||||||
|
steamguard_scheme: 1
|
||||||
|
},
|
||||||
|
source: 'steamcommunity'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!jsonBody.response) {
|
||||||
|
return reject(new Error('Malformed response'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!body.response.success) {
|
if (!jsonBody.response.success) {
|
||||||
callback(new Error('Request failed'));
|
return reject(new Error('Request failed'));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// success = true means it worked
|
// success = true means it worked
|
||||||
callback(null);
|
resolve();
|
||||||
}, 'steamcommunity');
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user