diff options
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/Utilities/HandleGithubCreatingTicket.cs | 64 |
1 files changed, 50 insertions, 14 deletions
diff --git a/src/server/Utilities/HandleGithubCreatingTicket.cs b/src/server/Utilities/HandleGithubCreatingTicket.cs index 3ead322..7b07306 100644 --- a/src/server/Utilities/HandleGithubCreatingTicket.cs +++ b/src/server/Utilities/HandleGithubCreatingTicket.cs @@ -5,26 +5,62 @@ namespace IOL.BookmarkThing.Server.Utilities; public static class HandleGithubCreatingTicket { - public static Task Handle(OAuthCreatingTicketContext context, IConfiguration configuration) { - var ghid = context.Identity?.FindFirst(p => p.Type == AppClaims.GITHUB_ID); - if (ghid == default) { - return default; + public static async Task Handle(OAuthCreatingTicketContext context, IConfiguration configuration) { + var githubId = context.Identity?.FindFirst(p => p.Type == ClaimTypes.NameIdentifier)?.Value; + var githubUsername = context.Identity?.FindFirst(p => p.Type == ClaimTypes.Name)?.Value; + + if (githubId.IsNullOrWhiteSpace() || githubUsername.IsNullOrWhiteSpace() || context.Identity == default) { + return; + } + + var claims = context.Identity.Claims.ToList(); + foreach (var claim in claims) { + context.Identity.RemoveClaim(claim); } var connstring = ConnectionStrings.AppDatabaseConnectionString(configuration); var connection = new NpgsqlConnection(connstring); - connection.Open(); - var query = new NpgsqlCommand(@$"SELECT user_id FROM github_user_mappings WHERE github_id='{ghid}'", connection); - var x = query.ExecuteReader(); - foreach (var row in x) { - Console.WriteLine(JsonSerializer.Serialize(row)); + + Console.WriteLine($"HandleGithubCreatingTicket: Getting user mappings for github user: {githubId}"); + var getMappedUserQuery = @$"SELECT u.id,u.username FROM github_user_mappings INNER JOIN users u on u.id = github_user_mappings.user_id WHERE github_id='{githubId}'"; + await connection.OpenAsync(); + await using var getMappedUserCommand = new NpgsqlCommand(getMappedUserQuery, connection); + await using var reader = await getMappedUserCommand.ExecuteReaderAsync(); + var handled = false; + while (await reader.ReadAsync()) { + try { + var userId = reader.GetGuid(0); + var username = reader.GetString(1); + context.Identity.AddClaim(new Claim(AppClaims.USER_ID, userId.ToString())); + context.Identity.AddClaim(new Claim(AppClaims.NAME, username)); + Console.WriteLine($"HandleGithubCreatingTicket: Found mapping for github id {githubId} mapped to user id {userId}"); + handled = true; + } catch (Exception e) { + Console.WriteLine(e); + handled = false; + } } - // var claims = context.Identity.Claims.ToList(); - // foreach (var claim in claims) { - // context.Identity.RemoveClaim(claim); - // } + await connection.CloseAsync(); - return Task.CompletedTask; + if (!handled) { + var userId = Guid.NewGuid(); + + var insertUserQuery = $@"INSERT INTO users VALUES ('{userId}', '{githubUsername}', '', '{DateTime.UtcNow}')"; + await connection.OpenAsync(); + await using var insertUserCommand = new NpgsqlCommand(insertUserQuery, connection); + await insertUserCommand.ExecuteNonQueryAsync(); + await connection.CloseAsync(); + + var insertMappingQuery = $@"INSERT INTO github_user_mappings VALUES ('{Guid.NewGuid()}', '{userId}', '{githubId}', '{DateTime.UtcNow}')"; + await connection.OpenAsync(); + await using var insertMappingCommand = new NpgsqlCommand(insertMappingQuery, connection); + await insertMappingCommand.ExecuteNonQueryAsync(); + await connection.CloseAsync(); + + context.Identity.AddClaim(new Claim(AppClaims.USER_ID, userId.ToString())); + context.Identity.AddClaim(new Claim(AppClaims.NAME, githubUsername)); + Console.WriteLine($"HandleGithubCreatingTicket: Created mapping for github id {githubId} mapped to user id {userId}"); + } } } |
