Skip to content

Commit

Permalink
Add Identity Service
Browse files Browse the repository at this point in the history
  • Loading branch information
ParadoxZero committed Jul 24, 2024
1 parent 7dcc8b3 commit 318aa93
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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<IIdentityService, FakeIdentityService>();
}
else
{
builder.Services.AddScoped<IIdentityService, AzureIdentityService>();
}


var app = builder.Build();

app.UseDefaultFiles();
Expand Down
47 changes: 47 additions & 0 deletions api/services/azure_identity_service.cs
Original file line number Diff line number Diff line change
@@ -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<dynamic>(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");
}
}
21 changes: 21 additions & 0 deletions api/services/fake_identity_service.cs
Original file line number Diff line number Diff line change
@@ -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";
}

}
18 changes: 12 additions & 6 deletions api/user_data_controller.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using budgetbud.Services;
using Microsoft.AspNetCore.Mvc;

namespace budgetbud.Controllers;
Expand All @@ -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()
{
Expand All @@ -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);
}
}
}

0 comments on commit 318aa93

Please sign in to comment.