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.Core/Helpers/UsersControl.cs
T

94 lines
3.7 KiB
C#

using Google.Apis.Auth.OAuth2;
using Google.Apis.Oauth2.v2;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Security.Authentication.Web;
using YouTube.Authorization;
using System.Text.RegularExpressions;
using Windows.Security.Credentials;
using FoxTube.Core.Models;
using YouTube;
using System.Threading;
using Google.Apis.YouTube.v3;
using System.Net.Http;
using Google.Apis.Auth.OAuth2.Responses;
using Newtonsoft.Json;
using Google.Apis.Auth.OAuth2.Flows;
namespace FoxTube
{
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,
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 ExtendedYouTubeService Service => CurrentUser?.Service ?? _defaultService;
public static async Task<bool> AddUser()
{
Uri requestString = AuthorizationHelpers.FormQueryString(ClientSecrets, Scopes);
WebAuthenticationResult result = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.UseTitle, requestString, AuthorizationHelpers.Endpoint);
switch(result.ResponseStatus)
{
case WebAuthenticationStatus.Success:
string successCode = new Regex(@"(?<=code=)(.*?)(?=&)").Match(result.ResponseData).Value;
UserCredential credential = await AuthorizationHelpers.ExchangeToken(ClientSecrets, successCode);
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;
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();
IReadOnlyList<PasswordCredential> credentials;
credentials = passwordVault.RetrieveAll();
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()
{
PasswordVault passwordVault = new PasswordVault();
PasswordCredential credential = passwordVault.Retrieve("foxtube", CurrentUser.UserInfo.Id);
passwordVault.Remove(credential);
await CurrentUser.Credential.RevokeTokenAsync(CancellationToken.None);
}
}
}