Skip to content

Commit

Permalink
Add: A way to authenticate with osu! cookies string
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol committed Jun 6, 2020
1 parent a82e6a5 commit 737cf29
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 43 deletions.
1 change: 1 addition & 0 deletions App/Misc/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static LoginData GetLoginData(this ILoginFormView loginForm)
{
loginData.Username = loginForm.Login;
loginData.Password = loginForm.Password;
loginData.OsuCookies = loginForm.OsuCookies;
}

return loginData.isValid() ? loginData : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,71 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Linq;
using System.Reflection;
using CollectionManagerExtensionsDll.Modules.DownloadManager.API;
using System.Text;
using System.Web;

namespace System.Net
{
using System.Text;
using System.Collections.Specialized;

public class CookieAwareWebClient : WebClient
{
public int ClientId = -1;
public string UserAgent { get; set; } = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36";

public void SetCookies(string cookies, string[] cookiesToIgnore, string cookieDomain)
{
foreach (var nameValuePair in HttpUtility.UrlDecode(cookies).Split(';'))
{
var split = nameValuePair.Split('=');
if (cookiesToIgnore.Contains(split[0].Trim()))
continue;
CookieContainer.Add(new Cookie(split[0].Trim(), split[1], "/", cookieDomain));
}
}

public bool IsLoggedIn(string checkUrl, string stringToFind)
{
var request = (HttpWebRequest)WebRequest.Create(checkUrl);
request.CookieContainer = CookieContainer;
request.UserAgent = UserAgent;
try
{
WebResponse response = request.GetResponse() as HttpWebResponse;
using (var reader = new StreamReader(response.GetResponseStream()))
{
string responseText = reader.ReadToEnd();
return responseText.IndexOf(stringToFind, StringComparison.InvariantCultureIgnoreCase) == -1;
}
}
catch (WebException e)
{
if (e.Response is HttpWebResponse resp && resp.StatusCode.GetHashCode() == 422)
{
return false;
}

throw;
}
}

public bool Login(string loginPageAddress, string loginData)
{
var homePageRequest = (HttpWebRequest)WebRequest.Create("https://osu.ppy.sh/home");
homePageRequest.CookieContainer = CookieContainer;
var homeResponse = (HttpWebResponse)homePageRequest.GetResponse();
var token = homeResponse.Cookies["XSRF-TOKEN"].Value;

var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var data = loginData.ToString();
var buffer = Encoding.ASCII.GetBytes("_token=" + token + "&" + loginData.ToString());
request.ContentLength = buffer.Length;
request.CookieContainer = CookieContainer;
request.UserAgent = UserAgent;
var requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
WebResponse response = null;
try
{
response = request.GetResponse();
}
catch (WebException e)
{
if (e.Response is HttpWebResponse resp && resp.StatusCode.GetHashCode() == 422)
{
return false;
}

throw;
}

string responseText;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
responseText = sr.ReadToEnd();
}
response.Close();
return !(responseText.IndexOf("Sign in", StringComparison.InvariantCultureIgnoreCase) > 0);
return IsLoggedIn("https://osu.ppy.sh/home", "Sign in");
}

public CookieAwareWebClient(CookieContainer container)
Expand All @@ -75,7 +88,7 @@ protected override WebRequest GetWebRequest(Uri address)
var request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
request.Timeout = 5 * 1000;
request.UserAgent = "CollectionManager";
request.UserAgent = UserAgent;
return request;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ public class LoginData
{
public string Username { get; set; }
public string Password { get; set; }
public string OsuCookies { get; set; }

public bool isValid()
{
if (string.IsNullOrWhiteSpace(Username) || string.IsNullOrWhiteSpace(Password))
if ((string.IsNullOrWhiteSpace(Username) || string.IsNullOrWhiteSpace(Password)) && string.IsNullOrWhiteSpace(OsuCookies))
return false;
return (Username.Length > 2 && Password.Length > 5);

return (Username.Length > 2 && Password.Length > 5) || !string.IsNullOrWhiteSpace(OsuCookies);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class OsuDownloader : API.DownloadManager
private CookieAwareWebClient webClient;
public int GetUsedQuota()
{
if(webClient==null)
if (webClient == null)
return -1;

var data = webClient.DownloadString(QuotaCheckUrl);
Expand All @@ -33,7 +33,6 @@ public bool Login(LoginData loginData)
var loginAddress = @"https://osu.ppy.sh/session";
string loginDataStr = string.Format("username={0}&password={1}", Uri.EscapeDataString(loginData.Username), Uri.EscapeDataString(loginData.Password));


CookieContainer cookies = null;
//Take all webClients and login/set correct cookies
List<CookieAwareWebClient> webClients = new List<CookieAwareWebClient>();
Expand All @@ -43,8 +42,17 @@ public bool Login(LoginData loginData)
var client = this.Clients.Dequeue();
if (i == clientCount)
{
if (!client.Login(loginAddress, loginDataStr))
return false;
if (!string.IsNullOrEmpty(loginData.OsuCookies))
{
client.SetCookies(loginData.OsuCookies, new[] { "_encid" }, ".ppy.sh");
if (!client.IsLoggedIn(@"https://osu.ppy.sh/home", "Sign in"))
return false;
}
else
{
if (!client.Login(loginAddress, loginDataStr))
return false;
}

cookies = client.CookieContainer;
webClient = client;
Expand Down Expand Up @@ -88,7 +96,7 @@ public override bool CanDownload(DownloadItem downloadItem)
protected override bool DownloadFile(DownloadItem downloadItem)
{
var downloadStarted = base.DownloadFile(downloadItem);
if(downloadStarted)
if (downloadStarted)
DownloadThrottler.RegisterDownload();

return downloadStarted;
Expand Down
1 change: 1 addition & 0 deletions Common/Interfaces/Forms/ILoginForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface ILoginFormView :IForm
{
string Login { get; }
string Password { get; }
string OsuCookies { get; }
bool ClickedLogin { get; }
event EventHandler LoginClick;
event EventHandler CancelClick;
Expand Down
41 changes: 37 additions & 4 deletions GuiComponents/Forms/LoginFormView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions GuiComponents/Forms/LoginFormView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public LoginFormView()

public string Login => ClickedLogin ? textBox_login.Text : "";
public string Password => ClickedLogin ? textBox_password.Text : "";
public string OsuCookies => ClickedLogin ? textBox_osuCookies.Text : "";
public bool ClickedLogin { get; set; }
public event EventHandler LoginClick;
public event EventHandler CancelClick;
Expand Down

0 comments on commit 737cf29

Please sign in to comment.