diff --git a/ApiDto/Common/OAuthProvider.cs b/ApiDto/Common/OAuthProvider.cs new file mode 100644 index 0000000..ccd0622 --- /dev/null +++ b/ApiDto/Common/OAuthProvider.cs @@ -0,0 +1,22 @@ +namespace Mirea.Api.Dto.Common; + +/// +/// Represents different OAuth providers for authentication. +/// +public enum OAuthProvider +{ + /// + /// OAuth provider for Google. + /// + Google, + + /// + /// OAuth provider for Yandex. + /// + Yandex, + + /// + /// OAuth provider for Mail.ru. + /// + MailRu +} diff --git a/ApiDto/Responses/AvailableProvidersResponse.cs b/ApiDto/Responses/AvailableProvidersResponse.cs new file mode 100644 index 0000000..c4e9dfc --- /dev/null +++ b/ApiDto/Responses/AvailableProvidersResponse.cs @@ -0,0 +1,25 @@ +using Mirea.Api.Dto.Common; +using System; + +namespace Mirea.Api.Dto.Responses; + +/// +/// Represents the response containing information about available OAuth providers. +/// +public class AvailableProvidersResponse +{ + /// + /// Gets or sets the name of the OAuth provider. + /// + public required string ProviderName { get; set; } + + /// + /// Gets or sets the enum value representing the OAuth provider. + /// + public OAuthProvider Provider { get; set; } + + /// + /// Gets or sets the redirect URI for the OAuth provider. + /// + public required Uri Redirect { get; set; } +} \ No newline at end of file diff --git a/Endpoint/Common/MapperDto/AvailableProvidersConverter.cs b/Endpoint/Common/MapperDto/AvailableProvidersConverter.cs new file mode 100644 index 0000000..02021bd --- /dev/null +++ b/Endpoint/Common/MapperDto/AvailableProvidersConverter.cs @@ -0,0 +1,27 @@ +using Mirea.Api.Dto.Responses; +using Mirea.Api.Security.Common.Domain; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Mirea.Api.Endpoint.Common.MapperDto; + +public static class AvailableProvidersConverter +{ + public static Dto.Common.OAuthProvider ConvertToDto(this OAuthProvider provider) => + provider switch + { + OAuthProvider.Google => Dto.Common.OAuthProvider.Google, + OAuthProvider.Yandex => Dto.Common.OAuthProvider.Yandex, + OAuthProvider.MailRu => Dto.Common.OAuthProvider.MailRu, + _ => throw new ArgumentOutOfRangeException(nameof(provider), provider, null) + }; + + public static List ConvertToDto(this (OAuthProvider Provider, Uri Redirect)[] data) => + data.Select(x => new AvailableProvidersResponse() + { + ProviderName = Enum.GetName(x.Provider)!, + Provider = x.Provider.ConvertToDto(), + Redirect = x.Redirect + }).ToList(); +} \ No newline at end of file diff --git a/Endpoint/Controllers/V1/AuthController.cs b/Endpoint/Controllers/V1/AuthController.cs index 69f0ed0..075f298 100644 --- a/Endpoint/Controllers/V1/AuthController.cs +++ b/Endpoint/Controllers/V1/AuthController.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Mirea.Api.Dto.Common; using Mirea.Api.Dto.Requests; +using Mirea.Api.Dto.Responses; using Mirea.Api.Endpoint.Common.Attributes; using Mirea.Api.Endpoint.Common.Exceptions; using Mirea.Api.Endpoint.Common.MapperDto; @@ -28,6 +29,12 @@ public class AuthController(IOptionsSnapshot user, AuthService auth, Pass Path = UrlHelper.GetSubPathWithoutFirstApiName + "api" }; + [HttpGet("GetAvailableProviders")] + public ActionResult> GetUrls() => + Ok(oAuthService + .GetAvailableProviders(HttpContext, GetCookieParams(), HttpContext.GetApiUrl(Url.Action("OAuth2")!)) + .ConvertToDto()); + [HttpPost("Login")] [BadRequestResponse] public async Task> Login([FromBody] LoginRequest request)