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.
2015-11-28 06:57:39 +08:00
} ;
SteamCommunity . prototype . enableTwoFactor = function ( callback ) {
2021-07-22 16:06:59 +08:00
if ( ! this . oAuthToken ) {
return callback ( new Error ( 'enableTwoFactor can only be used when logged on via steamcommunity\'s `login` method without the `disableMobile` option.' ) ) ;
}
this . httpRequestPost ( {
uri : 'https://api.steampowered.com/ITwoFactorService/AddAuthenticator/v1/' ,
form : {
steamid : this . steamID . getSteamID64 ( ) ,
access _token : this . oAuthToken ,
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 ) => {
2021-07-22 15:59:55 +08:00
if ( err ) {
2015-11-28 06:57:39 +08:00
callback ( err ) ;
return ;
}
2021-07-22 16:06:59 +08:00
if ( ! body . response ) {
callback ( new Error ( 'Malformed response' ) ) ;
return ;
}
2015-11-28 06:57:39 +08:00
2021-07-22 16:06:59 +08:00
let err2 = Helpers . eresultError ( body . response . status ) ;
if ( err2 ) {
return callback ( err2 ) ;
}
2015-11-28 11:09:36 +08:00
2021-07-22 16:06:59 +08:00
callback ( null , body . response ) ;
} , 'steamcommunity' ) ;
2015-11-28 06:57:39 +08:00
} ;
SteamCommunity . prototype . finalizeTwoFactor = function ( secret , activationCode , callback ) {
2021-07-22 16:06:59 +08:00
if ( ! this . oAuthToken ) {
return callback ( new Error ( 'finalizeTwoFactor can only be used when logged on via steamcommunity\'s `login` method without the `disableMobile` option.' ) ) ;
}
2021-07-22 15:59:55 +08:00
let attemptsLeft = 30 ;
let diff = 0 ;
2021-07-22 16:06:59 +08:00
const finalize = ( ) => {
2021-07-22 15:59:55 +08:00
let code = SteamTotp . generateAuthCode ( secret , diff ) ;
this . httpRequestPost ( {
uri : 'https://api.steampowered.com/ITwoFactorService/FinalizeAddAuthenticator/v1/' ,
form : {
steamid : this . steamID . getSteamID64 ( ) ,
2021-07-22 16:06:59 +08:00
access _token : this . oAuthToken ,
2021-07-22 15:59:55 +08:00
authenticator _code : code ,
authenticator _time : Math . floor ( Date . now ( ) / 1000 ) ,
activation _code : activationCode
2015-11-28 06:57:39 +08:00
} ,
2021-07-22 15:59:55 +08:00
json : true
} , ( err , response , body ) => {
2016-03-05 12:59:49 +08:00
if ( err ) {
callback ( err ) ;
2015-11-28 06:57:39 +08:00
return ;
}
2021-07-22 15:59:55 +08:00
if ( ! body . response ) {
callback ( new Error ( 'Malformed response' ) ) ;
2015-11-28 06:57:39 +08:00
return ;
}
body = body . response ;
2021-07-22 15:59:55 +08:00
if ( body . server _time ) {
2015-11-28 06:57:39 +08:00
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 ) {
2015-11-28 06:57:39 +08:00
attemptsLeft -- ;
diff += 30 ;
2021-07-22 16:06:59 +08:00
finalize ( ) ;
2021-07-22 15:59:55 +08:00
} else if ( ! body . success ) {
callback ( Helpers . eresultError ( body . status ) ) ;
2015-11-28 06:57:39 +08:00
} else {
callback ( null ) ;
}
2021-07-22 15:59:55 +08:00
} , 'steamcommunity' ) ;
2015-11-28 06:57:39 +08:00
}
2021-07-22 15:59:55 +08:00
2021-07-22 16:06:59 +08:00
SteamTotp . getTimeOffset ( ( err , offset , latency ) => {
2021-07-22 15:59:55 +08:00
if ( err ) {
callback ( err ) ;
return ;
}
2021-07-22 16:06:59 +08:00
diff = offset ;
finalize ( ) ;
2021-07-22 15:59:55 +08:00
} ) ;
2015-11-28 06:57:39 +08:00
} ;
2015-11-27 14:45:58 +08:00
SteamCommunity . prototype . disableTwoFactor = function ( revocationCode , callback ) {
2021-07-22 16:06:59 +08:00
if ( ! this . oAuthToken ) {
return callback ( new Error ( 'disableTwoFactor can only be used when logged on via steamcommunity\'s `login` method without the `disableMobile` option.' ) ) ;
}
this . httpRequestPost ( {
uri : 'https://api.steampowered.com/ITwoFactorService/RemoveAuthenticator/v1/' ,
form : {
steamid : this . steamID . getSteamID64 ( ) ,
access _token : this . oAuthToken ,
revocation _code : revocationCode ,
steamguard _scheme : 1
} ,
json : true
} , ( err , response , body ) => {
2021-07-22 15:59:55 +08:00
if ( err ) {
2015-11-27 14:45:58 +08:00
callback ( err ) ;
return ;
}
2021-07-22 16:06:59 +08:00
if ( ! body . response ) {
callback ( new Error ( 'Malformed response' ) ) ;
return ;
}
2015-11-27 14:45:58 +08:00
2021-07-22 16:06:59 +08:00
if ( ! body . response . success ) {
callback ( new Error ( 'Request failed' ) ) ;
return ;
}
2015-11-27 14:45:58 +08:00
2021-07-22 16:06:59 +08:00
// success = true means it worked
callback ( null ) ;
} , 'steamcommunity' ) ;
2015-11-27 14:45:58 +08:00
} ;