diff --git a/FoxTube.Core/FoxTube.Core.csproj b/FoxTube.Core/FoxTube.Core.csproj index f869b67..57f7302 100644 --- a/FoxTube.Core/FoxTube.Core.csproj +++ b/FoxTube.Core/FoxTube.Core.csproj @@ -136,6 +136,7 @@ + @@ -144,6 +145,7 @@ + @@ -151,6 +153,15 @@ 0.13.0 + + 1.42.0 + + + 1.42.0.1602 + + + 1.42.0.1758 + 2.6.1 @@ -175,6 +186,11 @@ Visual C++ 2015 Runtime for Universal Windows Platform Apps + + + ..\Src\YouTube.API.dll + + 14.0 diff --git a/FoxTube.Core/Helpers/UserControl.cs b/FoxTube.Core/Helpers/UserControl.cs new file mode 100644 index 0000000..1eecfee --- /dev/null +++ b/FoxTube.Core/Helpers/UserControl.cs @@ -0,0 +1,73 @@ +using Google.Apis.Auth.OAuth2; +using Google.Apis.Oauth2.v2; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Windows.Security.Authentication.Web; +using YouTube.Authorization; +using YouTube; +using System.Text.RegularExpressions; +using Windows.Security.Credentials; +using FoxTube.Core.Models; +using System.Threading; +using Google.Apis.YouTube.v3; + +namespace FoxTube +{ + public static class UserControl + { + static string[] Scopes { get; } = new string[] + { + Oauth2Service.Scope.UserinfoProfile, + Oauth2Service.Scope.UserinfoEmail, + YouTubeService.Scope.YoutubeForceSsl + }; + + static ClientSecrets ClientSecrets { get; } = new ClientSecrets + { + ClientId = "1096685398208-u95rcpkqb4e1ijfmb8jdq3jsg37l8igv.apps.googleusercontent.com", + ClientSecret = "IU5bbdjwvmx8ttJoXQ7e6JWd" + }; + + public static User CurrentUser { get; set; } + public static bool Authorized => CurrentUser != null; + + public static async Task AddUser() + { + Uri callbackUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri(); + Uri requestString = AuthorizationHelpers.FormQueryString(ClientSecrets, callbackUri, Scopes); + + WebAuthenticationResult result = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.UseTitle, requestString, AuthorizationHelpers.Endpoint); + switch(result.ResponseStatus) + { + case WebAuthenticationStatus.Success: + UserCredential credential = await AuthorizationHelpers.ExchangeToken(ClientSecrets, new Regex(@"(?<=code=).?\w+").Match(result.ResponseData).Value); // TODO: Add credential assignment + CurrentUser = new User(credential); + return true; + case WebAuthenticationStatus.UserCancel: + break; + case WebAuthenticationStatus.ErrorHttp: + Metrics.AddEvent("Authorization failed (HTTP Error)", ("Response data", result.ResponseData), ("Error details", result.ResponseErrorDetail.ToString())); + break; + } + return false; + } + + public static async Task Initialize() + { + PasswordVault passwordVault = new PasswordVault(); + List credentials = passwordVault.FindAllByResource("foxtube").ToList(); + + if (credentials.Count == 0) + return; + + credentials[0].RetrievePassword(); + UserCredential credential = await AuthorizationHelpers.RestoreUser(ClientSecrets, credentials[0].Password); + CurrentUser = new User(credential); + } + + public static async Task Logout() => + await CurrentUser.Credential.RevokeTokenAsync(CancellationToken.None); + } +} diff --git a/FoxTube.Core/Models/User.cs b/FoxTube.Core/Models/User.cs new file mode 100644 index 0000000..fc117d9 --- /dev/null +++ b/FoxTube.Core/Models/User.cs @@ -0,0 +1,24 @@ +using Google.Apis.Auth.OAuth2; +using Google.Apis.YouTube.v3.Data; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FoxTube.Core.Models +{ + public class User + { + public string Name { get; set; } + public string Email { get; set; } + internal string RefreshToken { get; set; } + public UserCredential Credential { get; set; } + public Channel Channel { get; set; } + + public User(UserCredential credential) + { + + } + } +} diff --git a/FoxTube/App.xaml.cs b/FoxTube/App.xaml.cs index 87ddf64..57cafe8 100644 --- a/FoxTube/App.xaml.cs +++ b/FoxTube/App.xaml.cs @@ -2,9 +2,7 @@ using FoxTube.Core.Helpers; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; -using Windows.UI; using Windows.UI.Popups; -using Windows.UI.ViewManagement; using Windows.UI.Xaml; namespace FoxTube @@ -21,6 +19,7 @@ namespace FoxTube protected override async void OnLaunched(LaunchActivatedEventArgs e) { + await UserControl.Initialize(); await StoreInterop.UpdateStoreState(); if (Settings.LastReviewedVersion != Metrics.CurrentVersion) Inbox.PushChangelog(); diff --git a/FoxTube/Controls/AccountManager.xaml b/FoxTube/Controls/AccountManager.xaml index 8e7dcbf..a194d6f 100644 --- a/FoxTube/Controls/AccountManager.xaml +++ b/FoxTube/Controls/AccountManager.xaml @@ -13,27 +13,27 @@ - + - + - - + - + - - + + diff --git a/FoxTube/Controls/AccountManager.xaml.cs b/FoxTube/Controls/AccountManager.xaml.cs index d6a9c1e..427c09c 100644 --- a/FoxTube/Controls/AccountManager.xaml.cs +++ b/FoxTube/Controls/AccountManager.xaml.cs @@ -1,27 +1,27 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Navigation; - -// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Media.Imaging; namespace FoxTube.Controls { public sealed partial class AccountManager : AppBarButton { - public AccountManager() - { + public AccountManager() => InitializeComponent(); + + async void Flyout_Opening(object sender, object e) + { + if (UserControl.Authorized) + return; + + (sender as Flyout).Hide(); + + if(await UserControl.AddUser()) + { + banner.Source = new BitmapImage(UserControl.CurrentUser.Channel.BrandingSettings.Image.BannerMobileLowImageUrl.ToUri()) { DecodePixelWidth = 300, DecodePixelHeight = 60 }; + avatar.ProfilePicture = new BitmapImage(UserControl.CurrentUser.Channel.Snippet.Thumbnails.Default__.Url.ToUri()) { DecodePixelHeight = 40, DecodePixelWidth = 40 }; + name.Text = UserControl.CurrentUser.Name; + email.Text = UserControl.CurrentUser.Email; + } } } } diff --git a/Src/YouTube.API.dll b/Src/YouTube.API.dll new file mode 100644 index 0000000..bd8c398 Binary files /dev/null and b/Src/YouTube.API.dll differ