From c9484a0834b5294dde7add874b809ea3a19798c5 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Mon, 12 Jun 2017 20:18:11 +0200 Subject: [PATCH 1/7] Modify test. UserId is a Guid type --- .../Locations/LocationsScenarioBase.cs | 2 +- .../Services/Locations/LocationsScenarios.cs | 7 +++--- .../Locations/LocationsTestsStartup.cs | 22 ++++++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/test/Services/IntegrationTests/Services/Locations/LocationsScenarioBase.cs b/test/Services/IntegrationTests/Services/Locations/LocationsScenarioBase.cs index be3b23803..e74781e63 100644 --- a/test/Services/IntegrationTests/Services/Locations/LocationsScenarioBase.cs +++ b/test/Services/IntegrationTests/Services/Locations/LocationsScenarioBase.cs @@ -27,7 +27,7 @@ namespace IntegrationTests.Services.Locations return $"api/v1/locations/{id}"; } - public static string UserLocationBy(int id) + public static string UserLocationBy(Guid id) { return $"api/v1/locations/user/{id}"; } diff --git a/test/Services/IntegrationTests/Services/Locations/LocationsScenarios.cs b/test/Services/IntegrationTests/Services/Locations/LocationsScenarios.cs index 63437e7dc..ae69219f5 100644 --- a/test/Services/IntegrationTests/Services/Locations/LocationsScenarios.cs +++ b/test/Services/IntegrationTests/Services/Locations/LocationsScenarios.cs @@ -7,6 +7,7 @@ using System.Net.Http; using System.Text; using System.Threading.Tasks; using Xunit; +using System; namespace IntegrationTests.Services.Locations { @@ -18,7 +19,7 @@ namespace IntegrationTests.Services.Locations { using (var server = CreateServer()) { - var userId = 1234; + var userId = new Guid("4611ce3f-380d-4db5-8d76-87a8689058ed"); var content = new StringContent(BuildLocationsRequest(-122.315752, 47.604610), UTF8Encoding.UTF8, "application/json"); // Expected result @@ -50,7 +51,7 @@ namespace IntegrationTests.Services.Locations { using (var server = CreateServer()) { - var userId = 1234; + var userId = new Guid("4611ce3f-380d-4db5-8d76-87a8689058ed"); var content = new StringContent(BuildLocationsRequest(-122.119998, 47.690876), UTF8Encoding.UTF8, "application/json"); // Expected result @@ -82,7 +83,7 @@ namespace IntegrationTests.Services.Locations { using (var server = CreateServer()) { - var userId = 1234; + var userId = new Guid("4611ce3f-380d-4db5-8d76-87a8689058ed"); var content = new StringContent(BuildLocationsRequest(-121.040360, 48.091631), UTF8Encoding.UTF8, "application/json"); // Expected result diff --git a/test/Services/IntegrationTests/Services/Locations/LocationsTestsStartup.cs b/test/Services/IntegrationTests/Services/Locations/LocationsTestsStartup.cs index 0e622d854..28a51456b 100644 --- a/test/Services/IntegrationTests/Services/Locations/LocationsTestsStartup.cs +++ b/test/Services/IntegrationTests/Services/Locations/LocationsTestsStartup.cs @@ -3,7 +3,10 @@ using IntegrationTests.Middleware; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.Http; using Microsoft.eShopOnContainers.Services.Locations.API; + using System.Security.Claims; + using System.Threading.Tasks; public class LocationsTestsStartup : Startup { @@ -15,12 +18,29 @@ { if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant()) { - app.UseMiddleware(); + app.UseMiddleware(); } else { base.ConfigureAuth(app); } } + + class LocationAuthorizeMiddleware + { + private readonly RequestDelegate _next; + public LocationAuthorizeMiddleware(RequestDelegate rd) + { + _next = rd; + } + + public async Task Invoke(HttpContext httpContext) + { + var identity = new ClaimsIdentity("cookies"); + identity.AddClaim(new Claim("sub", "4611ce3f-380d-4db5-8d76-87a8689058ed")); + httpContext.User.AddIdentity(identity); + await _next.Invoke(httpContext); + } + } } } From c9332396c5723663fc0c90d736487e2faa60e7f3 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Mon, 12 Jun 2017 20:18:52 +0200 Subject: [PATCH 2/7] Modify location.API change userId type from int to Guid --- .../Controllers/LocationsController.cs | 6 +++--- .../Repositories/ILocationsRepository.cs | 2 +- .../Repositories/LocationsRepository.cs | 2 +- .../Infrastructure/Services/ILocationsService.cs | 2 +- .../Infrastructure/Services/LocationsService.cs | 13 +++++++++---- .../Location/Locations.API/Model/UserLocation.cs | 2 +- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Services/Location/Locations.API/Controllers/LocationsController.cs b/src/Services/Location/Locations.API/Controllers/LocationsController.cs index 2d8b73e3c..71f21a721 100644 --- a/src/Services/Location/Locations.API/Controllers/LocationsController.cs +++ b/src/Services/Location/Locations.API/Controllers/LocationsController.cs @@ -21,11 +21,11 @@ namespace Locations.API.Controllers } //GET api/v1/[controller]/user/1 - [Route("user/{userId:int}")] + [Route("user/{userId:guid}")] [HttpGet] - public async Task GetUserLocation(int userId) + public async Task GetUserLocation(Guid userId) { - var userLocation = await _locationsService.GetUserLocation(userId); + var userLocation = await _locationsService.GetUserLocation(userId.ToString()); return Ok(userLocation); } diff --git a/src/Services/Location/Locations.API/Infrastructure/Repositories/ILocationsRepository.cs b/src/Services/Location/Locations.API/Infrastructure/Repositories/ILocationsRepository.cs index 91cb2c258..03e51b0f4 100644 --- a/src/Services/Location/Locations.API/Infrastructure/Repositories/ILocationsRepository.cs +++ b/src/Services/Location/Locations.API/Infrastructure/Repositories/ILocationsRepository.cs @@ -11,7 +11,7 @@ Task> GetLocationListAsync(); - Task GetUserLocationAsync(int userId); + Task GetUserLocationAsync(string userId); Task> GetCurrentUserRegionsListAsync(LocationRequest currentPosition); diff --git a/src/Services/Location/Locations.API/Infrastructure/Repositories/LocationsRepository.cs b/src/Services/Location/Locations.API/Infrastructure/Repositories/LocationsRepository.cs index 0d756ab5f..764962c8a 100644 --- a/src/Services/Location/Locations.API/Infrastructure/Repositories/LocationsRepository.cs +++ b/src/Services/Location/Locations.API/Infrastructure/Repositories/LocationsRepository.cs @@ -28,7 +28,7 @@ .FirstOrDefaultAsync(); } - public async Task GetUserLocationAsync(int userId) + public async Task GetUserLocationAsync(string userId) { var filter = Builders.Filter.Eq("UserId", userId); return await _context.UserLocation diff --git a/src/Services/Location/Locations.API/Infrastructure/Services/ILocationsService.cs b/src/Services/Location/Locations.API/Infrastructure/Services/ILocationsService.cs index c0e50169f..94c566664 100644 --- a/src/Services/Location/Locations.API/Infrastructure/Services/ILocationsService.cs +++ b/src/Services/Location/Locations.API/Infrastructure/Services/ILocationsService.cs @@ -9,7 +9,7 @@ { Task GetLocation(string locationId); - Task GetUserLocation(int id); + Task GetUserLocation(string id); Task> GetAllLocation(); diff --git a/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs b/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs index eaf89c03a..4b1e3d55d 100644 --- a/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs +++ b/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs @@ -23,9 +23,14 @@ return await _locationsRepository.GetAsync(locationId); } - public async Task GetUserLocation(int id) + public async Task GetUserLocation(string id) { - return await _locationsRepository.GetUserLocationAsync(id); + if (!Guid.TryParse(id, out Guid userId)) + { + throw new ArgumentException("Not valid userId"); + } + + return await _locationsRepository.GetUserLocationAsync(userId.ToString()); } public async Task> GetAllLocation() @@ -35,7 +40,7 @@ public async Task AddOrUpdateUserLocation(string id, LocationRequest currentPosition) { - if (!int.TryParse(id, out int userId)) + if (!Guid.TryParse(id, out Guid userId)) { throw new ArgumentException("Not valid userId"); } @@ -50,7 +55,7 @@ // If current area found, then update user location var locationAncestors = new List(); - var userLocation = await _locationsRepository.GetUserLocationAsync(userId); + var userLocation = await _locationsRepository.GetUserLocationAsync(userId.ToString()); userLocation = userLocation ?? new UserLocation(); userLocation.UserId = userId; userLocation.LocationId = currentUserAreaLocationList[0].Id; diff --git a/src/Services/Location/Locations.API/Model/UserLocation.cs b/src/Services/Location/Locations.API/Model/UserLocation.cs index e72d3e26e..1b7572426 100644 --- a/src/Services/Location/Locations.API/Model/UserLocation.cs +++ b/src/Services/Location/Locations.API/Model/UserLocation.cs @@ -9,7 +9,7 @@ [BsonIgnoreIfDefault] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } - public int UserId { get; set; } = 0; + public Guid UserId { get; set; } [BsonRepresentation(BsonType.ObjectId)] public string LocationId { get; set; } public DateTime UpdateDate { get; set; } From 4458e57734c32b1a368d506368c799300a76c013 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Mon, 12 Jun 2017 20:19:23 +0200 Subject: [PATCH 3/7] Add AllowAccessTokensViaBrowser in Identity server, we got an login error from xamarin --- src/Services/Identity/Identity.API/Configuration/Config.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Services/Identity/Identity.API/Configuration/Config.cs b/src/Services/Identity/Identity.API/Configuration/Config.cs index 2b78737a9..285b25193 100644 --- a/src/Services/Identity/Identity.API/Configuration/Config.cs +++ b/src/Services/Identity/Identity.API/Configuration/Config.cs @@ -76,7 +76,8 @@ namespace Identity.API.Configuration "locations" }, //Allow requesting refresh tokens for long lived API access - AllowOfflineAccess = true + AllowOfflineAccess = true, + AllowAccessTokensViaBrowser = true }, new Client { From ef8f14a2126027c7bd42a568d393270d58ea4310 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Mon, 12 Jun 2017 20:20:46 +0200 Subject: [PATCH 4/7] Add locations to allow scopes to dictionary --- .../eShopOnContainers.Core/Services/Identity/IdentityService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Identity/IdentityService.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Identity/IdentityService.cs index e144eb6ff..daeab7fec 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Identity/IdentityService.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Identity/IdentityService.cs @@ -16,7 +16,7 @@ namespace eShopOnContainers.Core.Services.Identity dic.Add("client_id", "xamarin"); dic.Add("client_secret", "secret"); dic.Add("response_type", "code id_token token"); - dic.Add("scope", "openid profile basket orders offline_access"); + dic.Add("scope", "openid profile basket orders locations offline_access"); dic.Add("redirect_uri", GlobalSetting.Instance.IdentityCallback); dic.Add("nonce", Guid.NewGuid().ToString("N")); From 3b5d081086d8947c6c6c43765c54c24967049201 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Mon, 12 Jun 2017 20:21:45 +0200 Subject: [PATCH 5/7] Add token when call location api --- .../Services/Location/ILocationService.cs | 2 +- .../Services/Location/LocationService.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Location/ILocationService.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Location/ILocationService.cs index bc2c59915..94bff221d 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Location/ILocationService.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Location/ILocationService.cs @@ -5,6 +5,6 @@ public interface ILocationService { - Task UpdateUserLocation(LocationRequest newLocReq); + Task UpdateUserLocation(LocationRequest newLocReq, string token); } } \ No newline at end of file diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Location/LocationService.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Location/LocationService.cs index 2149a5118..6c6a104c2 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Location/LocationService.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Location/LocationService.cs @@ -14,7 +14,7 @@ _requestProvider = requestProvider; } - public async Task UpdateUserLocation(LocationRequest newLocReq) + public async Task UpdateUserLocation(LocationRequest newLocReq, string token) { UriBuilder builder = new UriBuilder(GlobalSetting.Instance.LocationEndpoint); @@ -22,7 +22,7 @@ string uri = builder.ToString(); - var result = await _requestProvider.PostAsync(uri, newLocReq); + await _requestProvider.PostAsync(uri, newLocReq, token); } } } From 2283a9d12b58ecb892646e8e403cde5a18680b72 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Mon, 12 Jun 2017 20:22:38 +0200 Subject: [PATCH 6/7] Add userIsLogged contiditon in view for fake location --- .../ViewModels/LoginViewModel.cs | 4 +++- .../ViewModels/SettingsViewModel.cs | 5 ++++- .../eShopOnContainers.Core/Views/SettingsView.xaml | 12 +++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/LoginViewModel.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/LoginViewModel.cs index c174e36c8..95d2c8556 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/LoginViewModel.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/LoginViewModel.cs @@ -215,8 +215,10 @@ namespace eShopOnContainers.Core.ViewModels if(Settings.UseMocks) { Settings.AuthAccessToken = string.Empty; - Settings.AuthIdToken = string.Empty; + Settings.AuthIdToken = string.Empty; } + + Settings.UseFakeLocation = false; } private async Task NavigateAsync(string url) diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs index 8bfe9ed25..33188a96c 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs @@ -140,6 +140,8 @@ namespace eShopOnContainers.Core.ViewModels } } + public bool UserIsLogged => !string.IsNullOrEmpty(Settings.AuthAccessToken); + public ICommand ToggleMockServicesCommand => new Command(async () => await ToggleMockServicesAsync()); public ICommand ToggleFakeLocationCommand => new Command(() => ToggleFakeLocationAsync()); @@ -194,8 +196,9 @@ namespace eShopOnContainers.Core.ViewModels Latitude = _latitude, Longitude = _longitude }; + var authToken = Settings.AuthAccessToken; - await _locationService.UpdateUserLocation(locationRequest); + await _locationService.UpdateUserLocation(locationRequest, authToken); } private void UpdateInfo() diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Views/SettingsView.xaml b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Views/SettingsView.xaml index a8da35daa..e374af1a7 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Views/SettingsView.xaml +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Views/SettingsView.xaml @@ -163,7 +163,8 @@ Grid.ColumnSpan="2"/> + Grid.Row="4" + IsVisible="{Binding UserIsLogged}">