1414using System . Threading ;
1515using Cryptographic ;
1616using System . Runtime . InteropServices ;
17+ using System . Windows . Forms ;
1718
1819namespace KeyAuth
1920{
@@ -98,6 +99,9 @@ private class response_structure
9899
99100 [ DataMember ]
100101 public List < users > users { get ; set ; }
102+
103+ [ DataMember ( Name = "2fa" , IsRequired = false , EmitDefaultValue = false ) ] // Ensure mapping to "2fa"
104+ public TwoFactorData twoFactor { get ; set ; } // Add a property for the 2FA data
101105 }
102106
103107 public class msg
@@ -354,7 +358,7 @@ public void forgot(string username, string email)
354358 /// </summary>
355359 /// <param name="username">Username</param>
356360 /// <param name="pass">Password</param>
357- public void login ( string username , string pass )
361+ public void login ( string username , string pass , string code = null )
358362 {
359363 CheckInit ( ) ;
360364
@@ -368,7 +372,8 @@ public void login(string username, string pass)
368372 [ "hwid" ] = hwid ,
369373 [ "sessionid" ] = sessionid ,
370374 [ "name" ] = name ,
371- [ "ownerid" ] = ownerid
375+ [ "ownerid" ] = ownerid ,
376+ [ "code" ] = code ?? null
372377 } ;
373378
374379 var response = req ( values_to_upload ) ;
@@ -599,7 +604,7 @@ public void upgrade(string username, string key)
599604 /// Authenticate without using usernames and passwords
600605 /// </summary>
601606 /// <param name="key">Licence used to login with</param>
602- public void license ( string key )
607+ public void license ( string key , string code = null )
603608 {
604609 CheckInit ( ) ;
605610
@@ -612,7 +617,8 @@ public void license(string key)
612617 [ "hwid" ] = hwid ,
613618 [ "sessionid" ] = sessionid ,
614619 [ "name" ] = name ,
615- [ "ownerid" ] = ownerid
620+ [ "ownerid" ] = ownerid ,
621+ [ "code" ] = code ?? null
616622 } ;
617623
618624 var response = req ( values_to_upload ) ;
@@ -661,6 +667,68 @@ public void check()
661667 }
662668 }
663669 /// <summary>
670+ /// Disable two factor authentication (2fa)
671+ /// </summary>
672+ public void disable2fa ( string code )
673+ {
674+ CheckInit ( ) ;
675+
676+ var values_to_upload = new NameValueCollection
677+ {
678+ [ "type" ] = "2fadisable" ,
679+ [ "sessionid" ] = sessionid ,
680+ [ "name" ] = name ,
681+ [ "ownerid" ] = ownerid ,
682+ [ "code" ] = code
683+ } ;
684+
685+ var response = req ( values_to_upload ) ;
686+
687+ var json = response_decoder . string_to_generic < response_structure > ( response ) ;
688+ load_response_struct ( json ) ;
689+ }
690+ /// <summary>
691+ /// Enable two factor authentication (2fa)
692+ /// </summary>
693+ public void enable2fa ( string code = null )
694+ {
695+ CheckInit ( ) ;
696+
697+ var values_to_upload = new NameValueCollection
698+ {
699+ [ "type" ] = "2faenable" ,
700+ [ "sessionid" ] = sessionid ,
701+ [ "name" ] = name ,
702+ [ "ownerid" ] = ownerid ,
703+ [ "code" ] = code
704+ } ;
705+
706+ var response = req ( values_to_upload ) ;
707+
708+ var json = response_decoder . string_to_generic < response_structure > ( response ) ;
709+ load_response_struct ( json ) ;
710+
711+ if ( json . success )
712+ {
713+ if ( code == null ) // First time enabling 2FA, no code provided
714+ {
715+ // Display the secret code to the user
716+ Clipboard . SetText ( json . twoFactor . SecretCode ) ;
717+ System . Windows . MessageBox . Show ( $ "Your 2FA Secret Code has been copied to your clipboard! \n \n : { json . twoFactor . SecretCode } ", "2FA Secret" ) ;
718+
719+ }
720+ else // Code provided by the user
721+ {
722+ System . Windows . MessageBox . Show ( "2FA has been successfully enabled!" , "2FA Setup" ) ;
723+ }
724+ }
725+ else
726+ {
727+ Thread . Sleep ( 3000 ) ;
728+ TerminateProcess ( GetCurrentProcess ( ) , 1 ) ;
729+ }
730+ }
731+ /// <summary>
664732 /// Change the data of an existing user variable, *User must be logged in*
665733 /// </summary>
666734 /// <param name="var">User variable name</param>
@@ -1282,6 +1350,16 @@ private void load_user_data(user_data_structure data)
12821350 }
12831351 #endregion
12841352
1353+ [ DataContract ]
1354+ private class TwoFactorData
1355+ {
1356+ [ DataMember ( Name = "secret_code" ) ]
1357+ public string SecretCode { get ; set ; }
1358+
1359+ [ DataMember ( Name = "QRCode" ) ]
1360+ public string QRCode { get ; set ; }
1361+ }
1362+
12851363 #region response_struct
12861364 public response_class response = new response_class ( ) ;
12871365
0 commit comments