Skip to content

Commit

Permalink
Add support for WinAuth, #144
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Mar 12, 2016
1 parent d084627 commit 4710d9c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
98 changes: 74 additions & 24 deletions ArchiSteamFarm/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,9 @@ internal Bot(string botName) {
SentryFile = botPath + ".bin";

// Support and convert SDA files
if (BotDatabase.SteamGuardAccount == null && File.Exists(botPath + ".maFile")) {
Logging.LogGenericInfo("Converting SDA .maFile into ASF format...", botName);
try {
BotDatabase.SteamGuardAccount = JsonConvert.DeserializeObject<SteamGuardAccount>(File.ReadAllText(botPath + ".maFile"));
File.Delete(botPath + ".maFile");
Logging.LogGenericInfo("Success!", botName);
} catch (Exception e) {
Logging.LogGenericException(e, botName);
}
string maFilePath = botPath + ".maFile";
if (BotDatabase.SteamGuardAccount == null && File.Exists(maFilePath)) {
ImportAuthenticator(maFilePath);
}

// Initialize
Expand Down Expand Up @@ -432,6 +426,62 @@ private void Shutdown() {
Program.OnBotShutdown();
}

private void ImportAuthenticator(string maFilePath) {
if (BotDatabase.SteamGuardAccount != null || !File.Exists(maFilePath)) {
return;
}

Logging.LogGenericInfo("Converting SDA .maFile into ASF format...", BotName);
try {
BotDatabase.SteamGuardAccount = JsonConvert.DeserializeObject<SteamGuardAccount>(File.ReadAllText(maFilePath));
File.Delete(maFilePath);
Logging.LogGenericInfo("Success!", BotName);
} catch (Exception e) {
Logging.LogGenericException(e, BotName);
return;
}

// If this is SDA file, then we should already have everything ready
if (BotDatabase.SteamGuardAccount.Session != null) {
Logging.LogGenericInfo("Successfully finished importing mobile authenticator!", BotName);
return;
}

// But here we're dealing with WinAuth authenticator
Logging.LogGenericInfo("ASF requires a few more steps to complete authenticator import...", BotName);

InitializeLoginAndPassword();

UserLogin userLogin = new UserLogin(BotConfig.SteamLogin, BotConfig.SteamPassword);
LoginResult loginResult;
while ((loginResult = userLogin.DoLogin()) != LoginResult.LoginOkay) {
switch (loginResult) {
case LoginResult.Need2FA:
userLogin.TwoFactorCode = Program.GetUserInput(BotName, Program.EUserInputType.TwoFactorAuthentication);
break;
default:
Logging.LogGenericError("Unhandled situation: " + loginResult, BotName);
return;
}
}

if (userLogin.Session == null) {
BotDatabase.SteamGuardAccount = null;
Logging.LogGenericError("Session is invalid, linking can't be completed!", BotName);
return;
}

BotDatabase.SteamGuardAccount.FullyEnrolled = true;
BotDatabase.SteamGuardAccount.Session = userLogin.Session;

if (string.IsNullOrEmpty(BotDatabase.SteamGuardAccount.DeviceID)) {
BotDatabase.SteamGuardAccount.DeviceID = Program.GetUserInput(BotName, Program.EUserInputType.DeviceID);
}

BotDatabase.Save();
Logging.LogGenericInfo("Successfully finished importing mobile authenticator!", BotName);
}

private string ResponseStatus() {
if (CardsFarmer.CurrentGamesFarming.Count > 0) {
return "Bot " + BotName + " is currently farming appIDs: " + string.Join(", ", CardsFarmer.CurrentGamesFarming) + " and has a total of " + CardsFarmer.GamesToFarm.Count + " games left to farm.";
Expand Down Expand Up @@ -923,6 +973,9 @@ private void LinkMobileAuthenticator() {
}

Logging.LogGenericInfo("Linking new ASF MobileAuthenticator...", BotName);

InitializeLoginAndPassword();

UserLogin userLogin = new UserLogin(BotConfig.SteamLogin, BotConfig.SteamPassword);
LoginResult loginResult;
while ((loginResult = userLogin.DoLogin()) != LoginResult.LoginOkay) {
Expand Down Expand Up @@ -985,6 +1038,16 @@ private void JoinMasterChat() {
SteamFriends.JoinChat(BotConfig.SteamMasterClanID);
}

private void InitializeLoginAndPassword() {
if (string.IsNullOrEmpty(BotConfig.SteamLogin)) {
BotConfig.SteamLogin = Program.GetUserInput(BotName, Program.EUserInputType.Login);
}

if (string.IsNullOrEmpty(BotConfig.SteamPassword) && string.IsNullOrEmpty(BotDatabase.LoginKey)) {
BotConfig.SteamPassword = Program.GetUserInput(BotName, Program.EUserInputType.Password);
}
}

private void OnConnected(SteamClient.ConnectedCallback callback) {
if (callback == null) {
return;
Expand Down Expand Up @@ -1013,13 +1076,7 @@ private void OnConnected(SteamClient.ConnectedCallback callback) {
}
}

if (string.IsNullOrEmpty(BotConfig.SteamLogin)) {
BotConfig.SteamLogin = Program.GetUserInput(BotName, Program.EUserInputType.Login);
}

if (string.IsNullOrEmpty(BotConfig.SteamPassword) && string.IsNullOrEmpty(BotDatabase.LoginKey)) {
BotConfig.SteamPassword = Program.GetUserInput(BotName, Program.EUserInputType.Password);
}
InitializeLoginAndPassword();

SteamUser.LogOn(new SteamUser.LogOnDetails {
Username = BotConfig.SteamLogin,
Expand Down Expand Up @@ -1245,14 +1302,7 @@ private async void OnLoggedOn(SteamUser.LoggedOnCallback callback) {
// Support and convert SDA files
string maFilePath = Path.Combine(Program.ConfigDirectory, callback.ClientSteamID.ConvertToUInt64() + ".maFile");
if (BotDatabase.SteamGuardAccount == null && File.Exists(maFilePath)) {
Logging.LogGenericInfo("Converting SDA .maFile into ASF format...", BotName);
try {
BotDatabase.SteamGuardAccount = JsonConvert.DeserializeObject<SteamGuardAccount>(File.ReadAllText(maFilePath));
File.Delete(maFilePath);
Logging.LogGenericInfo("Success!", BotName);
} catch (Exception e) {
Logging.LogGenericException(e, BotName);
}
ImportAuthenticator(maFilePath);
}

if (BotConfig.UseAsfAsMobileAuthenticator && TwoFactorAuth == null && BotDatabase.SteamGuardAccount == null) {
Expand Down
2 changes: 1 addition & 1 deletion ArchiSteamFarm/BotDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private BotDatabase(string filePath) {
// This constructor is used only by deserializer
private BotDatabase() { }

private void Save() {
internal void Save() {
lock (FilePath) {
try {
File.WriteAllText(FilePath, JsonConvert.SerializeObject(this));
Expand Down
5 changes: 5 additions & 0 deletions ArchiSteamFarm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ limitations under the License.
namespace ArchiSteamFarm {
internal static class Program {
internal enum EUserInputType : byte {
Unknown,
DeviceID,
Login,
Password,
PhoneNumber,
Expand Down Expand Up @@ -281,6 +283,9 @@ internal static string GetUserInput(string botLogin, EUserInputType userInputTyp
lock (ConsoleLock) {
ConsoleIsBusy = true;
switch (userInputType) {
case EUserInputType.DeviceID:
Console.Write("<" + botLogin + "> Please enter your Device ID (including \"android:\"): ");
break;
case EUserInputType.Login:
Console.Write("<" + botLogin + "> Please enter your login: ");
break;
Expand Down

0 comments on commit 4710d9c

Please sign in to comment.