4using System.Threading.Tasks;
6using Microsoft.EntityFrameworkCore;
7using Microsoft.Extensions.Logging;
8using Microsoft.Extensions.Options;
37 readonly ILogger<AuthenticationContextFactory>
logger;
64 IOptions<SwarmConfiguration> swarmConfigurationOptions,
65 ILogger<AuthenticationContextFactory>
logger)
69 swarmConfiguration = swarmConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(swarmConfigurationOptions));
70 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
79 public async ValueTask<IAuthenticationContext>
CreateAuthenticationContext(
long userId,
long? instanceId, DateTimeOffset notBefore, CancellationToken cancellationToken)
82 throw new InvalidOperationException(
"Authentication context has already been loaded");
87 .Where(x => x.Id == userId)
88 .Include(x => x.CreatedBy)
89 .Include(x => x.PermissionSet)
90 .Include(x => x.Group)
92 .Include(x => x.OAuthConnections)
93 .FirstOrDefaultAsync(cancellationToken);
96 logger.LogWarning(
"Unable to find user with ID {userId}!", userId);
101 if (user.SystemIdentifier !=
null)
105 if (user.LastPasswordUpdate.HasValue && user.LastPasswordUpdate >= notBefore)
107 logger.LogDebug(
"Rejecting token for user {userId} created before last password update: {lastPasswordUpdate}", userId, user.LastPasswordUpdate.Value);
111 systemIdentity =
null;
114 var userPermissionSet = user.PermissionSet ?? user.Group!.PermissionSet;
118 if (instanceId.HasValue)
120 instancePermissionSet = await databaseContext.InstancePermissionSets
122 .Where(x => x.PermissionSetId == userPermissionSet!.Id && x.InstanceId == instanceId && x.Instance!.SwarmIdentifer ==
swarmConfiguration.
Identifier)
123 .Include(x => x.Instance)
124 .FirstOrDefaultAsync(cancellationToken);
126 if (instancePermissionSet ==
null)
127 logger.LogDebug(
"User {userId} does not have permissions on instance {instanceId}!", userId, instanceId.Value);
133 instancePermissionSet);
138 systemIdentity?.Dispose();
string? Identifier
The server's identifier.
Configuration for the server swarm system.
IAuthenticationContext CurrentAuthenticationContext
The IAuthenticationContext the AuthenticationContextFactory 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.
async ValueTask< IAuthenticationContext > CreateAuthenticationContext(long userId, long? instanceId, DateTimeOffset notBefore, CancellationToken cancellationToken)
Create an IAuthenticationContext in the request pipeline for a given userId and instanceId ....
readonly ILogger< AuthenticationContextFactory > logger
The ILogger for the AuthenticationContextFactory.
int initialized
1 if currentAuthenticationContext was initialized, 0 otherwise.
readonly SwarmConfiguration swarmConfiguration
The SwarmConfiguration for the AuthenticationContextFactory.
readonly IIdentityCache identityCache
The IIdentityCache for the AuthenticationContextFactory.
readonly AuthenticationContext currentAuthenticationContext
Backing field for CurrentAuthenticationContext.
void Initialize(ISystemIdentity? systemIdentity, User user, InstancePermissionSet? instanceUser)
Initializes the AuthenticationContext.
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.