From 9f8c5a12e4d670a94168720ff76a02308c5faef4 Mon Sep 17 00:00:00 2001 From: David Jimenez Barrantes Date: Mon, 26 Aug 2024 16:21:22 -0600 Subject: [PATCH] [DI-1383] - Adds basic authentication. (#132) * Adds basic authentication. * Trying to fix tests. * code simplification and renames --- .../PowerShellPreprocessorServiceTests.cs | 6 +- .../Helpers/AuthRequestWrapper.cs | 77 +++++++++++++++++++ .../Helpers/BasicAuthRequestWrapper.cs | 53 +++++++++++++ .../Helpers/IAuthRequestWrapper.cs | 18 +++++ .../Helpers/OAuthRequestWrapper.cs | 71 ++--------------- .../Helpers/OdsApiTokenRetriever.cs | 8 +- .../PowerShellPreprocessorService.cs | 4 +- .../FileGenerator/FileGeneratorTests.cs | 6 +- .../PowershellTabularDataTests.cs | 6 +- .../appsettings.json | 3 +- .../AppSettings.cs | 1 + DataImport.Server.TransformLoad/Startup.cs | 6 +- .../Helpers/OdsApiTokenRetrieverTests.cs | 12 +-- .../Services/EdFiV311Tests.cs | 2 +- DataImport.Web.Tests/appsettings.json | 3 +- DataImport.Web/AppSettings.cs | 1 + .../ApiServers/TestApiServerConnection.cs | 4 +- DataImport.Web/Services/EdFiServiceV25.cs | 4 +- DataImport.Web/Services/EdFiServiceV311.cs | 4 +- DataImport.Web/Startup.cs | 5 +- DataImport.Web/appsettings.json | 3 +- 21 files changed, 199 insertions(+), 98 deletions(-) create mode 100644 DataImport.Common/Helpers/AuthRequestWrapper.cs create mode 100644 DataImport.Common/Helpers/BasicAuthRequestWrapper.cs create mode 100644 DataImport.Common/Helpers/IAuthRequestWrapper.cs diff --git a/DataImport.Common.Tests/PowerShellPreprocessorServiceTests.cs b/DataImport.Common.Tests/PowerShellPreprocessorServiceTests.cs index 350c7870..fdeb8ec3 100644 --- a/DataImport.Common.Tests/PowerShellPreprocessorServiceTests.cs +++ b/DataImport.Common.Tests/PowerShellPreprocessorServiceTests.cs @@ -38,10 +38,10 @@ public void Setup() { _powerShellPreprocessorOptions = new PowerShellPreprocessorOptions(); - var oAuthRequestWrapper = A.Fake(); + var oAuthRequestWrapper = A.Fake(); A.CallTo(() => oAuthRequestWrapper.GetAccessCode(null, null)).WithAnyArguments().Returns("fake token"); - A.CallTo(() => oAuthRequestWrapper.GetBearerToken(null, null)).WithAnyArguments().Returns("fake token"); - A.CallTo(() => oAuthRequestWrapper.GetBearerToken(null, null, null)).WithAnyArguments().Returns("fake token"); + A.CallTo(() => oAuthRequestWrapper.GetToken(null, null)).WithAnyArguments().Returns("fake token"); + A.CallTo(() => oAuthRequestWrapper.GetToken(null, null, null)).WithAnyArguments().Returns("fake token"); var powerShellPreprocessSettings = new PowerShellPreprocessSettings { EncryptionKey = Guid.NewGuid().ToString() }; _service = new PowerShellPreprocessorService(powerShellPreprocessSettings, _powerShellPreprocessorOptions, oAuthRequestWrapper); diff --git a/DataImport.Common/Helpers/AuthRequestWrapper.cs b/DataImport.Common/Helpers/AuthRequestWrapper.cs new file mode 100644 index 00000000..be8119b4 --- /dev/null +++ b/DataImport.Common/Helpers/AuthRequestWrapper.cs @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: Apache-2.0 +// Licensed to the Ed-Fi Alliance under one or more agreements. +// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. +// See the LICENSE and NOTICES files in the project root for more information. + +using System; +using System.Net; +using System.Security.Authentication; +using DataImport.Models; +using RestSharp; +using static DataImport.Common.Encryption; + +namespace DataImport.Common.Helpers +{ + public abstract class AuthRequestWrapper + { + public virtual RestClientOptions GetOptions(Uri tokenUrl) + { + RestClientOptions options; + + if (ScriptExtensions.IgnoresCertificateErrors()) + { +#pragma warning disable S4830 + options = new RestClientOptions(tokenUrl.GetLeftPart(UriPartial.Authority)) + { + RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true + }; +#pragma warning restore S4830 + } + else + { + options = new RestClientOptions(tokenUrl.GetLeftPart(UriPartial.Authority)); + } + + return options; + } + + public virtual string GetAccessCode(ApiServer apiServer, string encryptionKey) + { + var authUrl = new Uri(apiServer.AuthUrl); + var authClient = new RestClient(authUrl.GetLeftPart(UriPartial.Authority)); + + var accessCodeRequest = new RestRequest(authUrl.AbsolutePath, Method.Post); + var apiServerKey = !string.IsNullOrEmpty(encryptionKey) + ? Decrypt(apiServer.Key, encryptionKey) + : apiServer.Key; + accessCodeRequest.AddParameter("Client_id", apiServerKey); + accessCodeRequest.AddParameter("Response_type", "code"); + + var accessCodeResponse = authClient.Execute(accessCodeRequest); + + if (accessCodeResponse.StatusCode != HttpStatusCode.OK) + throw new AuthenticationException("Unable to retrieve an authorization code. Error message: " + + accessCodeResponse.ErrorMessage); + if (accessCodeResponse.Data.Error != null) + throw new AuthenticationException( + "Unable to retrieve an authorization code. Please verify that your application key is correct. Alternately, the service address may not be correct: " + + authUrl); + + return accessCodeResponse.Data.Code; + } + + public virtual string GetToken(RestRequest tokenRequest, RestClient oauthClient) + { + var tokenResponse = oauthClient.Execute(tokenRequest); + if (tokenResponse.StatusCode != HttpStatusCode.OK) + throw new AuthenticationException("Unable to retrieve an access token. Error message: " + + tokenResponse.ErrorMessage); + + if (tokenResponse.Data.Error != null || tokenResponse.Data.TokenType != "bearer") + throw new AuthenticationException( + "Unable to retrieve an access token. Please verify that your application secret is correct."); + + return tokenResponse.Data.AccessToken; + } + } +} diff --git a/DataImport.Common/Helpers/BasicAuthRequestWrapper.cs b/DataImport.Common/Helpers/BasicAuthRequestWrapper.cs new file mode 100644 index 00000000..b140ae2b --- /dev/null +++ b/DataImport.Common/Helpers/BasicAuthRequestWrapper.cs @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: Apache-2.0 +// Licensed to the Ed-Fi Alliance under one or more agreements. +// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. +// See the LICENSE and NOTICES files in the project root for more information. + +using System; +using System.Text; +using DataImport.Models; +using RestSharp; +using static DataImport.Common.Encryption; + +namespace DataImport.Common.Helpers +{ + public class BasicAuthRequestWrapper : AuthRequestWrapper, IAuthRequestWrapper + { + public string GetToken(ApiServer apiServer, string encryptionKey) + { + return GetToken(apiServer, encryptionKey, null); + } + + public string GetToken(ApiServer apiServer, string encryptionKey, string accessCode) + { + var tokenUrl = new Uri(apiServer.TokenUrl); + RestClientOptions options = GetOptions(tokenUrl); + + var authClient = new RestClient(options); + + var tokenRequest = new RestRequest(tokenUrl.AbsolutePath, Method.Post); + + var apiServerKey = !string.IsNullOrEmpty(encryptionKey) + ? Decrypt(apiServer.Key, encryptionKey) + : apiServer.Key; + var apiServerSecret = !string.IsNullOrEmpty(encryptionKey) + ? Decrypt(apiServer.Secret, encryptionKey) + : apiServer.Secret; + + var keySecretBytes = Encoding.UTF8.GetBytes($"{apiServerKey}:{apiServerSecret}"); + tokenRequest.AddHeader("Authorization", $"Basic {Convert.ToBase64String(keySecretBytes)}"); + + if (accessCode != null) + { + tokenRequest.AddParameter("code", accessCode); + tokenRequest.AddParameter("grant_type", "authorization_code"); + } + else + { + tokenRequest.AddParameter("grant_type", "client_credentials"); + } + + return GetToken(tokenRequest, authClient); + } + } +} diff --git a/DataImport.Common/Helpers/IAuthRequestWrapper.cs b/DataImport.Common/Helpers/IAuthRequestWrapper.cs new file mode 100644 index 00000000..380c2948 --- /dev/null +++ b/DataImport.Common/Helpers/IAuthRequestWrapper.cs @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: Apache-2.0 +// Licensed to the Ed-Fi Alliance under one or more agreements. +// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. +// See the LICENSE and NOTICES files in the project root for more information. + +using DataImport.Models; + +namespace DataImport.Common.Helpers +{ + public interface IAuthRequestWrapper + { + string GetAccessCode(ApiServer apiServer, string encryptionKey); + + string GetToken(ApiServer apiServer, string encryptionKey, string accessCode); + + string GetToken(ApiServer apiServer, string encryptionKey); + } +} diff --git a/DataImport.Common/Helpers/OAuthRequestWrapper.cs b/DataImport.Common/Helpers/OAuthRequestWrapper.cs index 698b7e6a..3ce5a142 100644 --- a/DataImport.Common/Helpers/OAuthRequestWrapper.cs +++ b/DataImport.Common/Helpers/OAuthRequestWrapper.cs @@ -4,73 +4,23 @@ // See the LICENSE and NOTICES files in the project root for more information. using System; -using System.Net; -using System.Security.Authentication; using DataImport.Models; using RestSharp; using static DataImport.Common.Encryption; namespace DataImport.Common.Helpers { - public interface IOAuthRequestWrapper + public class OAuthRequestWrapper : AuthRequestWrapper, IAuthRequestWrapper { - string GetAccessCode(ApiServer apiServer, string encryptionKey); - - string GetBearerToken(ApiServer apiServer, string encryptionKey, string accessCode); - - string GetBearerToken(ApiServer apiServer, string encryptionKey); - } - - public class OAuthRequestWrapper : IOAuthRequestWrapper - { - public string GetAccessCode(ApiServer apiServer, string encryptionKey) + public string GetToken(ApiServer apiServer, string encryptionKey) { - var authUrl = new Uri(apiServer.AuthUrl); - var oauthClient = new RestClient(authUrl.GetLeftPart(UriPartial.Authority)); - - var accessCodeRequest = new RestRequest(authUrl.AbsolutePath, Method.Post); - var apiServerKey = !string.IsNullOrEmpty(encryptionKey) - ? Decrypt(apiServer.Key, encryptionKey) - : apiServer.Key; - accessCodeRequest.AddParameter("Client_id", apiServerKey); - accessCodeRequest.AddParameter("Response_type", "code"); - - var accessCodeResponse = oauthClient.Execute(accessCodeRequest); - - if (accessCodeResponse.StatusCode != HttpStatusCode.OK) - throw new AuthenticationException("Unable to retrieve an authorization code. Error message: " + - accessCodeResponse.ErrorMessage); - if (accessCodeResponse.Data.Error != null) - throw new AuthenticationException( - "Unable to retrieve an authorization code. Please verify that your application key is correct. Alternately, the service address may not be correct: " + - authUrl); - - return accessCodeResponse.Data.Code; + return GetToken(apiServer, encryptionKey, null); } - public string GetBearerToken(ApiServer apiServer, string encryptionKey) - { - return GetBearerToken(apiServer, encryptionKey, null); - } - - public string GetBearerToken(ApiServer apiServer, string encryptionKey, string accessCode) + public string GetToken(ApiServer apiServer, string encryptionKey, string accessCode) { var tokenUrl = new Uri(apiServer.TokenUrl); - RestClientOptions options; - - if (ScriptExtensions.IgnoresCertificateErrors()) - { -#pragma warning disable S4830 - options = new RestClientOptions(tokenUrl.GetLeftPart(UriPartial.Authority)) - { - RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true - }; -#pragma warning restore S4830 - } - else - { - options = new RestClientOptions(tokenUrl.GetLeftPart(UriPartial.Authority)); - } + RestClientOptions options = GetOptions(tokenUrl); var oauthClient = new RestClient(options); @@ -95,16 +45,7 @@ public string GetBearerToken(ApiServer apiServer, string encryptionKey, string a bearerTokenRequest.AddParameter("grant_type", "client_credentials"); } - var bearerTokenResponse = oauthClient.Execute(bearerTokenRequest); - if (bearerTokenResponse.StatusCode != HttpStatusCode.OK) - throw new AuthenticationException("Unable to retrieve an access token. Error message: " + - bearerTokenResponse.ErrorMessage); - - if (bearerTokenResponse.Data.Error != null || bearerTokenResponse.Data.TokenType != "bearer") - throw new AuthenticationException( - "Unable to retrieve an access token. Please verify that your application secret is correct."); - - return bearerTokenResponse.Data.AccessToken; + return GetToken(bearerTokenRequest, oauthClient); } } } diff --git a/DataImport.Common/Helpers/OdsApiTokenRetriever.cs b/DataImport.Common/Helpers/OdsApiTokenRetriever.cs index ca58d655..4adc651b 100644 --- a/DataImport.Common/Helpers/OdsApiTokenRetriever.cs +++ b/DataImport.Common/Helpers/OdsApiTokenRetriever.cs @@ -14,9 +14,9 @@ public class OdsApiTokenRetriever : ITokenRetriever { private readonly ApiServer _apiServer; private readonly string _encryptionKey; - private readonly IOAuthRequestWrapper _requestWrapper; + private readonly IAuthRequestWrapper _requestWrapper; - public OdsApiTokenRetriever(IOAuthRequestWrapper requestWrapper, ApiServer apiServer, string encryptionKey = "") + public OdsApiTokenRetriever(IAuthRequestWrapper requestWrapper, ApiServer apiServer, string encryptionKey = "") { _requestWrapper = requestWrapper ?? throw new ArgumentNullException(nameof(requestWrapper)); @@ -30,10 +30,10 @@ public string ObtainNewBearerToken() { var accessCode = _requestWrapper.GetAccessCode(_apiServer, _encryptionKey); - return _requestWrapper.GetBearerToken(_apiServer, _encryptionKey, accessCode); + return _requestWrapper.GetToken(_apiServer, _encryptionKey, accessCode); } - return _requestWrapper.GetBearerToken(_apiServer, _encryptionKey); + return _requestWrapper.GetToken(_apiServer, _encryptionKey); } } diff --git a/DataImport.Common/Preprocessors/PowerShellPreprocessorService.cs b/DataImport.Common/Preprocessors/PowerShellPreprocessorService.cs index 4d53bb71..b4699f74 100644 --- a/DataImport.Common/Preprocessors/PowerShellPreprocessorService.cs +++ b/DataImport.Common/Preprocessors/PowerShellPreprocessorService.cs @@ -28,10 +28,10 @@ namespace DataImport.Common.Preprocessors public class PowerShellPreprocessorService : IPowerShellPreprocessorService { private readonly PowerShellPreprocessorOptions _psPreprocessorOptions; - private readonly IOAuthRequestWrapper _authRequestWrapper; + private readonly IAuthRequestWrapper _authRequestWrapper; private readonly IPowerShellPreprocessSettings _powerShellPreprocessSettings; - public PowerShellPreprocessorService(IPowerShellPreprocessSettings powerShellPreprocessSettings, PowerShellPreprocessorOptions options, IOAuthRequestWrapper authRequestWrapper) + public PowerShellPreprocessorService(IPowerShellPreprocessSettings powerShellPreprocessSettings, PowerShellPreprocessorOptions options, IAuthRequestWrapper authRequestWrapper) { _psPreprocessorOptions = options ?? throw new ArgumentNullException(nameof(options)); _authRequestWrapper = authRequestWrapper; diff --git a/DataImport.Server.TransformLoad.Tests/Features/FileGenerator/FileGeneratorTests.cs b/DataImport.Server.TransformLoad.Tests/Features/FileGenerator/FileGeneratorTests.cs index ebf62930..f3b128d9 100644 --- a/DataImport.Server.TransformLoad.Tests/Features/FileGenerator/FileGeneratorTests.cs +++ b/DataImport.Server.TransformLoad.Tests/Features/FileGenerator/FileGeneratorTests.cs @@ -34,7 +34,7 @@ public async Task ShouldCorrectlyGenerateFile() var fileServices = GetRegisteredFileServices(); var dbContext = Services.GetService(); var options = Services.GetService>(); - var service = new PowerShellPreprocessorService(options.Value, new PowerShellPreprocessorOptions(), A.Fake()); + var service = new PowerShellPreprocessorService(options.Value, new PowerShellPreprocessorOptions(), A.Fake()); var extService = Services.GetService(); var commandHandler = new CommandHandlerTestWrapper(logger, options, dbContext, fileServices, service, extService); @@ -79,7 +79,7 @@ public async Task ShouldRunAgentsInOrder() var fileServices = GetRegisteredFileServices(); var dbContext = Services.GetService(); var options = Services.GetService>(); - var service = new PowerShellPreprocessorService(options.Value, new PowerShellPreprocessorOptions(), A.Fake()); + var service = new PowerShellPreprocessorService(options.Value, new PowerShellPreprocessorOptions(), A.Fake()); var extService = Services.GetService(); var commandHandler = new CommandHandlerTestWrapper(logger, options, dbContext, fileServices, service, extService); @@ -147,7 +147,7 @@ public async Task ShouldRunOnlyEnabledAgents() var fileServices = GetRegisteredFileServices(); var dbContext = Services.GetService(); var options = Services.GetService>(); - var service = new PowerShellPreprocessorService(options.Value, new PowerShellPreprocessorOptions(), A.Fake()); + var service = new PowerShellPreprocessorService(options.Value, new PowerShellPreprocessorOptions(), A.Fake()); var extService = Services.GetService(); var commandHandler = new CommandHandlerTestWrapper(logger, options, dbContext, fileServices, service, extService); diff --git a/DataImport.Server.TransformLoad.Tests/Features/LoadResources/PowershellTabularDataTests.cs b/DataImport.Server.TransformLoad.Tests/Features/LoadResources/PowershellTabularDataTests.cs index 2d7ee1de..dbb71470 100644 --- a/DataImport.Server.TransformLoad.Tests/Features/LoadResources/PowershellTabularDataTests.cs +++ b/DataImport.Server.TransformLoad.Tests/Features/LoadResources/PowershellTabularDataTests.cs @@ -36,10 +36,10 @@ function ReformatDate($value, $from, $to) { [SetUp] public void Setup() { - var oAuthRequestWrapper = A.Fake(); + var oAuthRequestWrapper = A.Fake(); A.CallTo(() => oAuthRequestWrapper.GetAccessCode(null, null)).WithAnyArguments().Returns("fake token"); - A.CallTo(() => oAuthRequestWrapper.GetBearerToken(null, null)).WithAnyArguments().Returns("fake token"); - A.CallTo(() => oAuthRequestWrapper.GetBearerToken(null, null, null)).WithAnyArguments().Returns("fake token"); + A.CallTo(() => oAuthRequestWrapper.GetToken(null, null)).WithAnyArguments().Returns("fake token"); + A.CallTo(() => oAuthRequestWrapper.GetToken(null, null, null)).WithAnyArguments().Returns("fake token"); var appSettings = new AppSettings { EncryptionKey = Guid.NewGuid().ToString() }; _service = new PowerShellPreprocessorService(appSettings, new PowerShellPreprocessorOptions(), oAuthRequestWrapper); diff --git a/DataImport.Server.TransformLoad.Tests/appsettings.json b/DataImport.Server.TransformLoad.Tests/appsettings.json index e7224cba..e77bcc8b 100644 --- a/DataImport.Server.TransformLoad.Tests/appsettings.json +++ b/DataImport.Server.TransformLoad.Tests/appsettings.json @@ -6,7 +6,8 @@ "FileMode": "Local", "ShareName": "(Initialized During Execution)", "UsePowerShellWithNoRestrictions": false, - "MinimumLevelIngestionLog": "INFORMATION" + "MinimumLevelIngestionLog": "INFORMATION", + "UseBasicAuthentication": false }, "Concurrency": { "LimitConcurrentApiPosts": true, diff --git a/DataImport.Server.TransformLoad/AppSettings.cs b/DataImport.Server.TransformLoad/AppSettings.cs index 0252542f..078786d9 100644 --- a/DataImport.Server.TransformLoad/AppSettings.cs +++ b/DataImport.Server.TransformLoad/AppSettings.cs @@ -18,6 +18,7 @@ public class AppSettings : IFileSettings, IPowerShellPreprocessSettings, IEncryp public bool UsePowerShellWithNoRestrictions { get; set; } public string MinimumLevelIngestionLog { get; set; } public bool IgnoresCertificateErrors { get; set; } = false; + public bool UseBasicAuthentication { get; set; } = false; } public class ConcurrencySettings diff --git a/DataImport.Server.TransformLoad/Startup.cs b/DataImport.Server.TransformLoad/Startup.cs index e0d6484c..c3474523 100644 --- a/DataImport.Server.TransformLoad/Startup.cs +++ b/DataImport.Server.TransformLoad/Startup.cs @@ -104,7 +104,11 @@ public static IServiceCollection ConfigureTransformLoadServices(this IServiceCol return resolver.Resolve(); }); services.AddTransient(); - services.AddTransient(); + + if (bool.Parse(configuration.GetSection("AppSettings")["UseBasicAuthentication"])) + services.AddTransient(); + else + services.AddTransient(); services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())); diff --git a/DataImport.Web.Tests/Helpers/OdsApiTokenRetrieverTests.cs b/DataImport.Web.Tests/Helpers/OdsApiTokenRetrieverTests.cs index c707a50a..2aa84d4d 100644 --- a/DataImport.Web.Tests/Helpers/OdsApiTokenRetrieverTests.cs +++ b/DataImport.Web.Tests/Helpers/OdsApiTokenRetrieverTests.cs @@ -23,7 +23,7 @@ public void Given_happy_path_should_not_throw_exception() { Action act = () => { - var _ = new OdsApiTokenRetriever(A.Fake(), new ApiServer + var _ = new OdsApiTokenRetriever(A.Fake(), new ApiServer { ApiVersion = new ApiVersion { @@ -59,7 +59,7 @@ public class ObtainNewBearerToken [SetUp] public void SetUp() { - _oAuthRequestWrapper = A.Fake(); + _oAuthRequestWrapper = A.Fake(); _apiServer = new ApiServer { ApiVersion = new ApiVersion() @@ -70,7 +70,7 @@ public void SetUp() private OdsApiTokenRetriever _systemUnderTest; - private IOAuthRequestWrapper _oAuthRequestWrapper; + private IAuthRequestWrapper _oAuthRequestWrapper; private ApiServer _apiServer; @@ -87,7 +87,7 @@ public void Given_setup_for_suite2_version260_then_use_access_code_to_get_bearer .Returns(AccessCode); A.CallTo(() => - _oAuthRequestWrapper.GetBearerToken(_apiServer, EncryptionKey, AccessCode)) + _oAuthRequestWrapper.GetToken(_apiServer, EncryptionKey, AccessCode)) .Returns(BearerToken); _apiServer.ApiVersion.Version = "2.6.0"; @@ -105,7 +105,7 @@ public void Given_setup_for_suite3_version311_then_do_not_need_access_code_for_b // Arrange const string BearerToken = "bearer token"; - A.CallTo(() => _oAuthRequestWrapper.GetBearerToken(_apiServer, EncryptionKey)) + A.CallTo(() => _oAuthRequestWrapper.GetToken(_apiServer, EncryptionKey)) .Returns(BearerToken); _apiServer.ApiVersion.Version = "3.1.1"; @@ -123,7 +123,7 @@ public void Given_setup_for_suite3_version510_then_do_not_need_access_code_for_b // Arrange const string BearerToken = "bearer token"; - A.CallTo(() => _oAuthRequestWrapper.GetBearerToken(_apiServer, EncryptionKey)) + A.CallTo(() => _oAuthRequestWrapper.GetToken(_apiServer, EncryptionKey)) .Returns(BearerToken); _apiServer.ApiVersion.Version = "5.1.0"; diff --git a/DataImport.Web.Tests/Services/EdFiV311Tests.cs b/DataImport.Web.Tests/Services/EdFiV311Tests.cs index 6854f77d..5c6fc463 100644 --- a/DataImport.Web.Tests/Services/EdFiV311Tests.cs +++ b/DataImport.Web.Tests/Services/EdFiV311Tests.cs @@ -34,7 +34,7 @@ public EdFiServiceV311Tss(ISwaggerMetadataFetcher metadataFetcher) GetEncryptionKeyResolver(), A.Fake(), metadataFetcher, - A.Fake(), + A.Fake(), null) { } diff --git a/DataImport.Web.Tests/appsettings.json b/DataImport.Web.Tests/appsettings.json index 7aae030f..d8e34595 100644 --- a/DataImport.Web.Tests/appsettings.json +++ b/DataImport.Web.Tests/appsettings.json @@ -7,7 +7,8 @@ "FileMode": "Local", "ShareName": "C:\\Temp", "UsePowerShellWithNoRestrictions": false, - "IgnoresCertificateErrors": false + "IgnoresCertificateErrors": false, + "UseBasicAuthentication": false }, "ConnectionStrings": { "defaultConnection": "Data Source=(local);Initial Catalog=EdFi_DataImport_Test;Trusted_Connection=True;Integrated Security=True;TrustServerCertificate=True;Encrypt=False", diff --git a/DataImport.Web/AppSettings.cs b/DataImport.Web/AppSettings.cs index bf052af8..3d60933b 100644 --- a/DataImport.Web/AppSettings.cs +++ b/DataImport.Web/AppSettings.cs @@ -21,5 +21,6 @@ public class AppSettings : IFileSettings, IPowerShellPreprocessSettings, IEncryp public string UserRecoveryToken { get; set; } public bool AllowUserRegistration { get; set; } public bool IgnoresCertificateErrors { get; set; } = false; + public bool UseBasicAuthentication { get; set; } = false; } } diff --git a/DataImport.Web/Features/ApiServers/TestApiServerConnection.cs b/DataImport.Web/Features/ApiServers/TestApiServerConnection.cs index 4e499276..565d734a 100644 --- a/DataImport.Web/Features/ApiServers/TestApiServerConnection.cs +++ b/DataImport.Web/Features/ApiServers/TestApiServerConnection.cs @@ -84,12 +84,12 @@ public class QueryHandler : IRequestHandler private readonly DataImportDbContext _database; private readonly IEncryptionService _encryptionService; private readonly IConfigurationService _configurationService; - private readonly IOAuthRequestWrapper _oAuthRequestWrapper; + private readonly IAuthRequestWrapper _oAuthRequestWrapper; private readonly string _encryptionKey; private readonly IOptions _options; public QueryHandler(ILogger logger, DataImportDbContext database, IEncryptionKeyResolver encryptionKeyResolver, - IEncryptionService encryptionService, IConfigurationService configurationService, IOAuthRequestWrapper oAuthRequestWrapper, IOptions options) + IEncryptionService encryptionService, IConfigurationService configurationService, IAuthRequestWrapper oAuthRequestWrapper, IOptions options) { _logger = logger; _database = database; diff --git a/DataImport.Web/Services/EdFiServiceV25.cs b/DataImport.Web/Services/EdFiServiceV25.cs index baff54c4..98e1a6e5 100644 --- a/DataImport.Web/Services/EdFiServiceV25.cs +++ b/DataImport.Web/Services/EdFiServiceV25.cs @@ -17,11 +17,11 @@ namespace DataImport.Web.Services { public class EdFiServiceV25 : EdFiServiceBase { - private readonly IOAuthRequestWrapper _oauthRequestWrapper; + private readonly IAuthRequestWrapper _oauthRequestWrapper; private readonly string _encryptionKey; private readonly IOptions _options; - public EdFiServiceV25(DataImportDbContext dbContext, IEncryptionKeyResolver encryptionKeyResolver, IMapper mapper, IOAuthRequestWrapper oauthRequestWrapper, IOptions options) + public EdFiServiceV25(DataImportDbContext dbContext, IEncryptionKeyResolver encryptionKeyResolver, IMapper mapper, IAuthRequestWrapper oauthRequestWrapper, IOptions options) : base(mapper, dbContext) { _encryptionKey = encryptionKeyResolver.GetEncryptionKey(); diff --git a/DataImport.Web/Services/EdFiServiceV311.cs b/DataImport.Web/Services/EdFiServiceV311.cs index cadc3377..bc53d96b 100644 --- a/DataImport.Web/Services/EdFiServiceV311.cs +++ b/DataImport.Web/Services/EdFiServiceV311.cs @@ -21,11 +21,11 @@ public class EdFiServiceV311 : EdFiServiceBase { private readonly ISwaggerMetadataFetcher _metadataFetcher; private readonly Dictionary _yearSpecificYearCache = new Dictionary(); - private readonly IOAuthRequestWrapper _oauthRequestWrapper; + private readonly IAuthRequestWrapper _oauthRequestWrapper; private string _encryptionKey; private readonly IOptions _options; - public EdFiServiceV311(DataImportDbContext dbContext, IEncryptionKeyResolver encryptionKeyResolver, IMapper mapper, ISwaggerMetadataFetcher metadataFetcher, IOAuthRequestWrapper oauthRequestWrapper, IOptions options) + public EdFiServiceV311(DataImportDbContext dbContext, IEncryptionKeyResolver encryptionKeyResolver, IMapper mapper, ISwaggerMetadataFetcher metadataFetcher, IAuthRequestWrapper oauthRequestWrapper, IOptions options) : base(mapper, dbContext) { _metadataFetcher = metadataFetcher; diff --git a/DataImport.Web/Startup.cs b/DataImport.Web/Startup.cs index 8ff63db0..90e1c471 100644 --- a/DataImport.Web/Startup.cs +++ b/DataImport.Web/Startup.cs @@ -118,7 +118,10 @@ public void ConfigureServices(IServiceCollection services) .Value.DefaultConnection)); } - services.AddTransient(); + if (bool.Parse(Configuration["AppSettings:UseBasicAuthentication"])) + services.AddTransient(); + else + services.AddTransient(); //Configure MVC Razor Views under "FeatureFolder" and with compilation services.Configure(options => options.ViewLocationExpanders.Add(new FeatureViewLocationExpander())) diff --git a/DataImport.Web/appsettings.json b/DataImport.Web/appsettings.json index ad7e90ae..34e53c28 100644 --- a/DataImport.Web/appsettings.json +++ b/DataImport.Web/appsettings.json @@ -11,7 +11,8 @@ "AllowUserRegistration": true, "UserRecoveryToken": "", "PathBase": "", - "IgnoresCertificateErrors": false + "IgnoresCertificateErrors": false, + "UseBasicAuthentication": false }, "IdentitySettings": { "Type": "EntityFramework",