1+ using System . Text ;
2+ using System . Web ;
3+ using HtmlAgilityPack ;
4+ using SaveMyMyimaths . Records ;
5+
6+ namespace SaveMyMyimaths ;
7+
8+ public static class LoginHandler
9+ {
10+ private static string _csrf = "" ;
11+ private static readonly int TaskAmount = 6 ;
12+ private static AccountInfoRecord _accountInfo = null ! ;
13+
14+ public static async Task Login ( AccountInfoRecord account )
15+ {
16+ _accountInfo = account ;
17+
18+ await SchoolLoginAsync ( account . SchoolUsername , account . SchoolPassword ) ;
19+ await PortalLoginAsync ( account . PortalUsername , account . PortalPassword ) ;
20+ }
21+
22+ private static async Task SchoolLoginAsync ( string username , string password )
23+ {
24+ var client = new HttpClient ( ) ;
25+ var request = new HttpRequestMessage ( HttpMethod . Post , "https://login.myimaths.com/login" ) ;
26+
27+ Utilities . Utilities . AddHeaders ( ref request ) ;
28+ request . Headers . Add ( "Referer" , "https://login.myimaths.com/login" ) ;
29+ request . Headers . Add ( "Origin" , "https://login.myimaths.com" ) ;
30+
31+ Registry . Registry . NewProgress ( "0" , "Fetching CSRF token" , TaskAmount ) ;
32+ _csrf = await FetchCsrfAsync ( "https://login.myimaths.com/login" , false ) ;
33+
34+ string requestString =
35+ "utf8=%E2%9C%93&authenticity_token=" + HttpUtility . UrlEncode ( _csrf , Encoding . UTF8 ) +
36+ "&_form_generated_at=" +
37+ HttpUtility . UrlEncode ( DateTimeOffset . UtcNow . ToString ( "yyyy-MM-ddTHH:mm:sszzz" ) , Encoding . UTF8 ) +
38+ "&account%5Buser_name%5D=" + HttpUtility . UrlEncode ( username , Encoding . UTF8 ) +
39+ "&account%5Bpassword%5D=" + HttpUtility . UrlEncode ( password , Encoding . UTF8 ) + "&commit=Log+in" ;
40+
41+ request . Content = new StringContent ( requestString , Encoding . UTF8 , "application/x-www-form-urlencoded" ) ;
42+
43+ Registry . Registry . GetProgress ( "0" ) . Update ( "Logging in to Myimaths..." ) ;
44+ var response = await client . SendAsync ( request ) ;
45+ Utilities . Utilities . UpdateCookie ( response ) ;
46+
47+ Registry . Registry . GetProgress ( "0" ) . Update ( "Updating Cookie..." ) ;
48+ client = new HttpClient ( new HttpClientHandler { CookieContainer = Registry . Registry . Container } ) ;
49+ response = await client . GetAsync ( "https://app.myimaths.com/myportal/library/39?login_modal=true" ) ;
50+ Utilities . Utilities . UpdateCookie ( response ) ;
51+ }
52+
53+
54+ private static async Task < string > FetchCsrfAsync ( string uri , bool isCookieNeeded )
55+ {
56+ var doc = new HtmlDocument ( ) ;
57+ var client = isCookieNeeded
58+ ? new HttpClient ( new HttpClientHandler { CookieContainer = Registry . Registry . Container } )
59+ : new HttpClient ( ) ;
60+
61+ var response = await client . GetAsync ( uri ) ;
62+ string responseContent = await response . Content . ReadAsStringAsync ( ) ;
63+ doc . LoadHtml ( responseContent ) ;
64+
65+ return doc . DocumentNode . SelectSingleNode ( "//meta[@name='csrf-token']" )
66+ . GetAttributeValue ( "content" , string . Empty ) ;
67+ }
68+
69+ private static async Task PortalLoginAsync ( string portalUsername , string portalPassword )
70+ {
71+ var client = new HttpClient ( new HttpClientHandler { CookieContainer = Registry . Registry . Container } ) ;
72+ var request = new HttpRequestMessage ( HttpMethod . Post , "https://app.myimaths.com/myportal/student/authenticate" ) ;
73+
74+ Utilities . Utilities . AddHeaders ( ref request ) ;
75+ request . Headers . Add ( "Referer" , "https://app.myimaths.com/myportal/library/39?login_modal=true" ) ;
76+ request . Headers . Add ( "Origin" , "https://app.myimaths.com" ) ;
77+
78+ Registry . Registry . GetProgress ( "0" ) . Update ( "Fetching CSRF token..." ) ;
79+ _csrf = await FetchCsrfAsync ( "https://app.myimaths.com/myportal/library/39" , true ) ;
80+
81+ string requestString =
82+ "utf8=%E2%9C%93&authenticity_token=" + HttpUtility . UrlEncode ( _csrf , Encoding . UTF8 ) +
83+ "&student%5Buser_name%5D=" + HttpUtility . UrlEncode ( portalUsername , Encoding . UTF8 ) +
84+ "&student%5Bpassword%5D=" + HttpUtility . UrlEncode ( portalPassword , Encoding . UTF8 ) + "&commit=Log+in" ;
85+ request . Content = new StringContent ( requestString , Encoding . UTF8 , "application/x-www-form-urlencoded" ) ;
86+
87+ Registry . Registry . GetProgress ( "0" ) . Update ( "Logging in to the student portal..." ) ;
88+ var response = await client . SendAsync ( request ) ;
89+ Utilities . Utilities . UpdateCookie ( response ) ;
90+ }
91+
92+ public static async Task FindAllTaskAsync ( )
93+ {
94+ var client = new HttpClient ( new HttpClientHandler { CookieContainer = Registry . Registry . Container } ) ;
95+
96+ Registry . Registry . GetProgress ( "0" ) . Update ( "Fetching unfinished tasks..." ) ;
97+ var response = await client . GetAsync ( "https://app.myimaths.com/myportal/student/my_homework" ) ;
98+ Utilities . Utilities . UpdateCookie ( response ) ;
99+
100+ Registry . Registry . GetProgress ( "0" ) . Update ( "Finished." ) ;
101+
102+ string responseContent = await response . Content . ReadAsStringAsync ( ) ;
103+
104+ var doc = new HtmlDocument ( ) ;
105+ doc . LoadHtml ( responseContent ) ;
106+
107+ var hrefs = doc . DocumentNode . SelectNodes ( "//a[@class='primary btn btn-m']" ) ;
108+ var infos = doc . DocumentNode . SelectNodes ( "//h2[@class='accordion-header']/button" ) ;
109+
110+ string ? studentName = doc . DocumentNode
111+ . SelectSingleNode ( "//div[@class='student-name d-flex align-items-center']/div[not(@*)]" )
112+ . InnerText ;
113+ Registry . Registry . Accounts . TryAdd ( studentName , _accountInfo ) ;
114+ Registry . Registry . DumpAccountInfoToFile ( ) ;
115+
116+ if ( hrefs == null ) return ;
117+ //I know this is extremely not elegant, but man, I know nothing about algorithms and please forgive me.
118+ for ( int i = 0 ; i < hrefs . Count ; i ++ )
119+ {
120+ string ? href = hrefs [ i ] . GetAttributeValue ( "href" , string . Empty ) ;
121+ string name = infos [ i ] . InnerText . Trim ( ) . Split ( "\n \n \n \n " ) [ 0 ] ;
122+ string dueDayInfo = infos [ i ] . InnerText . Trim ( ) . Split ( "\n \n \n \n " ) [ 1 ] ;
123+
124+ Registry . Registry . TaskInfoRecordsList . Add ( new TaskInfoRecord ( href , name , dueDayInfo ) ) ;
125+ }
126+ }
127+ }
0 commit comments