diff --git a/Program.cs b/Program.cs index a174956..ef0308c 100644 --- a/Program.cs +++ b/Program.cs @@ -1,7 +1,19 @@ +using budgetbud.Services; + var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddSwaggerGen(); +if (builder.Environment.IsDevelopment()) +{ + builder.Services.AddScoped(); +} +else +{ + builder.Services.AddScoped(); +} + + var app = builder.Build(); app.UseDefaultFiles(); diff --git a/api/services/azure_identity_service.cs b/api/services/azure_identity_service.cs index e69de29..eb346f4 100644 --- a/api/services/azure_identity_service.cs +++ b/api/services/azure_identity_service.cs @@ -0,0 +1,47 @@ +using System.Diagnostics; +using System.Text.Json; +using budgetbud.Services; + +namespace budgetbud.Services; + +public class AzureIdentityService : IIdentityService +{ + private readonly IHttpContextAccessor _httpContextAccessor; + + public AzureIdentityService(IHttpContextAccessor httpContextAccessor) + { + _httpContextAccessor = httpContextAccessor; + } + + public string GetUserIdentity() + { + if (_httpContextAccessor.HttpContext != null && _httpContextAccessor.HttpContext!.Request.Headers.TryGetValue("X-MS-CLIENT-PRINCIPAL-ID", out var userId)) + { + string decoded_userId = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(userId!)); + dynamic claims = JsonSerializer.Deserialize(decoded_userId) ?? throw new Exception("Claims not valid JSON"); + string provider = claims.provider_name; + switch (provider) + { + case "github": + return ProcessGithub(claims); + default: + throw new Exception("Provider not supported"); + } + } + + throw new Exception("X-MS-CLIENT-PRINCIPAL-ID header not found"); + } + + private static string ProcessGithub(dynamic json) + { + string? claims = json.user_claims; + foreach (dynamic claim in claims) + { + if (claim.typ == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier") + { + return "github:" + claim.val; + } + } + throw new Exception("Name claim not found"); + } +} \ No newline at end of file diff --git a/api/services/fake_identity_service.cs b/api/services/fake_identity_service.cs index e69de29..29f703d 100644 --- a/api/services/fake_identity_service.cs +++ b/api/services/fake_identity_service.cs @@ -0,0 +1,21 @@ +namespace budgetbud.Services; + +public class FakeIdentityService : IIdentityService +{ + private readonly IWebHostEnvironment _environment; + + public FakeIdentityService(IWebHostEnvironment environment) + { + _environment = environment; + } + + public string GetUserIdentity() + { + if (_environment.IsProduction()) + { + throw new Exception("FakeIdentityService should not be used in production"); + } + return "fake_user:123"; + } + +} \ No newline at end of file diff --git a/api/user_data_controller.cs b/api/user_data_controller.cs index 96d72ad..451878a 100644 --- a/api/user_data_controller.cs +++ b/api/user_data_controller.cs @@ -1,3 +1,4 @@ +using budgetbud.Services; using Microsoft.AspNetCore.Mvc; namespace budgetbud.Controllers; @@ -6,6 +7,13 @@ namespace budgetbud.Controllers; [Route("api/[controller]")] public class UserDataController : ControllerBase { + private readonly IIdentityService _identityService; + + public UserDataController(IIdentityService identityService) + { + _identityService = identityService; + } + [HttpGet("hello-world")] public IActionResult Get() { @@ -15,15 +23,13 @@ public IActionResult Get() [HttpGet("user-id")] public IActionResult GetUserId() { - if (HttpContext.Request.Headers.TryGetValue("X-MS-CLIENT-PRINCIPAL", out var clientPrincipal)) + try { - // Use the clientPrincipal value here - var decodedClientPrincipal = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(clientPrincipal)); - return Ok(decodedClientPrincipal); + return Ok(_identityService.GetUserIdentity()); } - else + catch (Exception e) { - return BadRequest("X-MS-CLIENT-PRINCIPAL header not found"); + return BadRequest(e.Message); } } } \ No newline at end of file