From bda1e81c87a34bc0e6d2ce805f706a726087e957 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sun, 30 Jan 2022 01:30:58 +0100 Subject: feat: WIP: Map github logins When github is used for login, we want to create a mapping to a regular user in our database. This is mainly so that we don't have to change the PK in our database or add a column to it. --- src/server/Api/Internal/Account/GetClaimsRoute.cs | 16 ++++++++++ .../Api/Internal/Account/GetProfileDataRoute.cs | 35 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/server/Api/Internal/Account/GetClaimsRoute.cs (limited to 'src/server/Api/Internal/Account') diff --git a/src/server/Api/Internal/Account/GetClaimsRoute.cs b/src/server/Api/Internal/Account/GetClaimsRoute.cs new file mode 100644 index 0000000..fde8887 --- /dev/null +++ b/src/server/Api/Internal/Account/GetClaimsRoute.cs @@ -0,0 +1,16 @@ +namespace IOL.BookmarkThing.Server.Api.Internal.Account; + +public class GetClaimsRoute : RouteBaseInternalSync.WithoutRequest.WithActionResult +{ + [HttpGet("~/v{apiVersion:apiVersion}/account/claims")] + public override ActionResult Handle() { + if (HttpContext.Request.Query.ContainsKey("download")) { + var serializerOptions = new JsonSerializerOptions() { + ReferenceHandler = ReferenceHandler.IgnoreCycles + }; + return File(JsonSerializer.SerializeToUtf8Bytes(User.Claims, serializerOptions), "application/json", "claims_" + User.Identity?.Name + ".json"); + } + + return Ok(User.Claims); + } +} diff --git a/src/server/Api/Internal/Account/GetProfileDataRoute.cs b/src/server/Api/Internal/Account/GetProfileDataRoute.cs index adf1cba..c68f295 100644 --- a/src/server/Api/Internal/Account/GetProfileDataRoute.cs +++ b/src/server/Api/Internal/Account/GetProfileDataRoute.cs @@ -2,10 +2,45 @@ namespace IOL.BookmarkThing.Server.Api.Internal.Account; public class GetProfileDataRoute : RouteBaseInternalSync.WithoutRequest.WithActionResult { + private readonly AppDbContext _context; + private readonly ILogger _logger; + + public GetProfileDataRoute(ILogger logger, AppDbContext context) { + _logger = logger; + _context = context; + } + [ApiVersionNeutral] [ApiExplorerSettings(IgnoreApi = true)] [HttpGet("~/v{version:apiVersion}/account/profile-data")] public override ActionResult Handle() { + // if (!Guid.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier), out var _)) { + // var github_id = User.FindFirstValue(AppClaims.GITHUB_ID); + // if (github_id.HasValue()) { + // var existing_mapping = _context.GithubUserMappings.Include(c => c.User).SingleOrDefault(c => c.GithubId == github_id); + // var id = new ClaimsIdentity(); + // if (existing_mapping != default) { + // id.AddClaims(existing_mapping.User.DefaultClaims()); + // User.AddIdentity(id); + // } else { + // var name = User.FindFirstValue(ClaimTypes.Name); + // var user = new User(name) { + // Id = Guid.NewGuid() + // }; + // var mapping = new GithubUserMapping { + // GithubId = github_id, + // User = user + // }; + // _context.GithubUserMappings.Add(mapping); + // _context.SaveChanges(); + // id.AddClaims(mapping.User.DefaultClaims()); + // User.AddIdentity(id); + // } + // + // _logger.LogInformation("Added user mapping for github user"); + // } + // } + return Ok(LoggedInUser); } } -- cgit v1.3