summaryrefslogtreecommitdiffstats
path: root/src/server/Utilities/HandleGithubCreatingTicket.cs
blob: 7b073069f9286a81ad731bdb8c323fa6e380bbe2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft.AspNetCore.Authentication.OAuth;
using Npgsql;

namespace IOL.BookmarkThing.Server.Utilities;

public static class HandleGithubCreatingTicket
{
	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);

		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;
			}
		}

		await connection.CloseAsync();

		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}");
		}
	}
}