-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dcc8b3
commit 318aa93
Showing
4 changed files
with
92 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters