Archived
1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FoxTube/FoxTube/Classes/UserManagement.cs
T
2019-08-25 23:57:26 +03:00

174 lines
6.8 KiB
C#

using FoxTube.Classes;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Oauth2.v2.Data;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
using Microsoft.AppCenter.Analytics;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Security.Credentials;
using Windows.Storage;
using YoutubeExplode;
namespace FoxTube
{
public static class UserManagement
{
static ApplicationDataContainer storage = ApplicationData.Current.LocalSettings;
public static event AuthorizationChangedEventHandler AuthorizationStateChanged;
public static event SubscriptionChangedEventHandler SubscriptionsChanged;
static string[] Secrets => new string[5]
{
"1096685398208-bktbnoe59bt69nhnrrb5j0tpld58orsv.apps.googleusercontent.com",
"1096685398208-u4a2pgpcn27c2kb3ud2eck1oh2ot68vs.apps.googleusercontent.com",
"1096685398208-a65ebfpqnhl7u3iipfmfe5cif6j07db3.apps.googleusercontent.com",
"1096685398208-in0gco58ckrumgjuo68st55fvb0ntllj.apps.googleusercontent.com",
"1096685398208-7gd029f6tku4sc756v2338g1f0fu4k2k.apps.googleusercontent.com"
};
static YouTubeService NoAuthService { get; } = new YouTubeService(new BaseClientService.Initializer
{
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0",
ApplicationName = "FoxTube"
});
static YoutubeClient NoAuthClient { get; } = new YoutubeClient();
public static YoutubeClient YoutubeClient => Current?.YoutubeClient ?? NoAuthClient;
public static bool IsAuthorized => Current != null;
public static User Current { get; set; }
public static Userinfoplus[] UserInfos { get; private set; } = new Userinfoplus[5];
static string[] tokens = new string[5];
public static async void Initialize()
{
if(storage.Values["users"] != null)
UserInfos = JsonConvert.DeserializeObject<Userinfoplus[]>(storage.Values["users"] as string);
PasswordVault vault = new PasswordVault();
foreach (Userinfoplus info in UserInfos)
{
if (info == null)
continue;
PasswordCredential credential = vault.Retrieve("FoxTube", info.Id);
credential.RetrievePassword();
tokens[UserInfos.ToList().IndexOf(info)] = credential.Password;
}
if(Settings.SelectedUser >= 0)
{
try { Current = new User(await Authenticator.RefreshToken(Secrets[Settings.SelectedUser], tokens[Settings.SelectedUser])); }
catch { Current = new User(await Authenticator.Authorize(Secrets[Settings.SelectedUser])); }
await Current.Initialize();
Current.SubscriptionsChanged += SubscriptionsChanged;
UserInfos[Settings.SelectedUser] = Current.UserInfo;
}
AuthorizationStateChanged?.Invoke(Settings.SelectedUser > -1);
}
public static async Task AddItemToWL(string id)
{
}
public static async void ChangeAccount(Userinfoplus newUser)
{
Current.Dispose();
Current.SubscriptionsChanged -= SubscriptionsChanged;
Current = null;
Settings.SelectedUser = UserInfos.ToList().IndexOf(newUser);
try { Current = new User(await Authenticator.RefreshToken(Secrets[Settings.SelectedUser], tokens[Settings.SelectedUser])); }
catch { Current = new User(await Authenticator.Authorize(Secrets[Settings.SelectedUser])); }
await Current.Initialize();
Current.SubscriptionsChanged += SubscriptionsChanged;
UserInfos[Settings.SelectedUser] = Current.UserInfo;
AuthorizationStateChanged?.Invoke(true);
}
public static async void CreateNew()
{
Analytics.TrackEvent("Initialized authorization sequence");
try
{
for (int k = 0; k < 5; k++)
if (tokens[k] == null)
{
TokenResponse response = await Authenticator.Authorize(Secrets[k]);
tokens[k] = response.RefreshToken;
Current?.Dispose();
if (Current != null)
Current.SubscriptionsChanged -= SubscriptionsChanged;
Current = new User(response);
await Current.Initialize();
Current.SubscriptionsChanged += SubscriptionsChanged;
UserInfos[k] = Current.UserInfo;
storage.Values["users"] = JsonConvert.SerializeObject(UserInfos);
PasswordVault vault = new PasswordVault();
vault.Add(new PasswordCredential("FoxTube", Current.UserInfo.Id, response.RefreshToken));
Settings.SelectedUser = k;
AuthorizationStateChanged?.Invoke(true);
break;
}
}
catch { }
}
public static async void Logout()
{
Current.Dispose();
Current.SubscriptionsChanged -= SubscriptionsChanged;
await Current.Deauthenticate();
PasswordVault vault = new PasswordVault();
vault.Remove(vault.Retrieve("FoxTube", Current.UserInfo.Id));
UserInfos[Settings.SelectedUser] = null;
tokens[Settings.SelectedUser] = null;
storage.Values["users"] = JsonConvert.SerializeObject(UserInfos);
Current = null;
Settings.SelectedUser = -1;
for (int i = 0; i < 5; i++)
if (UserInfos[i] != null)
{
Settings.SelectedUser = i;
break;
}
if (Settings.SelectedUser >= 0)
{
try { Current = new User(await Authenticator.RefreshToken(Secrets[Settings.SelectedUser], tokens[Settings.SelectedUser])); }
catch { Current = new User(await Authenticator.Authorize(Secrets[Settings.SelectedUser])); }
await Current.Initialize();
Current.SubscriptionsChanged += SubscriptionsChanged;
UserInfos[Settings.SelectedUser] = Current.UserInfo;
}
AuthorizationStateChanged?.Invoke(Settings.SelectedUser > -1);
}
public static async Task<TokenResponse> RefreshToken() =>
await Authenticator.RefreshToken(Secrets[Settings.SelectedUser], tokens[Settings.SelectedUser]);
}
}