4using System.Threading.Tasks;
6using Microsoft.EntityFrameworkCore;
7using Microsoft.Extensions.Logging;
8using Microsoft.Extensions.Options;
35 readonly ILogger<AuthenticationContextFactory>
logger;
52 IOptions<SwarmConfiguration> swarmConfigurationOptions,
53 ILogger<AuthenticationContextFactory>
logger)
57 swarmConfiguration = swarmConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(swarmConfigurationOptions));
58 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
68 throw new InvalidOperationException(
"Authentication context has already been loaded");
73 .Where(x => x.Id == userId)
74 .Include(x => x.CreatedBy)
75 .Include(x => x.PermissionSet)
76 .Include(x => x.Group)
77 .ThenInclude(x => x.PermissionSet)
78 .Include(x => x.OAuthConnections)
79 .FirstOrDefaultAsync(cancellationToken);
82 logger.LogWarning(
"Unable to find user with ID {0}!", userId);
88 if (user.SystemIdentifier !=
null)
92 if (user.LastPasswordUpdate.HasValue && user.LastPasswordUpdate > validAfter)
94 logger.LogDebug(
"Rejecting token for user {0} created before last password update: {1}", userId, user.LastPasswordUpdate.Value);
99 systemIdentity =
null;
102 var userPermissionSet = user.PermissionSet ?? user.Group.PermissionSet;
106 if (instanceId.HasValue)
108 instancePermissionSet = await databaseContext.InstancePermissionSets
110 .Where(x => x.PermissionSetId == userPermissionSet.Id && x.InstanceId == instanceId && x.Instance.SwarmIdentifer ==
swarmConfiguration.
Identifier)
111 .Include(x => x.Instance)
112 .FirstOrDefaultAsync(cancellationToken);
114 if (instancePermissionSet ==
null)
115 logger.LogDebug(
"User {0} does not have permissions on instance {1}!", userId, instanceId.Value);
121 instancePermissionSet);
125 systemIdentity?.Dispose();
string? Identifier
The server's identifier.
Configuration for the server swarm system.
async Task CreateAuthenticationContext(long userId, long? instanceId, DateTimeOffset validAfter, CancellationToken cancellationToken)
Create an IAuthenticationContext to populate CurrentAuthenticationContext. A Task representing the ru...
IAuthenticationContext CurrentAuthenticationContext
The IAuthenticationContext the IAuthenticationContextFactory created.
readonly IDatabaseContext databaseContext
The IDatabaseContext for the AuthenticationContextFactory.
AuthenticationContextFactory(IDatabaseContext databaseContext, IIdentityCache identityCache, IOptions< SwarmConfiguration > swarmConfigurationOptions, ILogger< AuthenticationContextFactory > logger)
Initializes a new instance of the AuthenticationContextFactory class.
readonly ILogger< AuthenticationContextFactory > logger
The ILogger for the AuthenticationContextFactory.
readonly SwarmConfiguration swarmConfiguration
The SwarmConfiguration for the AuthenticationContextFactory.
readonly IIdentityCache identityCache
The IIdentityCache for the AuthenticationContextFactory.
IDatabaseCollection< User > Users
The Users in the IDatabaseContext.
For creating and accessing authentication contexts.
Represents the currently authenticated Models.User.
For caching ISystemIdentitys.
ISystemIdentity LoadCachedIdentity(User user)
Attempt to load a cached ISystemIdentity.
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform.