summaryrefslogtreecommitdiffstats
path: root/server/src/Utilities/GithubAuthenticationHelpers.cs
blob: cf0cabb531e3febf2fe4282cc2abe9c64802aa8d (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Microsoft.AspNetCore.Authentication.OAuth;
using Npgsql;

namespace IOL.GreatOffice.Api.Utilities;

public static class GithubAuthenticationHelpers
{
	public static async Task HandleGithubTicketCreation(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;
		var githubEmail = context.Identity?.FindFirst(p => p.Type == ClaimTypes.Email)?.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 = configuration.GetAppDatabaseConnectionString();
		var connection = new NpgsqlConnection(connstring);

		Log.Information($"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));
				if (context.AccessToken != default) {
					context.Identity.AddClaim(new Claim(AppClaims.GITHUB_ACCESS_TOKEN, context.AccessToken ?? ""));
				}

				Log.Information($"Found mapping for github id {githubId} mapped to user id {userId}");
				handled = true;
			} catch (Exception e) {
				Log.Error(e, "An exception occured when handling github user mappings");
				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 refreshTokenEncryptionKey = configuration.GetValue<string>(AppEnvironmentVariables.APP_AES_KEY);
			string insertMappingQuery;

			if (context.RefreshToken.HasValue() && refreshTokenEncryptionKey.HasValue()) {
				var encryptedRefreshToken = context.RefreshToken.EncryptWithAes(refreshTokenEncryptionKey);
				insertMappingQuery = $@"INSERT INTO github_user_mappings VALUES ('{githubId}', '{userId}', '{githubEmail}', '{encryptedRefreshToken}')";
			} else {
				insertMappingQuery = $@"INSERT INTO github_user_mappings VALUES ('{githubId}', '{userId}', '{githubEmail}', '')";
			}

			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));
			if (context.AccessToken != default) {
				context.Identity.AddClaim(new Claim(AppClaims.GITHUB_ACCESS_TOKEN, context.AccessToken ?? ""));
			}

			Log.Information($"Created mapping for github id {githubId} mapped to user id {userId}");
		}
	}
}