using System; using System.Linq; using System.Text; using System.Threading.Tasks; using AgileConfig.Server.Common; using AgileConfig.Server.IService; using Microsoft.AspNetCore.Http; namespace AgileConfig.Server.Service; public class AppBasicAuthService : IAppBasicAuthService { private readonly IAppService _appService; public AppBasicAuthService(IAppService appService) { _appService = appService; } /// /// Parse the appId and secret from the HTTP request. /// /// Incoming HTTP request containing the Authorization header. /// Tuple of Application ID and secret extracted from the header. public (string, string) GetAppIdSecret(HttpRequest httpRequest) { return Encrypt.UnboxBasicAuth(httpRequest); } public async Task ValidAsync(HttpRequest httpRequest) { var appIdSecret = GetAppIdSecret(httpRequest); var appId = appIdSecret.Item1; var sec = appIdSecret.Item2; if (string.IsNullOrEmpty(appIdSecret.Item1)) return false; var app = await _appService.GetAsync(appId); if (app == null) return false; if (!app.Enabled) return false; var txt = $"{app.Id}:{app.Secret}"; return txt == $"{appId}:{sec}"; } public (string, string) GetUserNamePassword(HttpRequest httpRequest) { throw new NotImplementedException(); } }