hack: disable authorization + add fake identity service which takes userID straight from the header
This commit is contained in:
		
							parent
							
								
									eef41bbf8a
								
							
						
					
					
						commit
						792866c8d5
					
				| @ -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); | ||||||
|  | |||||||
| @ -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; | ||||||
|  | //     } | ||||||
|  | // } | ||||||
|  | 
 | ||||||
|  | |||||||
| @ -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(); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -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; | ||||||
|  | //     } | ||||||
|  | // } | ||||||
|  | |||||||
| @ -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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user