Browse Source

hack: disable authorization + add fake identity service which takes userID straight from the header

pull/2051/head
kct949 2 years ago
parent
commit
792866c8d5
5 changed files with 74 additions and 13 deletions
  1. +6
    -2
      src/Services/Basket/Basket.API/Controllers/BasketController.cs
  2. +28
    -4
      src/Services/Basket/Basket.API/Services/IdentityService.cs
  3. +3
    -1
      src/Services/Basket/Basket.API/Startup.cs
  4. +34
    -5
      src/Services/Ordering/Ordering.API/Infrastructure/Services/IdentityService.cs
  5. +3
    -1
      src/Services/Ordering/Ordering.API/Startup.cs

+ 6
- 2
src/Services/Basket/Basket.API/Controllers/BasketController.cs View File

@ -1,6 +1,7 @@
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers; namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
// HACK: no auth
// [Authorize] // [Authorize]
[ApiController] [ApiController]
public class BasketController : ControllerBase public class BasketController : ControllerBase
@ -56,8 +57,11 @@ public class BasketController : ControllerBase
return BadRequest(); return BadRequest();
} }
var userName = this.HttpContext.User.FindFirst(x => x.Type == ClaimTypes.Name).Value;
// HACK: no auth
// Authorization is disabled so the Name claim will not be available
// var userName = this.HttpContext.User.FindFirst(x => x.Type == ClaimTypes.Name).Value;
var userName = "Dummy User Name";
var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, userName, basketCheckout.City, basketCheckout.Street, var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, userName, basketCheckout.City, basketCheckout.Street,
basketCheckout.State, basketCheckout.Country, basketCheckout.ZipCode, basketCheckout.CardNumber, basketCheckout.CardHolderName, basketCheckout.State, basketCheckout.Country, basketCheckout.ZipCode, basketCheckout.CardNumber, basketCheckout.CardHolderName,
basketCheckout.CardExpiration, basketCheckout.CardSecurityNumber, basketCheckout.CardTypeId, basketCheckout.Buyer, basketCheckout.RequestId, basket); basketCheckout.CardExpiration, basketCheckout.CardSecurityNumber, basketCheckout.CardTypeId, basketCheckout.Buyer, basketCheckout.RequestId, basket);


+ 28
- 4
src/Services/Basket/Basket.API/Services/IdentityService.cs View File

@ -1,17 +1,41 @@
namespace Microsoft.eShopOnContainers.Services.Basket.API.Services; namespace Microsoft.eShopOnContainers.Services.Basket.API.Services;
public class IdentityService : IIdentityService
public class IdentityServiceFake : IIdentityService
{ {
private IHttpContextAccessor _context; private IHttpContextAccessor _context;
public IdentityService(IHttpContextAccessor context)
public IdentityServiceFake(IHttpContextAccessor context)
{ {
_context = context ?? throw new ArgumentNullException(nameof(context)); _context = context ?? throw new ArgumentNullException(nameof(context));
} }
public string GetUserIdentity() public string GetUserIdentity()
{ {
return _context.HttpContext.User.FindFirst("sub").Value;
if (_context.HttpContext
.Request
.Headers
.TryGetValue("user-id", out var value))
{
return value.Single();
}
return null;
} }
} }
// HACK: no auth
// public class IdentityService : IIdentityService
// {
// private IHttpContextAccessor _context;
//
// public IdentityService(IHttpContextAccessor context)
// {
// _context = context ?? throw new ArgumentNullException(nameof(context));
// }
//
// public string GetUserIdentity()
// {
// return _context.HttpContext.User.FindFirst("sub").Value;
// }
// }

+ 3
- 1
src/Services/Basket/Basket.API/Startup.cs View File

@ -135,7 +135,9 @@ public class Startup
}); });
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IBasketRepository, RedisBasketRepository>(); services.AddTransient<IBasketRepository, RedisBasketRepository>();
services.AddTransient<IIdentityService, IdentityService>();
// HACK: no auth
// services.AddTransient<IIdentityService, IdentityService>();
services.AddTransient<IIdentityService, IdentityServiceFake>();
services.AddOptions(); services.AddOptions();


+ 34
- 5
src/Services/Ordering/Ordering.API/Infrastructure/Services/IdentityService.cs View File

@ -1,21 +1,50 @@
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services; namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
public class IdentityService : IIdentityService
public class IdentityServiceFake : IIdentityService
{ {
private IHttpContextAccessor _context; private IHttpContextAccessor _context;
public IdentityService(IHttpContextAccessor context)
public IdentityServiceFake(IHttpContextAccessor context)
{ {
_context = context ?? throw new ArgumentNullException(nameof(context)); _context = context ?? throw new ArgumentNullException(nameof(context));
} }
public string GetUserIdentity() public string GetUserIdentity()
{ {
return _context.HttpContext.User.FindFirst("sub").Value;
if (_context.HttpContext
.Request
.Headers
.TryGetValue("user-id", out var value))
{
return value.Single();
}
return null;
} }
public string GetUserName() public string GetUserName()
{ {
return _context.HttpContext.User.Identity.Name;
return "Dummy User Name";
} }
} }
// HACK: no auth
// public class IdentityService : IIdentityService
// {
// private IHttpContextAccessor _context;
//
// public IdentityService(IHttpContextAccessor context)
// {
// _context = context ?? throw new ArgumentNullException(nameof(context));
// }
//
// public string GetUserIdentity()
// {
// return _context.HttpContext.User.FindFirst("sub").Value;
// }
//
// public string GetUserName()
// {
// return _context.HttpContext.User.Identity.Name;
// }
// }

+ 3
- 1
src/Services/Ordering/Ordering.API/Startup.cs View File

@ -250,7 +250,9 @@ static class CustomExtensionsMethods
public static IServiceCollection AddCustomIntegrations(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddCustomIntegrations(this IServiceCollection services, IConfiguration configuration)
{ {
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IIdentityService, IdentityService>();
// HACK: no auth
// services.AddTransient<IIdentityService, IdentityService>();
services.AddTransient<IIdentityService, IdentityServiceFake>();
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>( services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
sp => (DbConnection c) => new IntegrationEventLogService(c)); sp => (DbConnection c) => new IntegrationEventLogService(c));


Loading…
Cancel
Save