Skip to content

Commit

Permalink
Add feature to remove realm
Browse files Browse the repository at this point in the history
  • Loading branch information
thabart committed Nov 7, 2024
1 parent 6747169 commit 5d97d03
Show file tree
Hide file tree
Showing 45 changed files with 603 additions and 121 deletions.
5 changes: 2 additions & 3 deletions src/IdServer/SimpleIdServer.IdServer.Startup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ async void SeedData(WebApplication application, string scimBaseUrl)
{
var isInMemory = dbContext.Database.IsInMemory();
if (!isInMemory) dbContext.Database.Migrate();

if (dbContext.Translations.Any()) return;
var masterRealm = dbContext.Realms.FirstOrDefault(r => r.Name == SimpleIdServer.IdServer.Constants.StandardRealms.Master.Name) ?? SimpleIdServer.IdServer.Constants.StandardRealms.Master;
if (!dbContext.Realms.Any())
dbContext.Realms.AddRange(SimpleIdServer.IdServer.Startup.IdServerConfiguration.Realms);
Expand Down Expand Up @@ -434,8 +434,7 @@ async void SeedData(WebApplication application, string scimBaseUrl)
MigrateUsers(dbContext, groups.adminGroup, groups.adminRoGroup, groups.fastFedGroup);
if (!dbContext.SerializedFileKeys.Any())
{
dbContext.SerializedFileKeys.Add(KeyGenerator.GenerateRSASigningCredentials(SimpleIdServer.IdServer.Constants.StandardRealms.Master, "rsa-1"));
dbContext.SerializedFileKeys.Add(KeyGenerator.GenerateECDSASigningCredentials(SimpleIdServer.IdServer.Constants.StandardRealms.Master, "ecdsa-1"));
dbContext.SerializedFileKeys.AddRange(SimpleIdServer.IdServer.Constants.StandardKeys);
dbContext.SerializedFileKeys.Add(WsFederationKeyGenerator.GenerateWsFederationSigningCredentials(SimpleIdServer.IdServer.Constants.StandardRealms.Master));
}

Expand Down
8 changes: 4 additions & 4 deletions src/IdServer/SimpleIdServer.IdServer.Startup/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
},
"Authority": "https://localhost:5001",
"MessageBrokerOptions": {
"Transport": "INMEMORY",
"Transport": "SQLSERVER",
"ConnectionString": "Data Source=.;Initial Catalog=MessageBroker;Integrated Security=True;TrustServerCertificate=True",
"Username": "username",
"Password": "password"
"Password": "PW2H:4f[`,L9kBcMs"
},
"DistributedCacheConfiguration": {
"Type": "INMEMORY",
"Type": "SQLSERVER",
"ConnectionString": "Data Source=.;Initial Catalog=IdServer;Integrated Security=True;TrustServerCertificate=True",
"InstanceName": ""
},
"StorageConfiguration": {
"Type": "INMEMORY",
"Type": "SQLSERVER",
"ConnectionString": "Data Source=.;Initial Catalog=IdServer;Integrated Security=True;TrustServerCertificate=True"
},
"Facebook": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public Task<AuthenticationContextClassReference> GetByName(string realm, string
.SingleOrDefaultAsync(a => a.Realms.Any(r => r.Name == realm) && a.Name == name, cancellationToken);
}

public Task<List<AuthenticationContextClassReference>> GetByNames(List<string> names, CancellationToken cancellationToken)
{
return _dbContext.Acrs
.Include(a => a.Realms)
.Include(a => a.RegistrationWorkflow)
.Where(a => names.Contains(a.Name))
.ToListAsync(cancellationToken);
}

