mirror of
https://github.com/ZuneDev/ZuneShell.dll.git
synced 2026-07-27 13:11:51 -07:00
Implemented crude Escargot login
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using Meziantou.Framework.Win32;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Microsoft.Zune.Service
|
||||
{
|
||||
internal static partial class Escargot
|
||||
{
|
||||
const string ESCARGOT_NOTRST_URL = "https://login.zunes.me/NotRST.srf";
|
||||
const string HEADER_X_USER = "X-User";
|
||||
const string HEADER_X_PASS = "X-Password";
|
||||
const string HEADER_X_TOKEN = "X-Token";
|
||||
|
||||
private static string _token;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the username of the currently authenticated user.
|
||||
/// </summary>
|
||||
public static string Username { get; private set; }
|
||||
|
||||
public static bool HasToken => _token != null;
|
||||
|
||||
public static void CacheToken()
|
||||
{
|
||||
if (_token == null)
|
||||
return;
|
||||
|
||||
// Ensure that alternative Zune launchers can only access
|
||||
// credentials given explicitly to them by the user.
|
||||
string appName = Assembly.GetEntryAssembly().FullName;
|
||||
|
||||
CredentialManager.DeleteCredential(appName);
|
||||
CredentialManager.WriteCredential(appName, Username, _token, CredentialPersistence.LocalMachine);
|
||||
}
|
||||
|
||||
public static bool ReadCachedToken()
|
||||
{
|
||||
string appName = Assembly.GetEntryAssembly().FullName;
|
||||
|
||||
var cred = CredentialManager.ReadCredential(appName);
|
||||
if (cred == null)
|
||||
return false;
|
||||
|
||||
Username = cred.UserName;
|
||||
_token = cred.Password;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void ClearToken()
|
||||
{
|
||||
_token = null;
|
||||
Username = null;
|
||||
}
|
||||
|
||||
private static bool TrySetCredentials(string token, string username)
|
||||
{
|
||||
if (token == null)
|
||||
return false;
|
||||
|
||||
Username = username;
|
||||
_token = token;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#if !OPENZUNE
|
||||
|
||||
using System.Net;
|
||||
|
||||
namespace Microsoft.Zune.Service
|
||||
{
|
||||
partial class Escargot
|
||||
{
|
||||
public static bool TrySignIn(string username, string password)
|
||||
{
|
||||
var request = WebRequest.Create(ESCARGOT_NOTRST_URL);
|
||||
request.Headers.Add(HEADER_X_USER, username);
|
||||
request.Headers.Add(HEADER_X_PASS, password);
|
||||
|
||||
try
|
||||
{
|
||||
var response = request.GetResponse();
|
||||
string token = response.Headers[HEADER_X_TOKEN];
|
||||
|
||||
return TrySetCredentials(token, username);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
#if OPENZUNE
|
||||
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Microsoft.Zune.Service
|
||||
{
|
||||
partial class Escargot
|
||||
{
|
||||
private static readonly HttpClient _client = new();
|
||||
|
||||
public static bool TrySignIn(string username, string password)
|
||||
{
|
||||
HttpRequestMessage request = new(HttpMethod.Post, ESCARGOT_NOTRST_URL);
|
||||
request.Headers.TryAddWithoutValidation(HEADER_X_USER, username);
|
||||
request.Headers.TryAddWithoutValidation(HEADER_X_PASS, password);
|
||||
|
||||
try
|
||||
{
|
||||
var response = _client.Send(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
string token = response.Headers.GetValues(HEADER_X_TOKEN).FirstOrDefault();
|
||||
|
||||
return TrySetCredentials(token, username);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -10,9 +10,16 @@ namespace Microsoft.Zune.Service
|
||||
{
|
||||
private bool disposedValue;
|
||||
|
||||
private static Service2 _instance = new Service2();
|
||||
private static Service2 _instance = new Service2(true);
|
||||
public static Service2 Instance => _instance;
|
||||
|
||||
public bool UseNewSignIn { get; }
|
||||
|
||||
private Service2(bool useNewSignIn)
|
||||
{
|
||||
UseNewSignIn = useNewSignIn;
|
||||
}
|
||||
|
||||
public static EListType ContentTypeToListType(EContentType contentType)
|
||||
{
|
||||
switch (contentType)
|
||||
@@ -313,7 +320,13 @@ namespace Microsoft.Zune.Service
|
||||
|
||||
public uint GetSignedInGeoId() => Service.Instance.GetSignedInGeoId();
|
||||
|
||||
public string GetSignedInUsername() => Service.Instance.GetSignedInUsername();
|
||||
public string GetSignedInUsername()
|
||||
{
|
||||
if (!UseNewSignIn)
|
||||
return Service.Instance.GetSignedInUsername();
|
||||
else
|
||||
return Escargot.Username;
|
||||
}
|
||||
|
||||
public string GetSignInAtStartupUsername() => Service.Instance.GetSignInAtStartupUsername();
|
||||
|
||||
@@ -386,7 +399,13 @@ namespace Microsoft.Zune.Service
|
||||
|
||||
public bool IsParentallyControlled() => Service.Instance.IsParentallyControlled();
|
||||
|
||||
public bool IsSignedIn() => Service.Instance.IsSignedIn();
|
||||
public bool IsSignedIn()
|
||||
{
|
||||
if (!UseNewSignIn)
|
||||
return Service.Instance.IsSignedIn();
|
||||
else
|
||||
return Escargot.HasToken;
|
||||
}
|
||||
|
||||
public bool IsSignedInWithSubscription() => Service.Instance.IsSignedInWithSubscription();
|
||||
|
||||
@@ -439,14 +458,40 @@ namespace Microsoft.Zune.Service
|
||||
=> Service.Instance.SetUserTrackRating(iUserId, iRating, guidTrackMediaId, guidAlbumMediaId, iTrackNumber, strTitle, msDuration, strAlbum, strArtist, strGenre, strServiceContext);
|
||||
|
||||
public void SignIn(string strUsername, string strPassword, bool fRememberUsername, bool fRememberPassword, bool fAutomaticallySignInAtStartup, AsyncCompleteHandler eventHandler)
|
||||
=> Service.Instance.SignIn(strUsername, strPassword, fRememberUsername, fRememberPassword, fAutomaticallySignInAtStartup, eventHandler);
|
||||
{
|
||||
if (!UseNewSignIn)
|
||||
{
|
||||
Service.Instance.SignIn(strUsername, strPassword, fRememberUsername, fRememberPassword, fAutomaticallySignInAtStartup, eventHandler);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool success = Escargot.TrySignIn(strUsername, strPassword);
|
||||
if (success && fRememberPassword)
|
||||
{
|
||||
Escargot.CacheToken();
|
||||
}
|
||||
|
||||
int hr = success ? 0 : unchecked((int)0x80070057);
|
||||
eventHandler(hr);
|
||||
}
|
||||
}
|
||||
|
||||
public bool SignInAtStartup(string strUsername) => Service.Instance.SignInAtStartup(strUsername);
|
||||
|
||||
public bool SignInPasswordRequired(string strUsername)
|
||||
=> Service.Instance.SignInPasswordRequired(strUsername);
|
||||
|
||||
public void SignOut() => Service.Instance.SignOut();
|
||||
public void SignOut()
|
||||
{
|
||||
if (!UseNewSignIn)
|
||||
{
|
||||
Service.Instance.SignOut();
|
||||
}
|
||||
else
|
||||
{
|
||||
Escargot.ClearToken();
|
||||
}
|
||||
}
|
||||
|
||||
public bool SubscriptionPendingCancel() => Service.Instance.SubscriptionPendingCancel();
|
||||
|
||||
|
||||
@@ -36,11 +36,16 @@
|
||||
<ItemGroup Condition="$(UseOpenZune)">
|
||||
<PackageReference Include="StrixMusic.Sdk" Version="0.0.3-alpha" />
|
||||
<PackageReference Include="StrixMusic.Cores.LocalFiles" Version="1.0.0" />
|
||||
<PackageReference Include="Meziantou.Framework.Win32.CredentialManager" Version="1.4.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup Condition=" $(UseOpenZune) ">
|
||||
<PropertyGroup Condition="$(UseOpenZune)">
|
||||
<ApplicationVersion>5.0.0.0</ApplicationVersion>
|
||||
<DefineConstants>$(DefineConstants);OPENZUNE</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="!$(UseOpenZune)">
|
||||
<ProjectReference Include="..\libs\Meziantou.Framework.Win32.CredentialManager\Meziantou.Framework.Win32.CredentialManager.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user