Skip to content

Commit b1008e9

Browse files
Added two factor authentication
1 parent d99c33d commit b1008e9

File tree

2 files changed

+116
-11
lines changed

2 files changed

+116
-11
lines changed

Console/KeyAuth.cs

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ private class response_structure
9898

9999
[DataMember]
100100
public List<users> users { get; set; }
101+
102+
[DataMember(Name = "2fa", IsRequired = false, EmitDefaultValue = false)] // Ensure mapping to "2fa"
103+
public TwoFactorData twoFactor { get; set; } // Add a property for the 2FA data
101104
}
102105

103106
public class msg
@@ -354,7 +357,7 @@ public void forgot(string username, string email)
354357
/// </summary>
355358
/// <param name="username">Username</param>
356359
/// <param name="pass">Password</param>
357-
public void login(string username, string pass)
360+
public void login(string username, string pass, string code = null)
358361
{
359362
CheckInit();
360363

@@ -368,7 +371,8 @@ public void login(string username, string pass)
368371
["hwid"] = hwid,
369372
["sessionid"] = sessionid,
370373
["name"] = name,
371-
["ownerid"] = ownerid
374+
["ownerid"] = ownerid,
375+
["code"] = code ?? string.Empty
372376
};
373377

374378
var response = req(values_to_upload);
@@ -599,7 +603,7 @@ public void upgrade(string username, string key)
599603
/// Authenticate without using usernames and passwords
600604
/// </summary>
601605
/// <param name="key">Licence used to login with</param>
602-
public void license(string key)
606+
public void license(string key, string code = null)
603607
{
604608
CheckInit();
605609

@@ -612,7 +616,8 @@ public void license(string key)
612616
["hwid"] = hwid,
613617
["sessionid"] = sessionid,
614618
["name"] = name,
615-
["ownerid"] = ownerid
619+
["ownerid"] = ownerid,
620+
["code"] = code ?? string.Empty
616621
};
617622

618623
var response = req(values_to_upload);
@@ -661,6 +666,73 @@ public void check()
661666
}
662667
}
663668
/// <summary>
669+
/// Disable two factor authentication (2fa)
670+
/// </summary>
671+
public void disable2fa(string code)
672+
{
673+
CheckInit();
674+
675+
var values_to_upload = new NameValueCollection
676+
{
677+
["type"] = "2fadisable",
678+
["sessionid"] = sessionid,
679+
["name"] = name,
680+
["ownerid"] = ownerid,
681+
["code"] = code
682+
};
683+
684+
var response = req(values_to_upload);
685+
686+
var json = response_decoder.string_to_generic<response_structure>(response);
687+
load_response_struct(json);
688+
689+
Console.WriteLine(json.message);
690+
}
691+
/// <summary>
692+
/// Enable two factor authentication (2fa)
693+
/// </summary>
694+
public void enable2fa(string code = null)
695+
{
696+
CheckInit();
697+
698+
var values_to_upload = new NameValueCollection
699+
{
700+
["type"] = "2faenable",
701+
["sessionid"] = sessionid,
702+
["name"] = name,
703+
["ownerid"] = ownerid,
704+
["code"] = code
705+
};
706+
707+
var response = req(values_to_upload);
708+
709+
var json = response_decoder.string_to_generic<response_structure>(response);
710+
load_response_struct(json);
711+
712+
if (json.success)
713+
{
714+
if (code == null)
715+
{
716+
Console.WriteLine($"Your 2FA Secret is: {json.twoFactor.SecretCode}");
717+
718+
Console.Write("Enter the 6 digit authentication code from your authentication app: ");
719+
string code6Digit = Console.ReadLine();
720+
this.enable2fa(code6Digit);
721+
}
722+
else
723+
{
724+
Console.WriteLine("2FA has been successfully enabled!");
725+
Thread.Sleep(3000);
726+
}
727+
}
728+
else
729+
{
730+
Console.WriteLine($"Error: {json.message}");
731+
Thread.Sleep(3000);
732+
TerminateProcess(GetCurrentProcess(), 1);
733+
}
734+
}
735+
/// <summary>
664736
/// Change the data of an existing user variable, *User must be logged in*
665737
/// </summary>
666738
/// <param name="var">User variable name</param>
@@ -1173,7 +1245,7 @@ private static bool assertSSL(object sender, X509Certificate certificate, X509Ch
11731245

11741246
private static void sigCheck(string resp, WebHeaderCollection headers, string type)
11751247
{
1176-
if(type == "log" || type == "file") // log doesn't return a response.
1248+
if(type == "log" || type == "file" || type == "2faenable" || type == "2fadisable") // log doesn't return a response.
11771249
{
11781250
return;
11791251
}
@@ -1282,6 +1354,16 @@ private void load_user_data(user_data_structure data)
12821354
}
12831355
#endregion
12841356

1357+
[DataContract]
1358+
private class TwoFactorData
1359+
{
1360+
[DataMember(Name = "secret_code")]
1361+
public string SecretCode { get; set; }
1362+
1363+
[DataMember(Name = "QRCode")]
1364+
public string QRCode { get; set; }
1365+
}
1366+
12851367
#region response_struct
12861368
public response_class response = new response_class();
12871369

@@ -1295,7 +1377,7 @@ private void load_response_struct(response_structure data)
12951377
{
12961378
response.success = data.success;
12971379
response.message = data.message;
1298-
}
1380+
}
12991381
#endregion
13001382