public Task<List<AuthenticationContextClassReference>> GetByNames(string realm, List<string> names, CancellationToken cancellationToken)
{
return _dbContext.Acrs
Expand Down
16 changes: 14 additions & 2 deletions src/IdServer/SimpleIdServer.IdServer.Store.EF/ClientRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SimpleIdServer.IdServer.Domains;
using SimpleIdServer.IdServer.Helpers;
using SimpleIdServer.IdServer.Stores;
using SimpleIdServer.Scim.Domains;
using System.Linq.Dynamic.Core;

namespace SimpleIdServer.IdServer.Store.EF;
Expand Down Expand Up @@ -37,6 +38,17 @@ public Task<Client> GetByClientId(string realm, string clientId, CancellationTok
.SingleOrDefaultAsync(c => c.ClientId == clientId && c.Realms.Any(r => r.Name == realm), cancellationToken);
}

public Task<List<Client>> GetByClientIds(List<string> clientIds, CancellationToken cancellationToken)
{
return _dbContext.Clients
.Include(c => c.SerializedJsonWebKeys)
.Include(c => c.Realms)
.Include(c => c.Scopes)
.Include(c => c.Translations)
.Where(c => clientIds.Contains(c.ClientId))
.ToListAsync(cancellationToken);
}

public Task<List<Client>> GetByClientIds(string realm, List<string> clientIds, CancellationToken cancellationToken)
{
return _dbContext.Clients
Expand All @@ -48,10 +60,10 @@ public Task<List<Client>> GetByClientIds(string realm, List<string> clientIds, C
.ToListAsync(cancellationToken);
}

public Task<List<Client>> GetByClientIdsAndExistingBackchannelLogoutUri(string realm, List<string> clientIds, CancellationToken cancellationToken)
public Task<List<Client>> GetByClientIdsAndExistingBackchannelLogoutUri(List<string> clientIds, CancellationToken cancellationToken)
{
return _dbContext.Clients
.Where(c => clientIds.Contains(c.ClientId) && c.Realms.Any(r => r.Name == realm) && !string.IsNullOrWhiteSpace(c.BackChannelLogoutUri))
.Where(c => clientIds.Contains(c.ClientId) && !string.IsNullOrWhiteSpace(c.BackChannelLogoutUri))
.ToListAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public Task<List<Group>> GetAllByFullPath(string realm, string fullPath, Cancell
.Where(g => g.Realms.Any(r => r.RealmsName == realm) && g.FullPath.StartsWith(fullPath))
.ToListAsync(cancellationToken);

public Task<List<Group>> GetAllByStrictFullPath(List<string> fullPathLst, CancellationToken cancellationToken)
=> _dbContext.Groups
.Include(g => g.Roles).ThenInclude(r => r.Realms)
.Include(c => c.Realms)
.Where(g => fullPathLst.Contains(g.FullPath))
.ToListAsync(cancellationToken);

public Task<List<Group>> GetAllByStrictFullPath(string realm, List<string> fullPathLst, CancellationToken cancellationToken)
=> _dbContext.Groups
.Include(g => g.Roles).ThenInclude(r => r.Realms)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public Task<Realm> Get(string name, CancellationToken cancellationToken)
=> _dbContext.Realms.SingleOrDefaultAsync(r => r.Name == name, cancellationToken);

public void Add(Realm realm) =>_dbContext.Realms.Add(realm);

public void Remove(Realm realm) => _dbContext.Realms.Remove(realm);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public Task<Scope> GetByName(string realm, string name, CancellationToken cancel
.SingleOrDefaultAsync(s => s.Name == name && s.Realms.Any(r => r.Name == realm), cancellationToken);
}

public Task<List<Scope>> GetByNames(List<string> scopeNames, CancellationToken cancellationToken)
{
return _dbContext.Scopes
.Include(s => s.Realms)
.Include(s => s.ClaimMappers)
.Where(s => scopeNames.Contains(s.Name))
.ToListAsync(cancellationToken);
}

public Task<List<Scope>> GetByNames(string realm, List<string> scopeNames, CancellationToken cancellationToken)
{
return _dbContext.Scopes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.EntityFrameworkCore;
using SimpleIdServer.IdServer.Domains;
using SimpleIdServer.IdServer.Stores;
using SimpleIdServer.Scim.Domains;

namespace SimpleIdServer.IdServer.Store.EF;

Expand All @@ -15,6 +16,15 @@ public SerializedFileKeyStore(StoreDbContext dbContext)
{
_dbContext = dbContext;
}

public Task<List<SerializedFileKey>> GetByKeyIds(List<string> keyIds, CancellationToken cancellationToken)
{
return _dbContext.SerializedFileKeys
.Include(s => s.Realms)
.Where(s => keyIds.Contains(s.KeyId))
.ToListAsync(cancellationToken);
}

public IQueryable<SerializedFileKey> Query() => _dbContext.SerializedFileKeys;

public Task<List<SerializedFileKey>> GetAll(string realm, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ public async Task<UserSession> GetById(string sessionId, string realm, Cancellat
return session;
}

public Task<int> NbActiveSessions(string realm, CancellationToken cancellationToken)
=> _dbContext.UserSession.CountAsync(s => s.Realm == realm && s.State == UserSessionStates.Active, cancellationToken);

public async Task<SearchResult<UserSession>> SearchActiveSessions(string realm, SearchRequest request, CancellationToken cancellationToken)
{
var query = _dbContext.UserSession
.Include(u => u.User)
.Where(u => u.Realm == realm && u.State == UserSessionStates.Active)
.OrderBy(u => u.SessionId);
var count = query.Count();
var users = await query.Skip(request.Skip.Value).Take(request.Take.Value).ToListAsync(CancellationToken.None);
return new SearchResult<UserSession>
{
Content = users,
Count = count
};
}

public async Task<SearchResult<UserSession>> Search(string userId, string realm, SearchRequest request, CancellationToken cancellationToken)
{
var query = _dbContext.UserSession
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public async Task<AuthenticationContextClassReference> GetByName(string realm, s
return result?.ToDomain();
}

public async Task<List<AuthenticationContextClassReference>> GetByNames(List<string> names, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarAuthenticationContextClassReference>()
.Includes(a => a.Realms)
.Includes(a => a.RegistrationWorkflow)
.Where(a => names.Contains(a.Name))
.ToListAsync(cancellationToken);
return result.Select(r => r.ToDomain()).ToList();
}

public async Task<List<AuthenticationContextClassReference>> GetByNames(string realm, List<string> names, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarAuthenticationContextClassReference>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ public async Task<Client> GetByClientId(string realm, string clientId, Cancellat
return result?.ToDomain();
}

public async Task<List<Client>> GetByClientIds(List<string> clientIds, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarClient>()
.Includes(c => c.SerializedJsonWebKeys)
.Includes(c => c.Realms)
.Includes(c => c.ClientScopes, c => c.Scope)
.Includes(c => c.Translations)
.Where(c => clientIds.Contains(c.ClientId))
.ToListAsync(cancellationToken);
return result.Select(r => r.ToDomain()).ToList();
}

public async Task<List<Client>> GetByClientIds(string realm, List<string> clientIds, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarClient>()
Expand All @@ -105,10 +117,10 @@ public async Task<List<Client>> GetByClientIds(string realm, List<string> client
return result.Select(r => r.ToDomain()).ToList();
}

public async Task<List<Client>> GetByClientIdsAndExistingBackchannelLogoutUri(string realm, List<string> clientIds, CancellationToken cancellationToken)
public async Task<List<Client>> GetByClientIdsAndExistingBackchannelLogoutUri(List<string> clientIds, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarClient>()
.Where(c => clientIds.Contains(c.ClientId) && c.Realms.Any(r => r.RealmsName == realm) && !string.IsNullOrWhiteSpace(c.BackChannelLogoutUri))
.Where(c => clientIds.Contains(c.ClientId) && !string.IsNullOrWhiteSpace(c.BackChannelLogoutUri))
.ToListAsync();
return result.Select(r => r.ToDomain()).ToList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public async Task<List<Group>> GetAllByFullPath(string realm, string id, string
return result.Select(r => r.ToDomain()).ToList();
}

public async Task<List<Group>> GetAllByStrictFullPath(List<string> fullPathLst, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarGroup>()
.Includes(g => g.Roles, r => r.Realms)
.Includes(c => c.Realms)
.Where(g => fullPathLst.Contains(g.FullPath))
.ToListAsync(cancellationToken);
return result.Select(r => r.ToDomain()).ToList();
}

public async Task<List<Group>> GetAllByStrictFullPath(string realm, List<string> fullPathLst, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarGroup>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public async Task<Realm> Get(string name, CancellationToken cancellationToken)
public void Add(Realm realm)
=> _dbContext.Client.Insertable(Transform(realm)).ExecuteCommand();

public void Remove(Realm realm) => _dbContext.Client.Deleteable(realm);

private static SugarRealm Transform(Realm realm)
{
return new SugarRealm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public async Task<Scope> GetByName(string realm, string scopeName, CancellationT
return result?.ToDomain();
}

public async Task<List<Scope>> GetByNames(List<string> scopeNames, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarScope>()
.Includes(s => s.Realms)
.Includes(s => s.ClaimMappers)
.Where(s => scopeNames.Contains(s.Name))
.ToListAsync(cancellationToken);
return result.Select(s => s.ToDomain()).ToList();
}

public async Task<List<Scope>> GetByNames(string realm, List<string> scopeNames, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarScope>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SimpleIdServer.IdServer.Domains;
using SimpleIdServer.IdServer.Store.SqlSugar.Models;
using SimpleIdServer.IdServer.Stores;
using SimpleIdServer.Scim.Domains;

namespace SimpleIdServer.IdServer.Store.SqlSugar;

Expand All @@ -16,6 +17,15 @@ public SerializedFileKeyStore(DbContext dbContext)
_dbContext = dbContext;
}

public async Task<List<SerializedFileKey>> GetByKeyIds(List<string> keyIds, CancellationToken cancellationToken)
{
var result = await _dbContext.Client.Queryable<SugarSerializedFileKey>()
.Includes(s => s.Realms)
.Where(s => keyIds.Contains(s.KeyId))
.ToListAsync(cancellationToken);
return result.Select(r => r.ToDomain()).ToList();
}

public void Add(SerializedFileKey key)
{
_dbContext.Client.InsertNav(Transform(key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ public async Task<IEnumerable<UserSession>> GetInactiveAndNotNotified(Cancellati
return session.Select(s => s.ToDomain());
}

public Task<int> NbActiveSessions(string realm, CancellationToken cancellationToken)
=> _dbContext.Client.Queryable<SugarUserSession>().CountAsync(s => s.Realm == realm && s.State == UserSessionStates.Active, cancellationToken);

public async Task<SearchResult<UserSession>> SearchActiveSessions(string realm, SearchRequest request, CancellationToken cancellationToken)
{
var query = _dbContext.Client.Queryable<SugarUserSession>()
.Includes(s => s.User)
.Where(u => u.Realm == realm && u.State == UserSessionStates.Active)
.OrderBy(u => u.SessionId);
var count = query.Count();
var users = await query.Skip(request.Skip.Value).Take(request.Take.Value).ToListAsync(CancellationToken.None);
return new SearchResult<UserSession>
{
Content = users.Select(u => u.ToDomain()).ToList(),
Count = count
};
}

public async Task<SearchResult<UserSession>> Search(string userId, string realm, SearchRequest request, CancellationToken cancellationToken)
{
var query = _dbContext.Client.Queryable<SugarUserSession>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,6 @@ private StaticFileMiddleware GetStaticFileMiddleware(string path)
return _middlewares[key];
}

private StaticFileMiddleware CreateStaticFileMiddleware(
RequestDelegate next,
IWebHostEnvironment hostingEnv,
ILoggerFactory loggerFactory,
SwaggerUIOptions options)
{
var staticFileOptions = new StaticFileOptions
{
RequestPath = string.IsNullOrEmpty(options.RoutePrefix) ? string.Empty : $"/{options.RoutePrefix}",
FileProvider = new EmbeddedFileProvider(typeof(SwaggerUIMiddleware).GetTypeInfo().Assembly, EmbeddedFileNamespace),
};
return new StaticFileMiddleware(next, hostingEnv, Microsoft.Extensions.Options.Options.Create(staticFileOptions), loggerFactory);
}

private void RespondWithRedirect(HttpResponse response, string location)
{
response.StatusCode = 301;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ProjectReference Include="..\SimpleIdServer.IdServer\SimpleIdServer.IdServer.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
<PackageReference Include="Swashbuckle.AspNetCore.ReDoc" Version="6.9.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.ReDoc" Version="6.5.0" />
</ItemGroup>
</Project>
Loading

0 comments on commit 5d97d03

Please sign in to comment.