Completed core (except WL and history retrieval)
This commit is contained in:
@@ -130,7 +130,6 @@
|
|||||||
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
|
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Models\SuspendedUser.cs" />
|
|
||||||
<Compile Include="Services\DownloadsCenter.cs" />
|
<Compile Include="Services\DownloadsCenter.cs" />
|
||||||
<Compile Include="Extensions.cs" />
|
<Compile Include="Extensions.cs" />
|
||||||
<Compile Include="Utils\Feedback.cs" />
|
<Compile Include="Utils\Feedback.cs" />
|
||||||
@@ -157,7 +156,7 @@
|
|||||||
<Version>0.14.0</Version>
|
<Version>0.14.0</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="ExtendedYouTubeAPI">
|
<PackageReference Include="ExtendedYouTubeAPI">
|
||||||
<Version>1.0.2</Version>
|
<Version>1.0.3</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Google.Apis.Auth">
|
<PackageReference Include="Google.Apis.Auth">
|
||||||
<Version>1.45.0</Version>
|
<Version>1.45.0</Version>
|
||||||
@@ -184,10 +183,10 @@
|
|||||||
<Version>10.1901.28001</Version>
|
<Version>10.1901.28001</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.UI.Xaml">
|
<PackageReference Include="Microsoft.UI.Xaml">
|
||||||
<Version>2.3.200213001</Version>
|
<Version>2.4.0</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="YoutubeExplode">
|
<PackageReference Include="YoutubeExplode">
|
||||||
<Version>5.0.2</Version>
|
<Version>5.0.3</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace FoxTube.Models
|
|
||||||
{
|
|
||||||
public class SuspendedUser
|
|
||||||
{
|
|
||||||
public string Name { get; set; }
|
|
||||||
public string Email { get; set; }
|
|
||||||
public string Avatar { get; set; }
|
|
||||||
public string RefreshToken { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+24
-19
@@ -5,54 +5,59 @@ using Google.Apis.Services;
|
|||||||
using Google.Apis.YouTube.v3;
|
using Google.Apis.YouTube.v3;
|
||||||
using Google.Apis.YouTube.v3.Data;
|
using Google.Apis.YouTube.v3.Data;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using YouTube;
|
using YouTube;
|
||||||
|
using YoutubeExplode;
|
||||||
|
|
||||||
namespace FoxTube.Models
|
namespace FoxTube.Models
|
||||||
{
|
{
|
||||||
public class User
|
public class User
|
||||||
{
|
{
|
||||||
public Userinfoplus UserInfo { get; }
|
public Userinfoplus UserInfo { get; private set; }
|
||||||
public UserCredential Credential { get; }
|
public UserCredential Credential { get; private set; }
|
||||||
public Channel Channel { get; private set; }
|
public Channel Channel { get; private set; }
|
||||||
public List<Subscription> Subscriptions { get; } = new List<Subscription>();
|
public List<Subscription> Subscriptions { get; } = new List<Subscription>();
|
||||||
public ExtendedYouTubeService Service { get; }
|
|
||||||
|
|
||||||
public User(UserCredential credential)
|
public ExtendedYouTubeService Service { get; private set; }
|
||||||
|
public YoutubeClient Client { get; private set; }
|
||||||
|
|
||||||
|
public static async Task<User> GetUser(UserCredential credential)
|
||||||
{
|
{
|
||||||
Credential = credential;
|
User user = new User();
|
||||||
|
|
||||||
|
user.Credential = credential;
|
||||||
BaseClientService.Initializer initializer = new BaseClientService.Initializer
|
BaseClientService.Initializer initializer = new BaseClientService.Initializer
|
||||||
{
|
{
|
||||||
ApplicationName = "FoxTube",
|
ApplicationName = "FoxTube",
|
||||||
HttpClientInitializer = Credential
|
HttpClientInitializer = credential
|
||||||
};
|
};
|
||||||
|
|
||||||
Service = new ExtendedYouTubeService(initializer);
|
user.Service = new ExtendedYouTubeService(initializer);
|
||||||
|
|
||||||
UserInfo = new Oauth2Service(initializer).Userinfo.Get().Execute();
|
user.UserInfo = await new Oauth2Service(initializer).Userinfo.Get().ExecuteAsync();
|
||||||
|
|
||||||
// TODO: Retrieve history and WL
|
// TODO: Retrieve history and WL
|
||||||
|
|
||||||
SubscriptionsResource.ListRequest subRequest = Service.Subscriptions.List("snippet");
|
SubscriptionsResource.ListRequest subRequest = user.Service.Subscriptions.List("snippet");
|
||||||
subRequest.Mine = true;
|
subRequest.Mine = true;
|
||||||
subRequest.MaxResults = 50;
|
subRequest.MaxResults = 50;
|
||||||
subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance;
|
subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance;
|
||||||
SubscriptionListResponse subResponse;
|
SubscriptionListResponse subResponse;
|
||||||
string nextToken = null;
|
|
||||||
Subscriptions.Clear();
|
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
subRequest.PageToken = nextToken;
|
subResponse = await subRequest.ExecuteAsync();
|
||||||
subResponse = subRequest.Execute();
|
subRequest.PageToken = subResponse.NextPageToken;
|
||||||
foreach (Subscription s in subResponse.Items)
|
|
||||||
Subscriptions.Add(s);
|
|
||||||
nextToken = subResponse.NextPageToken;
|
|
||||||
|
|
||||||
} while (!string.IsNullOrWhiteSpace(nextToken));
|
user.Subscriptions.AddRange(subResponse.Items);
|
||||||
|
|
||||||
var request = Service.Channels.List("snippet,contentDetails,brandingSettings");
|
} while (!string.IsNullOrWhiteSpace(subRequest.PageToken));
|
||||||
|
|
||||||
|
var request = user.Service.Channels.List("snippet,contentDetails,brandingSettings");
|
||||||
request.Mine = true;
|
request.Mine = true;
|
||||||
Channel = request.Execute().Items[0];
|
user.Channel = request.Execute().Items[0];
|
||||||
|
|
||||||
|
return user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace FoxTube.Services
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// TODO: Add backend
|
// TODO: Add backend
|
||||||
HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/FoxTube/Messages?toast=true&publishedAfter={storage.Values["Inbox.lastCheck"]}&lang={Settings.Language}&appVersion={Metrics.CurrentVersion}");
|
HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/FoxTube/Inbox?toast=true&publishedAfter={storage.Values["Inbox.lastCheck"]}&lang={Settings.Language}&appVersion={Metrics.CurrentVersion}");
|
||||||
storage.Values["Inbox.lastCheck"] = DateTime.UtcNow.Ticks;
|
storage.Values["Inbox.lastCheck"] = DateTime.UtcNow.Ticks;
|
||||||
|
|
||||||
if (response.StatusCode == HttpStatusCode.NoContent)
|
if (response.StatusCode == HttpStatusCode.NoContent)
|
||||||
@@ -67,7 +67,7 @@ namespace FoxTube.Services
|
|||||||
// TODO: Add backend
|
// TODO: Add backend
|
||||||
Settings.LastReviewedVersion = Metrics.CurrentVersion;
|
Settings.LastReviewedVersion = Metrics.CurrentVersion;
|
||||||
|
|
||||||
HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/API/FoxTube/Changelogs?toast=true&lang={Settings.Language}&version={Metrics.CurrentVersion}");
|
HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/API/FoxTube/Changelog?lang={Settings.Language}&version={Metrics.CurrentVersion}");
|
||||||
|
|
||||||
if (response.StatusCode == HttpStatusCode.NoContent)
|
if (response.StatusCode == HttpStatusCode.NoContent)
|
||||||
return;
|
return;
|
||||||
|
|||||||
+115
-34
@@ -1,11 +1,9 @@
|
|||||||
using Google.Apis.Auth.OAuth2;
|
using Google.Apis.Auth.OAuth2;
|
||||||
using Google.Apis.Oauth2.v2;
|
using Google.Apis.Oauth2.v2;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Windows.Security.Authentication.Web;
|
using Windows.Security.Authentication.Web;
|
||||||
using YouTube.Authorization;
|
using YouTube.Authorization;
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using Windows.Security.Credentials;
|
using Windows.Security.Credentials;
|
||||||
using FoxTube.Models;
|
using FoxTube.Models;
|
||||||
using YouTube;
|
using YouTube;
|
||||||
@@ -13,58 +11,81 @@ using System.Threading;
|
|||||||
using Google.Apis.YouTube.v3;
|
using Google.Apis.YouTube.v3;
|
||||||
using Google.Apis.YouTube.v3.Data;
|
using Google.Apis.YouTube.v3.Data;
|
||||||
using Windows.UI.Xaml.Controls;
|
using Windows.UI.Xaml.Controls;
|
||||||
using Windows.UI.Popups;
|
|
||||||
using FoxTube.Utils;
|
using FoxTube.Utils;
|
||||||
|
using YoutubeExplode;
|
||||||
|
using System.Linq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Windows.Storage;
|
||||||
|
using Google.Apis.Oauth2.v2.Data;
|
||||||
|
|
||||||
namespace FoxTube
|
namespace FoxTube
|
||||||
{
|
{
|
||||||
public static class UserManagement
|
public static class UserManagement
|
||||||
{
|
{
|
||||||
static ExtendedYouTubeService _defaultService = new ExtendedYouTubeService(new Google.Apis.Services.BaseClientService.Initializer
|
public const int MaxUsersCount = 2;
|
||||||
|
|
||||||
|
#region Private members
|
||||||
|
private static readonly ApplicationDataContainer storage = ApplicationData.Current.LocalSettings;
|
||||||
|
|
||||||
|
private static ExtendedYouTubeService _defaultService = new ExtendedYouTubeService(new Google.Apis.Services.BaseClientService.Initializer
|
||||||
{
|
{
|
||||||
ApplicationName = "FoxTube",
|
ApplicationName = "FoxTube",
|
||||||
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0"
|
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0",
|
||||||
//ApiKey = "AIzaSyD7tpbuvmYDv9h4udo9L_g3r0sLPFAnN00"
|
//ApiKey = "AIzaSyD7tpbuvmYDv9h4udo9L_g3r0sLPFAnN00"
|
||||||
});
|
});
|
||||||
|
private static YoutubeClient _defaultYteClient = new YoutubeClient();
|
||||||
|
|
||||||
static string[] Scopes { get; } = new string[]
|
private static string[] Scopes { get; } = new string[]
|
||||||
{
|
{
|
||||||
Oauth2Service.Scope.UserinfoProfile,
|
Oauth2Service.Scope.UserinfoProfile,
|
||||||
Oauth2Service.Scope.UserinfoEmail,
|
Oauth2Service.Scope.UserinfoEmail,
|
||||||
YouTubeService.Scope.YoutubeForceSsl
|
YouTubeService.Scope.YoutubeForceSsl
|
||||||
};
|
};
|
||||||
|
|
||||||
static ClientSecrets ClientSecrets { get; } = new ClientSecrets
|
private static ClientSecrets[] ClientSecrets { get; } = new ClientSecrets[MaxUsersCount]
|
||||||
|
{
|
||||||
|
new ClientSecrets
|
||||||
{
|
{
|
||||||
ClientId = "349735264870-2ekqlm0a4mkg3mmrfcv90s3qp3o15dq0.apps.googleusercontent.com",
|
ClientId = "349735264870-2ekqlm0a4mkg3mmrfcv90s3qp3o15dq0.apps.googleusercontent.com",
|
||||||
ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_"
|
ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_"
|
||||||
//ClientId = "1096685398208-u95rcpkqb4e1ijfmb8jdq3jsg37l8igv.apps.googleusercontent.com",
|
},
|
||||||
//ClientSecret = "IU5bbdjwvmx8ttJoXQ7e6JWd"
|
new ClientSecrets // DISABLED
|
||||||
|
{
|
||||||
|
ClientId = "1096685398208-u95rcpkqb4e1ijfmb8jdq3jsg37l8igv.apps.googleusercontent.com",
|
||||||
|
ClientSecret = "IU5bbdjwvmx8ttJoXQ7e6JWd"
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public static Userinfoplus[] Users { get; private set; } = new Userinfoplus[MaxUsersCount];
|
||||||
public static User CurrentUser { get; set; }
|
public static User CurrentUser { get; set; }
|
||||||
public static bool Authorized => CurrentUser != null;
|
public static bool Authorized => CurrentUser != null;
|
||||||
public static ExtendedYouTubeService Service => CurrentUser?.Service ?? _defaultService;
|
public static ExtendedYouTubeService Service => CurrentUser?.Service ?? _defaultService;
|
||||||
|
public static YoutubeClient YoutubeClient => CurrentUser?.Client ?? _defaultYteClient;
|
||||||
|
|
||||||
public static event EventHandler<bool> UserStateUpdated;
|
public static event EventHandler<bool> UserStateUpdated;
|
||||||
public static event EventHandler<Subscription> SubscriptionsChanged;
|
public static event EventHandler<Subscription> SubscriptionsChanged;
|
||||||
|
|
||||||
public static async Task<bool> AddUser()
|
public static async Task<bool> AddUser()
|
||||||
{
|
{
|
||||||
Uri requestString = AuthorizationHelpers.FormQueryString(ClientSecrets, Scopes);
|
int queueIndex = Users.ToList().FindIndex(i => i == null);
|
||||||
|
|
||||||
|
if (queueIndex < 0)
|
||||||
|
throw new StackOverflowException("The maximum accounts limit is reached");
|
||||||
|
|
||||||
|
ClientSecrets secrets = ClientSecrets[queueIndex];
|
||||||
|
|
||||||
|
Uri requestString = AuthorizationHelpers.FormQueryString(secrets, Scopes);
|
||||||
|
|
||||||
WebAuthenticationResult result = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.UseTitle, requestString, AuthorizationHelpers.Endpoint);
|
WebAuthenticationResult result = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.UseTitle, requestString, AuthorizationHelpers.Endpoint);
|
||||||
switch (result.ResponseStatus)
|
switch (result.ResponseStatus)
|
||||||
{
|
{
|
||||||
case WebAuthenticationStatus.Success:
|
case WebAuthenticationStatus.Success:
|
||||||
string successCode = new Regex(@"(?<=code=)(.*?)(?=&)").Match(result.ResponseData).Value;
|
string successCode = AuthorizationHelpers.ParseSuccessCode(result.ResponseData);
|
||||||
|
|
||||||
UserCredential credential = await AuthorizationHelpers.ExchangeToken(ClientSecrets, successCode);
|
YouTube.Authorization.UserCredential credential = await AuthorizationHelpers.ExchangeToken(secrets, successCode);
|
||||||
CurrentUser = new User(credential);
|
|
||||||
|
|
||||||
PasswordVault passwordVault = new PasswordVault();
|
await LoadUser(credential, queueIndex);
|
||||||
passwordVault.Add(new PasswordCredential("foxtube", CurrentUser.UserInfo.Id, credential.Token.RefreshToken));
|
|
||||||
UserStateUpdated?.Invoke(null, true);
|
|
||||||
return true;
|
return true;
|
||||||
case WebAuthenticationStatus.UserCancel:
|
case WebAuthenticationStatus.UserCancel:
|
||||||
break;
|
break;
|
||||||
@@ -81,44 +102,104 @@ namespace FoxTube
|
|||||||
("Error details", result.ResponseErrorDetail.ToString()));
|
("Error details", result.ResponseErrorDetail.ToString()));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task Initialize()
|
public static async Task Initialize()
|
||||||
{
|
{
|
||||||
PasswordVault passwordVault = new PasswordVault();
|
Users = JsonConvert.DeserializeObject<Userinfoplus[]>(storage.Values["UserManagement.Users"] as string);
|
||||||
IReadOnlyList<PasswordCredential> credentials;
|
int? lastUserIndex = storage.Values["UserManagement.LastUser"] as int?;
|
||||||
credentials = passwordVault.RetrieveAll();
|
|
||||||
|
|
||||||
if (credentials.Count == 0)
|
if (lastUserIndex.HasValue && Users[lastUserIndex.Value] != null ||
|
||||||
return;
|
(lastUserIndex = Users.ToList().FindIndex(i => i != null)) > -1)
|
||||||
|
await SwitchUser(lastUserIndex.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<bool> Logout()
|
||||||
|
{
|
||||||
|
if (CurrentUser?.UserInfo?.Id == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
credentials[0].RetrievePassword();
|
string userId = CurrentUser.UserInfo.Id;
|
||||||
UserCredential credential = await AuthorizationHelpers.RestoreUser(ClientSecrets, credentials[0].Password);
|
|
||||||
credentials[0].Password = credential.Token.RefreshToken;
|
PasswordVault passwordVault = new PasswordVault();
|
||||||
CurrentUser = new User(credential);
|
PasswordCredential credential = passwordVault.Retrieve("foxtube", userId);
|
||||||
|
passwordVault.Remove(credential);
|
||||||
|
|
||||||
|
await CurrentUser.Credential.RevokeTokenAsync(CancellationToken.None);
|
||||||
|
|
||||||
|
CurrentUser = null;
|
||||||
|
Users[Users.ToList().FindIndex(i => i.Id == userId)] = null;
|
||||||
|
|
||||||
|
storage.Values["UserManagement.Users"] = JsonConvert.SerializeObject(Users);
|
||||||
|
storage.Values["UserManagement.LastUser"] = null;
|
||||||
|
|
||||||
|
if (Users.Any(i => i != null))
|
||||||
|
await SwitchUser(Users.ToList().FindIndex(i => i != null));
|
||||||
|
else
|
||||||
|
UserStateUpdated?.Invoke(Users, false);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
await new MessageDialog("It may be a bug or temporary server issues. Please, try again later", "Something went wrong...").ShowAsync();
|
Metrics.SendReport(new Exception("Failed to logout", e));
|
||||||
|
|
||||||
Metrics.SendReport(new Exception("Refresh token exchange failed", e));
|
await new ContentDialog
|
||||||
|
{
|
||||||
|
Title = "Something went wrong...",
|
||||||
|
Content = "It may be a bug or temporary server issues. Please, try again later"
|
||||||
|
}.ShowAsync();
|
||||||
|
|
||||||
foreach (PasswordCredential i in credentials)
|
return false;
|
||||||
passwordVault.Remove(i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task Logout()
|
public static async Task SwitchUser(int userIndex)
|
||||||
|
{
|
||||||
|
Userinfoplus userInfo = Users[userIndex];
|
||||||
|
|
||||||
|
PasswordVault valut = new PasswordVault();
|
||||||
|
PasswordCredential vaultCredential = valut.Retrieve("foxtube", userInfo.Id);
|
||||||
|
if (vaultCredential == null)
|
||||||
|
throw new NullReferenceException("No user found to switch on");
|
||||||
|
|
||||||
|
vaultCredential.RetrievePassword();
|
||||||
|
string token = vaultCredential.Password;
|
||||||
|
YouTube.Authorization.UserCredential credential = await AuthorizationHelpers.RestoreUser(ClientSecrets[userIndex], token);
|
||||||
|
|
||||||
|
await LoadUser(credential, userIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task LoadUser(YouTube.Authorization.UserCredential credential, int userIndex)
|
||||||
|
{
|
||||||
|
CurrentUser = await User.GetUser(credential);
|
||||||
|
Users[userIndex] = CurrentUser.UserInfo;
|
||||||
|
|
||||||
|
storage.Values["UserManagement.Users"] = JsonConvert.SerializeObject(Users);
|
||||||
|
storage.Values["UserManagement.LastUser"] = userIndex;
|
||||||
|
|
||||||
|
credential.RefreshTokenUpdated += (s, e) => UpdateToken(CurrentUser.UserInfo.Id, credential.Token.RefreshToken);
|
||||||
|
UpdateToken(CurrentUser.UserInfo.Id, credential.Token.RefreshToken);
|
||||||
|
|
||||||
|
UserStateUpdated?.Invoke(Users, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void UpdateToken(string id, string refreshToken)
|
||||||
{
|
{
|
||||||
PasswordVault passwordVault = new PasswordVault();
|
PasswordVault passwordVault = new PasswordVault();
|
||||||
PasswordCredential credential = passwordVault.Retrieve("foxtube", CurrentUser.UserInfo.Id);
|
PasswordCredential vaultCredential = passwordVault.Retrieve("foxtube", id);
|
||||||
passwordVault.Remove(credential);
|
|
||||||
await CurrentUser.Credential.RevokeTokenAsync(CancellationToken.None);
|
|
||||||
|
|
||||||
UserStateUpdated?.Invoke(null, false);
|
if (vaultCredential == null)
|
||||||
|
{
|
||||||
|
vaultCredential = new PasswordCredential("foxtube", CurrentUser.UserInfo.Id, refreshToken);
|
||||||
|
passwordVault.Add(vaultCredential);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
vaultCredential.Password = refreshToken;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user