Archived
1
0

Added authorization mechanism

This commit is contained in:
Michael Gordeev
2019-12-05 00:58:09 +03:00
parent 903c0c563d
commit e2b2a7cc8d
9 changed files with 148 additions and 40 deletions
@@ -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);
}
}
}