-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrapperExtension.cs
More file actions
78 lines (63 loc) · 3.59 KB
/
Copy pathBootstrapperExtension.cs
File metadata and controls
78 lines (63 loc) · 3.59 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
namespace HttpsRichardy.Federation.WebApi.Extensions;
[ExcludeFromCodeCoverage]
public static class BootstrapperExtension
{
public static async Task UseBootstrapperAsync(this IApplicationBuilder builder)
{
using var scope = builder.ApplicationServices.CreateScope();
var realmCollection = scope.ServiceProvider.GetRequiredService<IRealmCollection>();
var userCollection = scope.ServiceProvider.GetRequiredService<IUserCollection>();
var scopeRepository = scope.ServiceProvider.GetRequiredService<IScopeCollection>();
var permissionCollection = scope.ServiceProvider.GetRequiredService<IPermissionCollection>();
var realmProvider = scope.ServiceProvider.GetRequiredService<IRealmProvider>();
var credentialsGenerator = scope.ServiceProvider.GetRequiredService<IClientCredentialsGenerator>();
var passwordHasher = scope.ServiceProvider.GetRequiredService<IPasswordHasher>();
var settings = scope.ServiceProvider.GetRequiredService<ISettings>();
var realmCredentials = await credentialsGenerator.GenerateAsync("master", cancellation: default);
var defaultRealm = new Realm { Name = "master", ClientId = realmCredentials.ClientId };
var realmFilters = RealmFilters.WithSpecifications()
.WithName("master")
.Build();
var realms = await realmCollection.GetRealmsAsync(realmFilters, cancellation: default);
var realm = realms.FirstOrDefault();
if (realm is not null)
{
return;
}
defaultRealm.SecretHash = await passwordHasher.HashPasswordAsync(realmCredentials.ClientId + defaultRealm.Name);
defaultRealm.Permissions = [.. RealmPermissions.SystemPermissions.Select(permissionName => new Permission
{
Id = Identifier.Generate<Permission>(),
Name = permissionName,
RealmId = defaultRealm.Id
})];
var scopes = new List<Scope>
{
new() { Id = Identifier.Generate<Scope>(), Name = Scopes.OpenID.Name, Description = Scopes.OpenID.Description, IsGlobal = true },
new() { Id = Identifier.Generate<Scope>(), Name = Scopes.Profile.Name, Description = Scopes.Profile.Description, IsGlobal = true },
new() { Id = Identifier.Generate<Scope>(), Name = Scopes.Email.Name, Description = Scopes.Email.Description, IsGlobal = true },
new() { Id = Identifier.Generate<Scope>(), Name = Scopes.Address.Name, Description = Scopes.Address.Description, IsGlobal = true },
new() { Id = Identifier.Generate<Scope>(), Name = Scopes.Phone.Name, Description = Scopes.Phone.Description, IsGlobal = true },
};
realmProvider.SetRealm(defaultRealm);
await realmCollection.InsertAsync(defaultRealm);
await scopeRepository.InsertManyAsync(scopes);
await permissionCollection.InsertManyAsync(defaultRealm.Permissions);
var userFilters = UserFilters.WithSpecifications()
.WithUsername(settings.Administration.Username)
.Build();
var existingUsers = await userCollection.GetUsersAsync(userFilters);
var rootUser = existingUsers.FirstOrDefault();
if (rootUser is null)
{
rootUser = new User
{
Username = settings.Administration.Username,
RealmId = defaultRealm.Id,
Permissions = [.. defaultRealm.Permissions],
PasswordHash = await passwordHasher.HashPasswordAsync(settings.Administration.Password)
};
await userCollection.InsertAsync(rootUser);
}
}
}