Added authorization mechanism
This commit is contained in:
@@ -136,7 +136,7 @@
|
||||
<Compile Include="Helpers\Metrics.cs" />
|
||||
<Compile Include="Helpers\Settings.cs" />
|
||||
<Compile Include="Helpers\StoreInterop.cs" />
|
||||
<Compile Include="Helpers\UserControl.cs" />
|
||||
<Compile Include="Helpers\UsersControl.cs" />
|
||||
<Compile Include="Helpers\Utils.cs" />
|
||||
<Compile Include="Models\Inbox\Changelog.cs" />
|
||||
<Compile Include="Models\Inbox\DeveloperMessage.cs" />
|
||||
@@ -153,6 +153,9 @@
|
||||
<PackageReference Include="AngleSharp">
|
||||
<Version>0.13.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="ExtendedYouTubeAPI">
|
||||
<Version>1.0.1</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Google.Apis.Auth">
|
||||
<Version>1.42.0</Version>
|
||||
</PackageReference>
|
||||
@@ -163,10 +166,10 @@
|
||||
<Version>1.42.0.1758</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.AppCenter.Analytics">
|
||||
<Version>2.6.1</Version>
|
||||
<Version>2.6.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.AppCenter.Crashes">
|
||||
<Version>2.6.1</Version>
|
||||
<Version>2.6.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
|
||||
<Version>6.2.9</Version>
|
||||
@@ -186,11 +189,6 @@
|
||||
<Name>Visual C++ 2015 Runtime for Universal Windows Platform Apps</Name>
|
||||
</SDKReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="YouTube.API">
|
||||
<HintPath>..\Src\YouTube.API.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' < '14.0' ">
|
||||
<VisualStudioVersion>14.0</VisualStudioVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -2,21 +2,26 @@
|
||||
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 YouTube;
|
||||
using System.Threading;
|
||||
using Google.Apis.YouTube.v3;
|
||||
|
||||
namespace FoxTube
|
||||
{
|
||||
public static class UserControl
|
||||
public static class UsersControl
|
||||
{
|
||||
static ExtendedYouTubeService _defaultService = new ExtendedYouTubeService(new Google.Apis.Services.BaseClientService.Initializer
|
||||
{
|
||||
ApplicationName = "FoxTube",
|
||||
ApiKey = "AIzaSyD7tpbuvmYDv9h4udo9L_g3r0sLPFAnN00"
|
||||
});
|
||||
|
||||
static string[] Scopes { get; } = new string[]
|
||||
{
|
||||
Oauth2Service.Scope.UserinfoProfile,
|
||||
@@ -32,6 +37,7 @@ namespace FoxTube
|
||||
|
||||
public static User CurrentUser { get; set; }
|
||||
public static bool Authorized => CurrentUser != null;
|
||||
public static ExtendedYouTubeService Service => CurrentUser?.Service ?? _defaultService;
|
||||
|
||||
public static async Task<bool> AddUser()
|
||||
{
|
||||
@@ -42,8 +48,10 @@ namespace FoxTube
|
||||
switch(result.ResponseStatus)
|
||||
{
|
||||
case WebAuthenticationStatus.Success:
|
||||
UserCredential credential = await AuthorizationHelpers.ExchangeToken(ClientSecrets, new Regex(@"(?<=code=).?\w+").Match(result.ResponseData).Value); // TODO: Add credential assignment
|
||||
UserCredential credential = await AuthorizationHelpers.ExchangeToken(ClientSecrets, new Regex(@"(?<=code=).?\w+").Match(result.ResponseData).Value);
|
||||
CurrentUser = new User(credential);
|
||||
PasswordVault passwordVault = new PasswordVault();
|
||||
passwordVault.Add(new PasswordCredential("foxtube", CurrentUser.UserInfo.Id, credential.Token.RefreshToken));
|
||||
return true;
|
||||
case WebAuthenticationStatus.UserCancel:
|
||||
break;
|
||||
@@ -57,7 +65,8 @@ namespace FoxTube
|
||||
public static async Task Initialize()
|
||||
{
|
||||
PasswordVault passwordVault = new PasswordVault();
|
||||
List<PasswordCredential> credentials = passwordVault.FindAllByResource("foxtube").ToList();
|
||||
IReadOnlyList<PasswordCredential> credentials;
|
||||
credentials = passwordVault.RetrieveAll();
|
||||
|
||||
if (credentials.Count == 0)
|
||||
return;
|
||||
@@ -67,7 +76,12 @@ namespace FoxTube
|
||||
CurrentUser = new User(credential);
|
||||
}
|
||||
|
||||
public static async Task Logout() =>
|
||||
public static async Task Logout()
|
||||
{
|
||||
PasswordVault passwordVault = new PasswordVault();
|
||||
PasswordCredential credential = passwordVault.Retrieve("foxtube", CurrentUser.UserInfo.Id);
|
||||
passwordVault.Remove(credential);
|
||||
await CurrentUser.Credential.RevokeTokenAsync(CancellationToken.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,58 @@
|
||||
using Google.Apis.Auth.OAuth2;
|
||||
using Google.Apis.Oauth2.v2;
|
||||
using Google.Apis.Oauth2.v2.Data;
|
||||
using Google.Apis.Services;
|
||||
using Google.Apis.YouTube.v3;
|
||||
using Google.Apis.YouTube.v3.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using YouTube;
|
||||
|
||||
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 Userinfoplus UserInfo { get; }
|
||||
public UserCredential Credential { get; }
|
||||
public Channel Channel { get; private set; }
|
||||
public List<Subscription> Subscriptions { get; } = new List<Subscription>();
|
||||
public ExtendedYouTubeService Service { get; }
|
||||
|
||||
public User(UserCredential credential)
|
||||
{
|
||||
Credential = credential;
|
||||
BaseClientService.Initializer initializer = new BaseClientService.Initializer
|
||||
{
|
||||
ApplicationName = "FoxTube",
|
||||
HttpClientInitializer = Credential
|
||||
};
|
||||
|
||||
Service = new ExtendedYouTubeService(initializer);
|
||||
|
||||
UserInfo = new Oauth2Service(initializer).Userinfo.Get().Execute();
|
||||
|
||||
// TODO: Retrieve history and WL
|
||||
|
||||
SubscriptionsResource.ListRequest subRequest = Service.Subscriptions.List("snippet");
|
||||
subRequest.Mine = true;
|
||||
subRequest.MaxResults = 50;
|
||||
subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance;
|
||||
SubscriptionListResponse subResponse;
|
||||
string nextToken = null;
|
||||
Subscriptions.Clear();
|
||||
|
||||
do
|
||||
{
|
||||
subRequest.PageToken = nextToken;
|
||||
subResponse = subRequest.Execute();
|
||||
foreach (Subscription s in subResponse.Items)
|
||||
Subscriptions.Add(s);
|
||||
nextToken = subResponse.NextPageToken;
|
||||
|
||||
} while (!string.IsNullOrWhiteSpace(nextToken));
|
||||
|
||||
var request = Service.Channels.List("snippet,contentDetails");
|
||||
request.Mine = true;
|
||||
Channel = request.Execute().Items[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user