diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-01-30 01:30:58 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-01-30 01:30:58 +0100 |
| commit | bda1e81c87a34bc0e6d2ce805f706a726087e957 (patch) | |
| tree | 124ebf09d1289900e0d67d8723528b96bfbb6a7e /src/server/Api | |
| parent | 253c8479b9ae0ba6853a70728d3f6e904e1ac2ba (diff) | |
| download | bookmark-thing-bda1e81c87a34bc0e6d2ce805f706a726087e957.tar.xz bookmark-thing-bda1e81c87a34bc0e6d2ce805f706a726087e957.zip | |
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.
Diffstat (limited to 'src/server/Api')
| -rw-r--r-- | src/server/Api/Internal/Account/GetClaimsRoute.cs | 16 | ||||
| -rw-r--r-- | src/server/Api/Internal/Account/GetProfileDataRoute.cs | 35 | ||||
| -rw-r--r-- | src/server/Api/Internal/BaseInternalRoute.cs | 3 | ||||
| -rw-r--r-- | src/server/Api/Internal/LoggedInInternalUser.cs | 2 | ||||
| -rw-r--r-- | src/server/Api/Internal/OAuthCallbackRoute.cs | 1 | ||||
| -rw-r--r-- | src/server/Api/V1/BaseV1Route.cs | 5 |
6 files changed, 57 insertions, 5 deletions
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<LoggedInInternalUser> { + private readonly AppDbContext _context; + private readonly ILogger<GetProfileDataRoute> _logger; + + public GetProfileDataRoute(ILogger<GetProfileDataRoute> logger, AppDbContext context) { + _logger = logger; + _context = context; + } + [ApiVersionNeutral] [ApiExplorerSettings(IgnoreApi = true)] [HttpGet("~/v{version:apiVersion}/account/profile-data")] public override ActionResult<LoggedInInternalUser> 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); } } diff --git a/src/server/Api/Internal/BaseInternalRoute.cs b/src/server/Api/Internal/BaseInternalRoute.cs index 2f92c8e..6c0a2d9 100644 --- a/src/server/Api/Internal/BaseInternalRoute.cs +++ b/src/server/Api/Internal/BaseInternalRoute.cs @@ -5,11 +5,12 @@ namespace IOL.BookmarkThing.Server.Api.Internal; [ApiController] public class BaseInternalRoute : ControllerBase { + /// <summary> /// User data for the currently logged on user. /// </summary> protected LoggedInInternalUser LoggedInUser => new() { + Id = User.FindFirstValue(ClaimTypes.NameIdentifier).ToGuid(), Username = User.Identity?.Name, - Id = User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value.ToGuid() ?? default }; } diff --git a/src/server/Api/Internal/LoggedInInternalUser.cs b/src/server/Api/Internal/LoggedInInternalUser.cs index e08dd51..36906ca 100644 --- a/src/server/Api/Internal/LoggedInInternalUser.cs +++ b/src/server/Api/Internal/LoggedInInternalUser.cs @@ -2,6 +2,6 @@ namespace IOL.BookmarkThing.Server.Api.Internal; public class LoggedInInternalUser { - public Guid Id { get; set; } + public Guid? Id { get; set; } public string Username { get; set; } } diff --git a/src/server/Api/Internal/OAuthCallbackRoute.cs b/src/server/Api/Internal/OAuthCallbackRoute.cs index 156ff11..d2823c4 100644 --- a/src/server/Api/Internal/OAuthCallbackRoute.cs +++ b/src/server/Api/Internal/OAuthCallbackRoute.cs @@ -13,6 +13,7 @@ public class OAuthCallbackRoute : RouteBaseInternalSync.WithoutRequest.WithActio [ApiExplorerSettings(IgnoreApi = true)] [HttpGet("~/oauth-cb")] public override ActionResult Handle() { + Console.WriteLine(JsonSerializer.Serialize(HttpContext.User)); return Redirect(_configuration.GetValue<string>("FRONTEND_CANONICAL_URL")); } } diff --git a/src/server/Api/V1/BaseV1Route.cs b/src/server/Api/V1/BaseV1Route.cs index 21c8128..9322bf9 100644 --- a/src/server/Api/V1/BaseV1Route.cs +++ b/src/server/Api/V1/BaseV1Route.cs @@ -13,8 +13,8 @@ public class BaseV1Route : ControllerBase /// User data for the currently logged on user. /// </summary> protected LoggedInV1User LoggedInUser => new() { + Id = User.FindFirstValue(ClaimTypes.NameIdentifier).ToGuid(), Username = User.Identity?.Name, - Id = User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value.ToGuid() ?? default }; protected bool IsApiCall() { @@ -22,8 +22,7 @@ public class BaseV1Route : ControllerBase try { var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]); if (authHeader.Parameter == null) return false; - } catch (Exception e) { - Console.WriteLine(e); + } catch { return false; } |
