From bab2603326dbce4fa77e64f0350b5b698e976014 Mon Sep 17 00:00:00 2001 From: Roger Torkelsen Date: Tue, 1 Oct 2024 13:43:31 +0200 Subject: [PATCH] Simple graph integration to get user data + minor cleanup --- .../Controllers/ItemController.cs | 12 ++--- .../Controllers/UserController.cs | 44 ++++++---------- .../HandlenettAPI/Helpers/AzureSQLSetup.cs | 3 -- .../web-api/HandlenettAPI/Program.cs | 26 ++++------ .../HandlenettAPI/Services/AzureSQLContext.cs | 2 +- .../HandlenettAPI/Services/CosmosDBService.cs | 3 +- .../HandlenettAPI/Services/UserService.cs | 50 +++++++++++++++++-- 7 files changed, 80 insertions(+), 60 deletions(-) diff --git a/handlenett-backend/web-api/HandlenettAPI/Controllers/ItemController.cs b/handlenett-backend/web-api/HandlenettAPI/Controllers/ItemController.cs index 5f39e6b..5336a83 100644 --- a/handlenett-backend/web-api/HandlenettAPI/Controllers/ItemController.cs +++ b/handlenett-backend/web-api/HandlenettAPI/Controllers/ItemController.cs @@ -18,7 +18,7 @@ public class ItemController : ControllerBase { private readonly ILogger _logger; private readonly IConfiguration _config; - private readonly CosmosDBService _cosmosDBService; + private readonly CosmosDBService _cosmosDBService; public ItemController(ILogger logger, IConfiguration config) { @@ -48,9 +48,9 @@ public async Task>> Get() { try { - var sqlQuery = "SELECT * FROM c"; //ORDER BY c.created ? feiler dersom ingen items - var result = await _cosmosDBService.GetByQuery(sqlQuery); - return Ok(result); + var sqlQuery = "SELECT * FROM c"; //ORDER BY c.created ? feiler dersom ingen items + var result = await _cosmosDBService.GetByQuery(sqlQuery); + return Ok(result); } catch (Exception ex) { @@ -84,8 +84,8 @@ public async Task Add(ItemPostDTO item) { try { - var result = await _cosmosDBService.Add(item, GetUsername()); - return Ok(result); + var result = await _cosmosDBService.Add(item, GetUsername()); + return Ok(result); } catch (Exception ex) { diff --git a/handlenett-backend/web-api/HandlenettAPI/Controllers/UserController.cs b/handlenett-backend/web-api/HandlenettAPI/Controllers/UserController.cs index 7513a8b..8c39718 100644 --- a/handlenett-backend/web-api/HandlenettAPI/Controllers/UserController.cs +++ b/handlenett-backend/web-api/HandlenettAPI/Controllers/UserController.cs @@ -1,10 +1,13 @@ -using HandlenettAPI.Models; +using Azure.Identity; +using HandlenettAPI.Models; using HandlenettAPI.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Graph; namespace HandlenettAPI.Controllers { + [Authorize] [ApiController] [Route("[controller]")] public class UserController : ControllerBase @@ -23,33 +26,18 @@ public UserController(ILogger logger, GraphServiceClient graphSe [HttpGet(Name = "GetUsers")] public async Task> Get() { - //Get all users, mÃ¥ bare endre til dto - var dbService = new UserService(_config); - - //not ready, msal auth issue in graph api - await dbService.AddUserIfNotExists(_graphClient); - - return dbService.GetUsers(); - + try + { + var dbService = new UserService(_config); + await dbService.AddUserIfNotExists(_graphClient); + + return dbService.GetUsers(); //endre til dto + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to get users"); + throw new Exception("Failed to get users"); + } } - - - //[HttpGet(Name = "GetUser")] - //public async Task Get() - //{ - // return await _graphServiceClient.Me.Request().GetAsync(); ; - //} - - //[HttpGet("Login")] - //public async Task Login() - //{ - // return true; - //} - - //[HttpGet("Logout")] - //public async Task Logout() - //{ - // return true; - //} } } diff --git a/handlenett-backend/web-api/HandlenettAPI/Helpers/AzureSQLSetup.cs b/handlenett-backend/web-api/HandlenettAPI/Helpers/AzureSQLSetup.cs index 6ebdfef..d374bac 100644 --- a/handlenett-backend/web-api/HandlenettAPI/Helpers/AzureSQLSetup.cs +++ b/handlenett-backend/web-api/HandlenettAPI/Helpers/AzureSQLSetup.cs @@ -2,12 +2,9 @@ { public static class AzureSQLSetup { - private static bool IsRunningInAzure() { return Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID") != null; } - - } } diff --git a/handlenett-backend/web-api/HandlenettAPI/Program.cs b/handlenett-backend/web-api/HandlenettAPI/Program.cs index c7bb07b..9da99a5 100644 --- a/handlenett-backend/web-api/HandlenettAPI/Program.cs +++ b/handlenett-backend/web-api/HandlenettAPI/Program.cs @@ -3,18 +3,17 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.Azure; +using Microsoft.Graph; using Microsoft.Graph.ExternalConnectors; using Microsoft.Identity.Web; using Microsoft.OpenApi.Models; -var builder = WebApplication.CreateBuilder(args); +var builder = Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(args); // Swagger configuration builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Handlenett API", Version = "v1" }); - - // Add JWT Authentication c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Name = "Authorization", @@ -24,7 +23,6 @@ In = ParameterLocation.Header, Description = "Enter 'Bearer' [space] and then your token in the text input below.\n\nExample: \"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI...\"", }); - c.AddSecurityRequirement(new OpenApiSecurityRequirement { { @@ -44,9 +42,12 @@ // Add services to the container. builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd")) - .EnableTokenAcquisitionToCallDownstreamApi() // new[] { "https://graph.microsoft.com/.default" } - .AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph")) - .AddInMemoryTokenCaches(); + .EnableTokenAcquisitionToCallDownstreamApi() + .AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph")) + .AddInMemoryTokenCaches(); + +//TODO: add AzureSQLContext singleton +//builder.Services.AddSingleton(graphClient); builder.Configuration.AddAzureKeyVault( new Uri($"https://{builder.Configuration["AzureKeyVaultNameProd"]}.vault.azure.net/"), @@ -54,13 +55,10 @@ //DefaultAzureCredential() is handled by enabling system assigned identity on container app and creating access policy in kv builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); - - - +//TODO: Fiks dev miljø for key vault //if (builder.Environment.IsProduction()) //{ // builder.Configuration.AddAzureKeyVault( @@ -76,12 +74,9 @@ var app = builder.Build(); - -// Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); - //app.UseSwaggerUI(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API v1"); @@ -91,11 +86,8 @@ } app.UseHttpsRedirection(); - app.UseAuthentication(); - app.UseAuthorization(); - app.MapControllers(); ConfigurationHelper.Initialize(app.Configuration); diff --git a/handlenett-backend/web-api/HandlenettAPI/Services/AzureSQLContext.cs b/handlenett-backend/web-api/HandlenettAPI/Services/AzureSQLContext.cs index 3bfda8f..5840acd 100644 --- a/handlenett-backend/web-api/HandlenettAPI/Services/AzureSQLContext.cs +++ b/handlenett-backend/web-api/HandlenettAPI/Services/AzureSQLContext.cs @@ -80,7 +80,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) } else { - connectionString = _config.GetConnectionString("AzureSQLDB_Local"); + connectionString = _config.GetConnectionString("AzureSQLDBForLocalTesting"); var azureCredential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { diff --git a/handlenett-backend/web-api/HandlenettAPI/Services/CosmosDBService.cs b/handlenett-backend/web-api/HandlenettAPI/Services/CosmosDBService.cs index 6cd776a..9d999ae 100644 --- a/handlenett-backend/web-api/HandlenettAPI/Services/CosmosDBService.cs +++ b/handlenett-backend/web-api/HandlenettAPI/Services/CosmosDBService.cs @@ -10,6 +10,7 @@ namespace HandlenettAPI.Services public class CosmosDBService// where T : IBase //Lag generisk, støtte for andre enn Item struktur, basert pÃ¥ containerName? { private readonly Container _container; + public CosmosDBService(CosmosClient client, string databaseName, string containerName) { _container = client.GetContainer(databaseName, containerName); @@ -77,4 +78,4 @@ public async Task Update (string id, ItemPutDTO item, string username) return updatedItem; } } -} +} \ No newline at end of file diff --git a/handlenett-backend/web-api/HandlenettAPI/Services/UserService.cs b/handlenett-backend/web-api/HandlenettAPI/Services/UserService.cs index a960778..f382841 100644 --- a/handlenett-backend/web-api/HandlenettAPI/Services/UserService.cs +++ b/handlenett-backend/web-api/HandlenettAPI/Services/UserService.cs @@ -1,5 +1,4 @@ -using HandlenettAPI.Models; -using Microsoft.Data.SqlClient; +using Microsoft.Data.SqlClient; using Microsoft.Graph; namespace HandlenettAPI.Services @@ -8,7 +7,7 @@ namespace HandlenettAPI.Services public class UserService { private IConfiguration _config; - public UserService(IConfiguration config) + public UserService(IConfiguration config) { _config = config; } @@ -37,9 +36,52 @@ public UserService(IConfiguration config) public async Task AddUserIfNotExists(GraphServiceClient graphServiceClient) { - await graphServiceClient.Me.Request().GetAsync(); + var myUser = await graphServiceClient.Me.Request().GetAsync(); + + if (myUser == null) + { + throw new Exception("Could not get Ad profile"); + } + + if (!CheckIfUserExists(new Guid(myUser.Id))) + { + AddUser(myUser); + } return new Models.User(); } + + + private bool CheckIfUserExists(Guid userId) + { + using (var db = new AzureSQLContext(_config)) + { + var users = db.Users + .Where(u => u.Id == userId) + .FirstOrDefault(); + + if (users != null) + { + return true; + } + return false; + } + } + + private bool AddUser(Microsoft.Graph.User user) + { + using (var db = new AzureSQLContext(_config)) + { + var newUser = new Models.User + { + Id = new Guid(user.Id), + FirstName = user.GivenName, + LastName = user.Surname + }; + db.Users.Add(newUser); + db.SaveChanges(); + return true; + } + } } }