Archived
1
0

Added user authorization (not tested)

This commit is contained in:
Michael Gordeev
2019-12-04 16:19:57 +03:00
parent 3b08b18d4f
commit 903c0c563d
7 changed files with 139 additions and 27 deletions
+16
View File
@@ -136,6 +136,7 @@
<Compile Include="Helpers\Metrics.cs" /> <Compile Include="Helpers\Metrics.cs" />
<Compile Include="Helpers\Settings.cs" /> <Compile Include="Helpers\Settings.cs" />
<Compile Include="Helpers\StoreInterop.cs" /> <Compile Include="Helpers\StoreInterop.cs" />
<Compile Include="Helpers\UserControl.cs" />
<Compile Include="Helpers\Utils.cs" /> <Compile Include="Helpers\Utils.cs" />
<Compile Include="Models\Inbox\Changelog.cs" /> <Compile Include="Models\Inbox\Changelog.cs" />
<Compile Include="Models\Inbox\DeveloperMessage.cs" /> <Compile Include="Models\Inbox\DeveloperMessage.cs" />
@@ -144,6 +145,7 @@
<Compile Include="Models\Notifications.cs" /> <Compile Include="Models\Notifications.cs" />
<Compile Include="Models\PageView.cs" /> <Compile Include="Models\PageView.cs" />
<Compile Include="Models\SearchSuggestion.cs" /> <Compile Include="Models\SearchSuggestion.cs" />
<Compile Include="Models\User.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Properties\FoxTube.Core.rd.xml" /> <EmbeddedResource Include="Properties\FoxTube.Core.rd.xml" />
</ItemGroup> </ItemGroup>
@@ -151,6 +153,15 @@
<PackageReference Include="AngleSharp"> <PackageReference Include="AngleSharp">
<Version>0.13.0</Version> <Version>0.13.0</Version>
</PackageReference> </PackageReference>
<PackageReference Include="Google.Apis.Auth">
<Version>1.42.0</Version>
</PackageReference>
<PackageReference Include="Google.Apis.Oauth2.v2">
<Version>1.42.0.1602</Version>
</PackageReference>
<PackageReference Include="Google.Apis.YouTube.v3">
<Version>1.42.0.1758</Version>
</PackageReference>
<PackageReference Include="Microsoft.AppCenter.Analytics"> <PackageReference Include="Microsoft.AppCenter.Analytics">
<Version>2.6.1</Version> <Version>2.6.1</Version>
</PackageReference> </PackageReference>
@@ -175,6 +186,11 @@
<Name>Visual C++ 2015 Runtime for Universal Windows Platform Apps</Name> <Name>Visual C++ 2015 Runtime for Universal Windows Platform Apps</Name>
</SDKReference> </SDKReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="YouTube.API">
<HintPath>..\Src\YouTube.API.dll</HintPath>
</Reference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' "> <PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion> <VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup> </PropertyGroup>
+73
View File
@@ -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<bool> 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<PasswordCredential> 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);
}
}
+24
View File
@@ -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)
{
}
}
}
+1 -2
View File
@@ -2,9 +2,7 @@
using FoxTube.Core.Helpers; using FoxTube.Core.Helpers;
using Windows.ApplicationModel; using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.Activation;
using Windows.UI;
using Windows.UI.Popups; using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml; using Windows.UI.Xaml;
namespace FoxTube namespace FoxTube
@@ -21,6 +19,7 @@ namespace FoxTube
protected override async void OnLaunched(LaunchActivatedEventArgs e) protected override async void OnLaunched(LaunchActivatedEventArgs e)
{ {
await UserControl.Initialize();
await StoreInterop.UpdateStoreState(); await StoreInterop.UpdateStoreState();
if (Settings.LastReviewedVersion != Metrics.CurrentVersion) if (Settings.LastReviewedVersion != Metrics.CurrentVersion)
Inbox.PushChangelog(); Inbox.PushChangelog();
+6 -6
View File
@@ -15,25 +15,25 @@
</AppBarButton.Icon> </AppBarButton.Icon>
<AppBarButton.Flyout> <AppBarButton.Flyout>
<Flyout> <Flyout Opening="Flyout_Opening">
<StackPanel Margin="-12" Width="300"> <StackPanel Margin="-12" Width="300">
<Grid Height="60"> <Grid Height="60">
<controls:ImageEx Stretch="UniformToFill" <controls:ImageEx x:Name="banner" Stretch="UniformToFill"
PlaceholderSource="/Assets/DefaultChannelBanner.png" PlaceholderSource="/Assets/DefaultChannelBanner.png"
PlaceholderStretch="UniformToFill"/> PlaceholderStretch="UniformToFill"/>
<Grid Padding="10" ColumnSpacing="20"> <Grid Padding="10" ColumnSpacing="20">
<Grid.Background> <Grid.Background>
<SolidColorBrush Color="Black" Opacity=".7"/> <AcrylicBrush TintColor="Black" TintOpacity=".2"/>
</Grid.Background> </Grid.Background>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>
<ColumnDefinition/> <ColumnDefinition/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<PersonPicture Height="40"/> <PersonPicture Height="40" x:Name="avatar"/>
<StackPanel Grid.Column="1"> <StackPanel Grid.Column="1">
<TextBlock Text="Michael 'XFox' Gordeev"/> <TextBlock x:Name="name" Text="█████████"/>
<TextBlock Text="michael.xfox@outlook.com"/> <TextBlock x:Name="email" Text="██████████████████"/>
</StackPanel> </StackPanel>
</Grid> </Grid>
</Grid> </Grid>
+18 -18
View File
@@ -1,27 +1,27 @@
using System; using Windows.UI.Xaml.Controls;
using System.Collections.Generic; using Windows.UI.Xaml.Media.Imaging;
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
namespace FoxTube.Controls namespace FoxTube.Controls
{ {
public sealed partial class AccountManager : AppBarButton public sealed partial class AccountManager : AppBarButton
{ {
public AccountManager() public AccountManager() =>
{
InitializeComponent(); 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;
}
} }
} }
} }
Binary file not shown.