@@ -30,62 +30,48 @@ public CookieState State
3030 private const string AuthPattern = $@ "\t{ AuthCookieName } \t(.+?)(;|$)";
3131 private string CookiesPath => Path . Combine ( Paths . Roblox , "LocalStorage" , "RobloxCookies.dat" ) ;
3232
33- public async Task < AuthenticatedUser ? > GetAuthenticated ( )
33+ public async Task < HttpResponseMessage > AuthRequest ( HttpRequestMessage request )
3434 {
35- const string LOG_IDENT = "CookiesManager::GetAuthenticated" ;
36-
37- var request = new HttpRequestMessage ( )
38- {
39- Method = HttpMethod . Get ,
40- RequestUri = new Uri ( "https://users.roblox.com/v1/users/authenticated" )
41- } ;
35+ string ? host = request . RequestUri ? . Host ;
4236
43- request . Headers . Add ( "Cookie" , $ ".ROBLOSECURITY={ AuthCookie } ") ;
37+ if ( host is null )
38+ throw new ArgumentNullException ( "Host cannot be null" ) ;
4439
45- try
46- {
47- HttpResponseMessage response = await App . HttpClient . SendAsync ( request ) ;
48- response . EnsureSuccessStatusCode ( ) ;
40+ if (
41+ ! host . Equals ( "roblox.com" , StringComparison . OrdinalIgnoreCase ) &&
42+ ! host . EndsWith ( ".roblox.com" , StringComparison . OrdinalIgnoreCase )
43+ )
44+ throw new HttpRequestException ( "Host must end with roblox.com" ) ;
4945
50- string content = await response . Content . ReadAsStringAsync ( ) ;
51- AuthenticatedUser user = JsonSerializer . Deserialize < AuthenticatedUser > ( content ) ! ;
46+ if ( ! Enabled )
47+ throw new NullReferenceException ( "Cookie access is not enabled" ) ;
5248
53- return user ;
54- }
55- catch ( HttpRequestException ex )
56- {
57- App . Logger . WriteLine ( LOG_IDENT , "Failed to get authenticated user" ) ;
58- App . Logger . WriteException ( LOG_IDENT , ex ) ;
59- }
49+ request . Headers . Add ( "Cookie" , $ ".ROBLOSECURITY={ AuthCookie } ") ;
50+ var response = await App . HttpClient . SendAsync ( request ) ;
6051
61- return null ;
52+ return response ;
6253 }
6354
64- public async Task < UserChannel ? > GetUserChannel ( string binaryType )
65- {
66- const string LOG_IDENT = "CookiesManager::GetUserChannel" ;
67-
68- var request = new HttpRequestMessage ( )
69- {
70- Method = HttpMethod . Get ,
71- RequestUri = new Uri ( $ "https://clientsettings.roblox.com/v2/user-channel?binaryType={ binaryType } ")
72- } ;
55+ public async Task < HttpResponseMessage > AuthGet ( string uri ) => await AuthRequest ( new HttpRequestMessage { RequestUri = new Uri ( uri ) , Method = HttpMethod . Get } ) ;
56+ public async Task < HttpResponseMessage > AuthPost ( string uri , HttpContent ? content ) => await AuthRequest ( new HttpRequestMessage { RequestUri = new Uri ( uri ) , Content = content , Method = HttpMethod . Post } ) ;
7357
74- request . Headers . Add ( "Cookie" , $ ".ROBLOSECURITY={ AuthCookie } ") ;
58+ public async Task < AuthenticatedUser ? > GetAuthenticated ( )
59+ {
60+ const string LOG_IDENT = "CookiesManager::GetAuthenticated" ;
7561
7662 try
7763 {
78- HttpResponseMessage response = await App . HttpClient . SendAsync ( request ) ;
64+ HttpResponseMessage response = await AuthGet ( "https://users.roblox.com/v1/users/authenticated" ) ;
7965 response . EnsureSuccessStatusCode ( ) ;
8066
8167 string content = await response . Content . ReadAsStringAsync ( ) ;
82- UserChannel channelInfo = JsonSerializer . Deserialize < UserChannel > ( content ) ! ;
68+ AuthenticatedUser user = JsonSerializer . Deserialize < AuthenticatedUser > ( content ) ! ;
8369
84- return channelInfo ;
70+ return user ;
8571 }
8672 catch ( HttpRequestException ex )
8773 {
88- App . Logger . WriteLine ( LOG_IDENT , "Failed to get user channel " ) ;
74+ App . Logger . WriteLine ( LOG_IDENT , "Failed to get authenticated user " ) ;
8975 App . Logger . WriteException ( LOG_IDENT , ex ) ;
9076 }
9177
0 commit comments