13011383
private json_wrapper response_decoder = new json_wrapper(new response_structure());

Console/Program.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Program
2424

2525
public static api KeyAuthApp = new api(
2626
name: "", // Application Name
27-
ownerid: "", // Owner ID
28-
version: "" // Application Version /*
27+
ownerid: "", // Account ID
28+
version: "" // Application version. Used for automatic downloads see video here https://www.youtube.com/watch?v=kW195PLCBKs
2929
//path: @"Your_Path_Here" // (OPTIONAL) see tutorial here https://www.youtube.com/watch?v=I9rxt821gMk&t=1s
3030
);
3131

@@ -58,7 +58,7 @@ static void Main(string[] args)
5858

5959
Console.Write("\n [1] Login\n [2] Register\n [3] Upgrade\n [4] License key only\n [5] Forgot password\n\n Choose option: ");
6060

61-
string username, password, key, email;
61+
string username, password, key, email, code;
6262

6363
int option = int.Parse(Console.ReadLine());
6464
switch (option)
@@ -68,7 +68,9 @@ static void Main(string[] args)
6868
username = Console.ReadLine();
6969
Console.Write("\n\n Enter password: ");
7070
password = Console.ReadLine();
71-
KeyAuthApp.login(username, password);
71+
Console.Write("\n\n Enter 2fa code: (not using 2fa? Just press enter) ");
72+
code = Console.ReadLine();
73+
KeyAuthApp.login(username, password, code);
7274
break;
7375
case 2:
7476
Console.Write("\n\n Enter username: ");
@@ -95,7 +97,9 @@ static void Main(string[] args)
9597
case 4:
9698
Console.Write("\n\n Enter license: ");
9799
key = Console.ReadLine();
98-
KeyAuthApp.license(key);
100+
Console.Write("\n\n Enter 2fa code: (not using 2fa? Just press enter");
101+
code = Console.ReadLine();
102+
KeyAuthApp.license(key, code);
99103
break;
100104
case 5:
101105
Console.Write("\n\n Enter username: ");
@@ -140,6 +144,25 @@ static void Main(string[] args)
140144
Console.WriteLine(" Subscription name: " + KeyAuthApp.user_data.subscriptions[i].subscription + " - Expires at: " + UnixTimeToDateTime(long.Parse(KeyAuthApp.user_data.subscriptions[i].expiry)) + " - Time left in seconds: " + KeyAuthApp.user_data.subscriptions[i].timeleft);
141145
}
142146

147+
Console.Write("\n [1] Enable 2FA\n [2] Disable 2FA\n Choose option: ");
148+
int tfaOptions = int.Parse(Console.ReadLine());
149+
switch (tfaOptions)
150+
{
151+
case 1:
152+
KeyAuthApp.enable2fa();
153+
break;
154+
case 2:
155+
Console.Write("Enter your 6 digit authorization code: ");
156+
code = Console.ReadLine();
157+
KeyAuthApp.disable2fa(code);
158+
break;
159+
default:
160+
Console.WriteLine("\n\n Invalid Selection");
161+
Thread.Sleep(2500);
162+
TerminateProcess(GetCurrentProcess(), 1);
163+
break; // no point in this other than to not get error from IDE
164+
}
165+
143166
Console.WriteLine("\n Closing in five seconds...");
144167
Thread.Sleep(-1);
145168
Environment.Exit(0);

0 commit comments

Comments
 (0)