From a8f8030a838e458bf28b5e71b1c07fe936f8c6d4 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Sun, 21 May 2023 20:30:49 +0200 Subject: [PATCH] Rewritten the oauth2 system to be more modular and also adjusted the default config --- Moonlight/App/Helpers/StringHelper.cs | 13 ++ .../Api/Moonlight/OAuth2Controller.cs | 123 ++--------------- .../App/Models/Misc/OAuth2ProviderConfig.cs | 8 ++ Moonlight/App/OAuth2/OAuth2Provider.cs | 15 ++ .../OAuth2/Providers/DiscordOAuth2Provider.cs | 122 +++++++++++++++++ .../Providers/GoogleOAuth2Provider.cs} | 109 ++++++--------- .../Services/OAuth2/DiscordOAuth2Service.cs | 129 ------------------ Moonlight/App/Services/OAuth2Service.cs | 85 ++++++++++++ Moonlight/Program.cs | 5 +- Moonlight/Shared/Components/Auth/Login.razor | 54 +++----- .../Shared/Components/Auth/Register.razor | 46 +++---- Moonlight/defaultstorage/configs/config.json | 25 ++-- 12 files changed, 355 insertions(+), 379 deletions(-) create mode 100644 Moonlight/App/Models/Misc/OAuth2ProviderConfig.cs create mode 100644 Moonlight/App/OAuth2/OAuth2Provider.cs create mode 100644 Moonlight/App/OAuth2/Providers/DiscordOAuth2Provider.cs rename Moonlight/App/{Services/OAuth2/GoogleOAuth2Service.cs => OAuth2/Providers/GoogleOAuth2Provider.cs} (53%) delete mode 100644 Moonlight/App/Services/OAuth2/DiscordOAuth2Service.cs create mode 100644 Moonlight/App/Services/OAuth2Service.cs diff --git a/Moonlight/App/Helpers/StringHelper.cs b/Moonlight/App/Helpers/StringHelper.cs index 8c322e97..40069a50 100644 --- a/Moonlight/App/Helpers/StringHelper.cs +++ b/Moonlight/App/Helpers/StringHelper.cs @@ -30,4 +30,17 @@ public static class StringHelper return result; } + + public static string CapitalizeFirstCharacter(string input) + { + if (string.IsNullOrEmpty(input)) + { + return input; + } + + char firstChar = char.ToUpper(input[0]); + string restOfString = input.Substring(1); + + return firstChar + restOfString; + } } \ No newline at end of file diff --git a/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs b/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs index aa6c9fe5..5fc50874 100644 --- a/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs +++ b/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs @@ -1,12 +1,6 @@ using Logging.Net; using Microsoft.AspNetCore.Mvc; -using Moonlight.App.Exceptions; -using Moonlight.App.Helpers; -using Moonlight.App.Models.Misc; -using Moonlight.App.Repositories; using Moonlight.App.Services; -using Moonlight.App.Services.OAuth2; -using Moonlight.App.Services.Sessions; namespace Moonlight.App.Http.Controllers.Api.Moonlight; @@ -14,130 +8,37 @@ namespace Moonlight.App.Http.Controllers.Api.Moonlight; [Route("api/moonlight/oauth2")] public class OAuth2Controller : Controller { - private readonly GoogleOAuth2Service GoogleOAuth2Service; - private readonly DiscordOAuth2Service DiscordOAuth2Service; - private readonly UserRepository UserRepository; private readonly UserService UserService; + private readonly OAuth2Service OAuth2Service; private readonly DateTimeService DateTimeService; - public OAuth2Controller( - GoogleOAuth2Service googleOAuth2Service, - UserRepository userRepository, - UserService userService, - DiscordOAuth2Service discordOAuth2Service, DateTimeService dateTimeService) + public OAuth2Controller(UserService userService, OAuth2Service oAuth2Service, DateTimeService dateTimeService) { - GoogleOAuth2Service = googleOAuth2Service; - UserRepository = userRepository; UserService = userService; - DiscordOAuth2Service = discordOAuth2Service; + OAuth2Service = oAuth2Service; DateTimeService = dateTimeService; } - [HttpGet("google")] - public async Task Google([FromQuery] string code) + [HttpGet("{id}")] + public async Task Hande([FromRoute] string id, [FromQuery] string code) { try { - var userData = await GoogleOAuth2Service.HandleCode(code); + var user = await OAuth2Service.HandleCode(id, code); - if (userData == null) - return Redirect("/login"); - - try + Response.Cookies.Append("token", await UserService.GenerateToken(user), new() { - var user = UserRepository.Get().FirstOrDefault(x => x.Email == userData.Email); + Expires = new DateTimeOffset(DateTimeService.GetCurrent().AddDays(10)) + }); - string token; - - if (user == null) - { - token = await UserService.Register( - userData.Email, - StringHelper.GenerateString(32), - userData.FirstName, - userData.LastName - ); - } - else - { - token = await UserService.GenerateToken(user, true); - } - - Response.Cookies.Append("token", token, new () - { - Expires = new DateTimeOffset(DateTimeService.GetCurrent().AddDays(10)) - }); - - return Redirect("/"); - } - catch (Exception e) - { - Logger.Warn(e.Message); - return Redirect("/login"); - } + return Redirect("/"); } catch (Exception e) { + Logger.Warn("An unexpected error occured while handling oauth2"); Logger.Warn(e.Message); - return BadRequest(); - } - } - [HttpGet("discord")] - public async Task Discord([FromQuery] string code) - { - try - { - var userData = await DiscordOAuth2Service.HandleCode(code); - - if (userData == null) - return Redirect("/login"); - - try - { - var user = UserRepository.Get().FirstOrDefault(x => x.Email == userData.Email); - - string token; - - if (user == null) - { - token = await UserService.Register( - userData.Email, - StringHelper.GenerateString(32), - userData.FirstName, - userData.LastName - ); - - var newUser = UserRepository - .Get() - .First(x => x.Email == userData.Email); - - newUser.Status = UserStatus.DataPending; - - UserRepository.Update(newUser); - } - else - { - token = await UserService.GenerateToken(user, true); - } - - Response.Cookies.Append("token", token, new () - { - Expires = new DateTimeOffset(DateTimeService.GetCurrent().AddDays(10)) - }); - - return Redirect("/"); - } - catch (Exception e) - { - Logger.Warn(e.Message); - return Redirect("/login"); - } - } - catch (Exception e) - { - Logger.Warn(e.Message); - return BadRequest(); + return Redirect("/login"); } } } \ No newline at end of file diff --git a/Moonlight/App/Models/Misc/OAuth2ProviderConfig.cs b/Moonlight/App/Models/Misc/OAuth2ProviderConfig.cs new file mode 100644 index 00000000..de914010 --- /dev/null +++ b/Moonlight/App/Models/Misc/OAuth2ProviderConfig.cs @@ -0,0 +1,8 @@ +namespace Moonlight.App.Models.Misc; + +public class OAuth2ProviderConfig +{ + public string Id { get; set; } = ""; + public string ClientId { get; set; } = ""; + public string ClientSecret { get; set; } = ""; +} \ No newline at end of file diff --git a/Moonlight/App/OAuth2/OAuth2Provider.cs b/Moonlight/App/OAuth2/OAuth2Provider.cs new file mode 100644 index 00000000..af582c13 --- /dev/null +++ b/Moonlight/App/OAuth2/OAuth2Provider.cs @@ -0,0 +1,15 @@ +using Moonlight.App.Database.Entities; +using Moonlight.App.Models.Misc; + +namespace Moonlight.App.OAuth2; + +public abstract class OAuth2Provider +{ + public OAuth2ProviderConfig Config { get; set; } + public string Url { get; set; } + public IServiceScopeFactory ServiceScopeFactory { get; set; } + public string DisplayName { get; set; } + + public abstract Task GetUrl(); + public abstract Task HandleCode(string code); +} \ No newline at end of file diff --git a/Moonlight/App/OAuth2/Providers/DiscordOAuth2Provider.cs b/Moonlight/App/OAuth2/Providers/DiscordOAuth2Provider.cs new file mode 100644 index 00000000..5ed2efea --- /dev/null +++ b/Moonlight/App/OAuth2/Providers/DiscordOAuth2Provider.cs @@ -0,0 +1,122 @@ +using System.Text; +using Logging.Net; +using Moonlight.App.Database.Entities; +using Moonlight.App.Exceptions; +using Moonlight.App.Helpers; +using Moonlight.App.Models.Misc; +using Moonlight.App.Repositories; +using Moonlight.App.Services; +using RestSharp; + +namespace Moonlight.App.OAuth2.Providers; + +public class DiscordOAuth2Provider : OAuth2Provider +{ + public override Task GetUrl() + { + string url = $"https://discord.com/api/oauth2/authorize?client_id={Config.ClientId}" + + $"&redirect_uri={Url}/api/moonlight/oauth2/discord" + + "&response_type=code&scope=identify%20email"; + + return Task.FromResult( + url + ); + } + + public override async Task HandleCode(string code) + { + // Endpoints + + var endpoint = Url + "/api/moonlight/oauth2/discord"; + var discordUserDataEndpoint = "https://discordapp.com/api/users/@me"; + var discordEndpoint = "https://discordapp.com/api/oauth2/token"; + + // Generate access token + + using var client = new RestClient(); + var request = new RestRequest(discordEndpoint); + + request.AddParameter("client_id", Config.ClientId); + request.AddParameter("client_secret", Config.ClientSecret); + request.AddParameter("grant_type", "authorization_code"); + request.AddParameter("code", code); + request.AddParameter("redirect_uri", endpoint); + + var response = await client.ExecutePostAsync(request); + + if (!response.IsSuccessful) + { + Logger.Warn("Error verifying oauth2 code"); + Logger.Warn(response.ErrorMessage); + throw new DisplayException("An error occured while verifying oauth2 code"); + } + + // parse response + + var data = new ConfigurationBuilder().AddJsonStream( + new MemoryStream(Encoding.ASCII.GetBytes(response.Content!)) + ).Build(); + + var accessToken = data.GetValue("access_token"); + + // Now, we will call the discord api with our access token to get the data we need + + var getRequest = new RestRequest(discordUserDataEndpoint); + getRequest.AddHeader("Authorization", $"Bearer {accessToken}"); + + var getResponse = await client.ExecuteGetAsync(getRequest); + + if (!getResponse.IsSuccessful) + { + Logger.Warn("An unexpected error occured while fetching user data from remote api"); + Logger.Warn(getResponse.ErrorMessage); + + throw new DisplayException("An unexpected error occured while fetching user data from remote api"); + } + + // Parse response + + var getData = new ConfigurationBuilder().AddJsonStream( + new MemoryStream(Encoding.ASCII.GetBytes(getResponse.Content!)) + ).Build(); + + var email = getData.GetValue("email"); + var id = getData.GetValue("id"); + + // Handle data + + using var scope = ServiceScopeFactory.CreateScope(); + + var userRepo = scope.ServiceProvider.GetRequiredService>(); + var userService = scope.ServiceProvider.GetRequiredService(); + + if (userRepo.Get().Any(x => x.Email == email)) + { + var user = userRepo.Get().First(x => x.Email == email); + + user.DiscordId = id; + + userRepo.Update(user); + + return user; + } + else + { + await userService.Register( + email, + StringHelper.GenerateString(32), + "User", + "User" + ); + + var user = userRepo.Get().First(x => x.Email == email); + user.Status = UserStatus.DataPending; + + user.DiscordId = id; + + userRepo.Update(user); + + return user; + } + } +} \ No newline at end of file diff --git a/Moonlight/App/Services/OAuth2/GoogleOAuth2Service.cs b/Moonlight/App/OAuth2/Providers/GoogleOAuth2Provider.cs similarity index 53% rename from Moonlight/App/Services/OAuth2/GoogleOAuth2Service.cs rename to Moonlight/App/OAuth2/Providers/GoogleOAuth2Provider.cs index 391308ab..72758542 100644 --- a/Moonlight/App/Services/OAuth2/GoogleOAuth2Service.cs +++ b/Moonlight/App/OAuth2/Providers/GoogleOAuth2Provider.cs @@ -3,70 +3,42 @@ using Logging.Net; using Moonlight.App.ApiClients.Google.Requests; using Moonlight.App.Database.Entities; using Moonlight.App.Exceptions; +using Moonlight.App.Helpers; +using Moonlight.App.Models.Misc; +using Moonlight.App.Repositories; +using Moonlight.App.Services; using RestSharp; -namespace Moonlight.App.Services.OAuth2; +namespace Moonlight.App.OAuth2.Providers; -public class GoogleOAuth2Service +public class GoogleOAuth2Provider : OAuth2Provider { - private readonly bool EnableGoogle; - private readonly string GoogleClientId; - private readonly string GoogleClientSecret; - - private readonly bool EnableOverrideUrl; - private readonly string OverrideUrl; - private readonly string AppUrl; - - public GoogleOAuth2Service(ConfigService configService) + public override Task GetUrl() { - var config = configService - .GetSection("Moonlight") - .GetSection("OAuth2"); - - EnableGoogle = config - .GetSection("Google") - .GetValue("Enable"); - - if (EnableGoogle) - { - GoogleClientId = config.GetSection("Google").GetValue("ClientId"); - GoogleClientSecret = config.GetSection("Google").GetValue("ClientSecret"); - } - - EnableOverrideUrl = config.GetValue("EnableOverrideUrl"); - - if (EnableOverrideUrl) - OverrideUrl = config.GetValue("OverrideUrl"); - - AppUrl = configService.GetSection("Moonlight").GetValue("AppUrl"); - } - - public Task GetUrl() - { - if (!EnableGoogle) - throw new DisplayException("Google OAuth2 not enabled"); - - var endpoint = GetBaseUrl() + "/api/moonlight/oauth2/google"; + var endpoint = Url + "/api/moonlight/oauth2/google"; var scope = "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"; return Task.FromResult( - $"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={GoogleClientId}&redirect_uri={endpoint}&scope={scope}" + $"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={Config.ClientId}&redirect_uri={endpoint}&scope={scope}" ); } - public async Task HandleCode(string code) + public override async Task HandleCode(string code) { - // Generate access token - var endpoint = GetBaseUrl() + "/api/moonlight/oauth2/google"; + // Endpoints + var endpoint = Url + "/api/moonlight/oauth2/google"; var googleEndpoint = "https://oauth2.googleapis.com/token"; + var googlePeopleEndpoint = "https://people.googleapis.com/v1/people/me"; + + // Generate access token // Setup payload var payload = new GoogleOAuth2CodePayload() { Code = code, RedirectUri = endpoint, - ClientId = GoogleClientId, - ClientSecret = GoogleClientSecret + ClientId = Config.ClientId, + ClientSecret = Config.ClientSecret }; using var client = new RestClient(); @@ -77,9 +49,9 @@ public class GoogleOAuth2Service if (!response.IsSuccessful) { - //TODO: Maybe add better error handling - Logger.Debug("oAuth2 validate error: " + response.Content!); - return null; + Logger.Warn("Error verifying oauth2 code"); + Logger.Warn(response.ErrorMessage); + throw new DisplayException("An error occured while verifying oauth2 code"); } // parse response @@ -91,8 +63,6 @@ public class GoogleOAuth2Service var accessToken = data.GetValue("access_token"); // Now, we will call the google api with our access token to get the data we need - - var googlePeopleEndpoint = "https://people.googleapis.com/v1/people/me"; var getRequest = new RestRequest(googlePeopleEndpoint); getRequest.AddHeader("Authorization", $"Bearer {accessToken}"); @@ -102,9 +72,10 @@ public class GoogleOAuth2Service if (!getResponse.IsSuccessful) { - //TODO: Maybe add better error handling - Logger.Debug("OAuth2 api access error: " + getResponse.Content!); - return null; + Logger.Warn("An unexpected error occured while fetching user data from remote api"); + Logger.Warn(getResponse.ErrorMessage); + + throw new DisplayException("An unexpected error occured while fetching user data from remote api"); } // Parse response @@ -131,19 +102,29 @@ public class GoogleOAuth2Service .First() .GetValue("value"); - return new() + using var scope = ServiceScopeFactory.CreateScope(); + + var userRepo = scope.ServiceProvider.GetRequiredService>(); + var userService = scope.ServiceProvider.GetRequiredService(); + + if (userRepo.Get().Any(x => x.Email == email)) { - Email = email, - FirstName = firstName, - LastName = lastName - }; - } + var user = userRepo.Get().First(x => x.Email == email); + + return user; + } + else + { + await userService.Register( + email, + StringHelper.GenerateString(32), + firstName, + lastName + ); - private string GetBaseUrl() - { - if (EnableOverrideUrl) - return OverrideUrl; + var user = userRepo.Get().First(x => x.Email == email); - return AppUrl; + return user; + } } } \ No newline at end of file diff --git a/Moonlight/App/Services/OAuth2/DiscordOAuth2Service.cs b/Moonlight/App/Services/OAuth2/DiscordOAuth2Service.cs deleted file mode 100644 index c387999e..00000000 --- a/Moonlight/App/Services/OAuth2/DiscordOAuth2Service.cs +++ /dev/null @@ -1,129 +0,0 @@ -using System.Text; -using Logging.Net; -using Moonlight.App.Database.Entities; -using Moonlight.App.Exceptions; -using Moonlight.App.Models.Misc; -using RestSharp; - -namespace Moonlight.App.Services.OAuth2; - -public class DiscordOAuth2Service -{ - private readonly bool Enable; - private readonly string ClientId; - private readonly string ClientSecret; - - private readonly bool EnableOverrideUrl; - private readonly string OverrideUrl; - private readonly string AppUrl; - - public DiscordOAuth2Service(ConfigService configService) - { - var config = configService - .GetSection("Moonlight") - .GetSection("OAuth2"); - - Enable = config - .GetSection("Discord") - .GetValue("Enable"); - - if (Enable) - { - ClientId = config.GetSection("Discord").GetValue("ClientId"); - ClientSecret = config.GetSection("Discord").GetValue("ClientSecret"); - } - - EnableOverrideUrl = config.GetValue("EnableOverrideUrl"); - - if (EnableOverrideUrl) - OverrideUrl = config.GetValue("OverrideUrl"); - - AppUrl = configService.GetSection("Moonlight").GetValue("AppUrl"); - } - - public Task GetUrl() - { - if (!Enable) - throw new DisplayException("Discord OAuth2 not enabled"); - - string url = $"https://discord.com/api/oauth2/authorize?client_id={ClientId}" + - $"&redirect_uri={GetBaseUrl()}/api/moonlight/oauth2/discord" + - "&response_type=code&scope=identify%20email"; - - return Task.FromResult( - url - ); - } - - public async Task HandleCode(string code) - { - // Generate access token - var endpoint = GetBaseUrl() + "/api/moonlight/oauth2/discord"; - var discordEndpoint = "https://discordapp.com/api/oauth2/token"; - - using var client = new RestClient(); - var request = new RestRequest(discordEndpoint); - - request.AddParameter("client_id", ClientId); - request.AddParameter("client_secret", ClientSecret); - request.AddParameter("grant_type", "authorization_code"); - request.AddParameter("code", code); - request.AddParameter("redirect_uri", endpoint); - - var response = await client.ExecutePostAsync(request); - - if (!response.IsSuccessful) - { - //TODO: Maybe add better error handling - Logger.Debug("oAuth2 validate error: " + response.Content!); - return null; - } - - // parse response - - var data = new ConfigurationBuilder().AddJsonStream( - new MemoryStream(Encoding.ASCII.GetBytes(response.Content!)) - ).Build(); - - var accessToken = data.GetValue("access_token"); - - // Now, we will call the google api with our access token to get the data we need - - var googlePeopleEndpoint = "https://discordapp.com/api/users/@me"; - - var getRequest = new RestRequest(googlePeopleEndpoint); - getRequest.AddHeader("Authorization", $"Bearer {accessToken}"); - - var getResponse = await client.ExecuteGetAsync(getRequest); - - if (!getResponse.IsSuccessful) - { - //TODO: Maybe add better error handling - Logger.Debug("OAuth2 api access error: " + getResponse.Content!); - return null; - } - - // Parse response - - var getData = new ConfigurationBuilder().AddJsonStream( - new MemoryStream(Encoding.ASCII.GetBytes(getResponse.Content!)) - ).Build(); - - return new User() - { - Email = getData.GetValue("email"), - FirstName = "User", - LastName = "User", - DiscordId = getData.GetValue("id"), - Status = UserStatus.DataPending - }; - } - - private string GetBaseUrl() - { - if (EnableOverrideUrl) - return OverrideUrl; - - return AppUrl; - } -} \ No newline at end of file diff --git a/Moonlight/App/Services/OAuth2Service.cs b/Moonlight/App/Services/OAuth2Service.cs new file mode 100644 index 00000000..3a014b41 --- /dev/null +++ b/Moonlight/App/Services/OAuth2Service.cs @@ -0,0 +1,85 @@ +using Moonlight.App.Database.Entities; +using Moonlight.App.Exceptions; +using Moonlight.App.Helpers; +using Moonlight.App.Models.Misc; +using Moonlight.App.OAuth2; +using Moonlight.App.OAuth2.Providers; + +namespace Moonlight.App.Services; + +public class OAuth2Service +{ + public readonly Dictionary Providers = new(); + private readonly OAuth2ProviderConfig[] Configs; + + private readonly ConfigService ConfigService; + private readonly IServiceScopeFactory ServiceScopeFactory; + + private readonly string OverrideUrl; + private readonly bool EnableOverrideUrl; + private readonly string AppUrl; + + public OAuth2Service(ConfigService configService, IServiceScopeFactory serviceScopeFactory) + { + ConfigService = configService; + ServiceScopeFactory = serviceScopeFactory; + + var config = ConfigService.GetSection("Moonlight").GetSection("OAuth2"); + + Configs = config.GetSection("Providers").Get(); + OverrideUrl = config.GetValue("OverrideUrl"); + EnableOverrideUrl = config.GetValue("EnableOverrideUrl"); + AppUrl = configService.GetSection("Moonlight").GetValue("AppUrl"); + + // Register additional providers here + RegisterOAuth2("discord"); + RegisterOAuth2("google"); + } + + private void RegisterOAuth2(string id, string displayName = "") + { + var name = + string.IsNullOrEmpty(displayName) ? + StringHelper.CapitalizeFirstCharacter(id) : displayName; + + var provider = Activator.CreateInstance()! as OAuth2Provider; + + if (provider == null) + throw new Exception($"Unable to cast oauth2 provider '{typeof(T).Name}'"); + + provider.Config = Configs.First(x => x.Id == id); + provider.Url = GetAppUrl(); + provider.ServiceScopeFactory = ServiceScopeFactory; + provider.DisplayName = name; + + Providers.Add(id, provider); + } + + public async Task GetUrl(string id) + { + if (Providers.All(x => x.Key != id)) + throw new DisplayException("Invalid oauth2 id"); + + var provider = Providers[id]; + + return await provider.GetUrl(); + } + + public async Task HandleCode(string id, string code) + { + if (Providers.All(x => x.Key != id)) + throw new DisplayException("Invalid oauth2 id"); + + var provider = Providers[id]; + + return await provider.HandleCode(code); + } + + private string GetAppUrl() + { + if (EnableOverrideUrl) + return OverrideUrl; + + return AppUrl; + } +} \ No newline at end of file diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index e401e348..daa9fec0 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -23,7 +23,6 @@ using Moonlight.App.Services.LogServices; using Moonlight.App.Services.Mail; using Moonlight.App.Services.Minecraft; using Moonlight.App.Services.Notifications; -using Moonlight.App.Services.OAuth2; using Moonlight.App.Services.Sessions; using Moonlight.App.Services.Statistics; using Moonlight.App.Services.SupportChat; @@ -119,9 +118,7 @@ namespace Moonlight builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); - - builder.Services.AddScoped(); - builder.Services.AddScoped(); + builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/Moonlight/Shared/Components/Auth/Login.razor b/Moonlight/Shared/Components/Auth/Login.razor index dd642e39..5d8b1e04 100644 --- a/Moonlight/Shared/Components/Auth/Login.razor +++ b/Moonlight/Shared/Components/Auth/Login.razor @@ -11,7 +11,6 @@ @using Logging.Net @using Moonlight.App.Database.Entities @using Moonlight.App.Models.Misc -@using Moonlight.App.Services.OAuth2 @using Moonlight.App.Services.Sessions @using System.ComponentModel.DataAnnotations @using Moonlight.App.Models.Forms @@ -21,8 +20,7 @@ @inject SmartTranslateService SmartTranslateService @inject CookieService CookieService @inject NavigationManager NavigationManager -@inject GoogleOAuth2Service GoogleOAuth2Service -@inject DiscordOAuth2Service DiscordOAuth2Service +@inject OAuth2Service OAuth2Service
@@ -39,25 +37,23 @@ Sign in to start with moonlight
- -
-
- - + }
@@ -171,6 +167,9 @@ } catch (Exception e) { + Logger.Error("Error while login"); + Logger.Error(e); + // Reset state LoginData = new(); TotpData = new(); @@ -180,21 +179,12 @@ SmartTranslateService.Translate("Error"), SmartTranslateService.Translate("An error occured while logging you in") ); - - Logger.Error("Error while login"); - Logger.Error(e); } } - private async Task DoGoogle() + private async Task StartOAuth2(string id) { - var url = await GoogleOAuth2Service.GetUrl(); - NavigationManager.NavigateTo(url, true); - } - - private async Task DoDiscord() - { - var url = await DiscordOAuth2Service.GetUrl(); - NavigationManager.NavigateTo(url, true); + var url = await OAuth2Service.GetUrl(id); + NavigationManager.NavigateTo(url ,true); } } \ No newline at end of file diff --git a/Moonlight/Shared/Components/Auth/Register.razor b/Moonlight/Shared/Components/Auth/Register.razor index 714bed6d..1e7332d3 100644 --- a/Moonlight/Shared/Components/Auth/Register.razor +++ b/Moonlight/Shared/Components/Auth/Register.razor @@ -6,18 +6,16 @@ *@ @using Moonlight.App.Services -@using Moonlight.App.Services.OAuth2 @using Moonlight.App.Models.Forms @using Moonlight.App.Services.Interop @using Moonlight.App.Services.Sessions @inject SmartTranslateService SmartTranslateService -@inject GoogleOAuth2Service GoogleOAuth2Service @inject NavigationManager NavigationManager -@inject DiscordOAuth2Service DiscordOAuth2Service @inject AlertService AlertService @inject UserService UserService @inject CookieService CookieService +@inject OAuth2Service OAuth2Service
@@ -33,24 +31,22 @@
-
-
- - + }
@@ -112,16 +108,10 @@ { private UserRegisterModel UserRegisterModel = new(); - private async Task DoGoogle() + private async Task StartOAuth2(string id) { - var url = await GoogleOAuth2Service.GetUrl(); - NavigationManager.NavigateTo(url, true); - } - - private async Task DoDiscord() - { - var url = await DiscordOAuth2Service.GetUrl(); - NavigationManager.NavigateTo(url, true); + var url = await OAuth2Service.GetUrl(id); + NavigationManager.NavigateTo(url ,true); } private async Task CreateUser() diff --git a/Moonlight/defaultstorage/configs/config.json b/Moonlight/defaultstorage/configs/config.json index 397cb151..0a841337 100644 --- a/Moonlight/defaultstorage/configs/config.json +++ b/Moonlight/defaultstorage/configs/config.json @@ -34,17 +34,20 @@ "Website": "https://mycoolproject.de/" }, "OAuth2": { - "Discord": { - "ClientId": "10324", - "ClientSecret": "s3cr3t", - "Enable": "True" - }, - "Google": { - "ClientId": "xyz.apps.googleusercontent.com", - "ClientSecret": "s3cr3t", - "Enable": "True" - }, - "EnableOverrideUrl": "True", + "_exampleProviders": [ + { + "Id": "discord", + "ClientId": "", + "ClientSecret": "" + }, + { + "Id": "google", + "ClientId": "", + "ClientSecret": "" + } + ], + "Providers": [], + "EnableOverrideUrl": false, "OverrideUrl": "http://your-moonlight-url.test" }, "Security": {