-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServiceExtensions.cs
More file actions
94 lines (76 loc) · 3.44 KB
/
Copy pathServiceExtensions.cs
File metadata and controls
94 lines (76 loc) · 3.44 KB
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
85
86
87
88
89
90
91
92
93
94
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025 Datadog, Inc.
using Confluent.Kafka;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Stickerlandia.UserManagement.Auth;
using Stickerlandia.UserManagement.Core;
using Stickerlandia.UserManagement.Core.Outbox;
namespace Stickerlandia.UserManagement.Agnostic;
public static class ServiceExtensions
{
public static IServiceCollection AddAgnosticAdapters(this IServiceCollection services, IConfiguration configuration, bool enableDefaultUi = true)
{
services.AddKafkaMessaging(configuration);
services.AddPostgresAuthServices(configuration, enableDefaultUi);
return services;
}
public static IServiceCollection AddKafkaMessaging(this IServiceCollection services, IConfiguration configuration)
{
var producerConfig = new ProducerConfig
{
// User-specific properties that you must set
BootstrapServers = configuration.GetConnectionString("messaging"),
// Fixed properties
SecurityProtocol = SecurityProtocol.Plaintext,
Acks = Acks.All
};
var consumerConfig = new ConsumerConfig
{
// User-specific properties that you must set
BootstrapServers = configuration.GetConnectionString("messaging"),
// Fixed properties
SecurityProtocol = SecurityProtocol.Plaintext,
GroupId = "stickerlandia-users",
AutoOffsetReset = AutoOffsetReset.Earliest,
EnableAutoCommit = false
};
services.AddSingleton(producerConfig);
services.AddSingleton(consumerConfig);
// Register event publisher as singleton
services.AddSingleton<IUserEventPublisher, KafkaEventPublisher>();
services.AddSingleton<IMessagingWorker, KafakStickerClaimedWorker>();
return services;
}
public static IServiceCollection AddPostgresAuthServices(this IServiceCollection services,
IConfiguration configuration,
bool enableDefaultUi = true)
{
services.AddDbContext<UserManagementDbContext>(options =>
{
options.UseNpgsql(configuration.GetConnectionString("database"),
npgsqlOptions => npgsqlOptions.MigrationsAssembly("Stickerlandia.UserManagement.Agnostic"));
options.UseOpenIddict();
});
var identityOptions = services.AddIdentity<PostgresUserAccount, IdentityRole>()
.AddEntityFrameworkStores<UserManagementDbContext>()
.AddDefaultTokenProviders();
if (enableDefaultUi)
{
identityOptions.AddDefaultUI();
}
var disableSsl = false;
if (configuration.GetValue<bool>("DISABLE_SSL")) disableSsl = true;
services.AddCoreAuthentication(options =>
options.UseEntityFrameworkCore()
.UseDbContext<UserManagementDbContext>(), disableSsl);
services.AddScoped<IAuthService, MicrosoftIdentityAuthService>();
services.AddScoped<IUsers, PostgresUserRepository>();
services.AddScoped<IOutbox, PostgresUserRepository>();
return services;
}
}