1 using Microsoft.EntityFrameworkCore;
2 using Microsoft.Extensions.Logging;
6 using System.Threading.Tasks;
31 readonly ILogger<AuthenticationContextFactory>
logger;
42 ILogger<AuthenticationContextFactory> logger)
44 this.databaseContext = databaseContext ??
throw new ArgumentNullException(nameof(databaseContext));
45 this.identityCache = identityCache ??
throw new ArgumentNullException(nameof(identityCache));
46 this.logger = logger ??
throw new ArgumentNullException(nameof(logger));
50 public void Dispose() => CurrentAuthenticationContext?.Dispose();
55 if (CurrentAuthenticationContext != null)
56 throw new InvalidOperationException(
"Authentication context has already been loaded");
58 var user = await databaseContext
61 .Where(x => x.Id == userId)
62 .Include(x => x.CreatedBy)
63 .FirstOrDefaultAsync(cancellationToken)
64 .ConfigureAwait(
false);
67 logger.LogWarning(
"Unable to find user with ID {0}!", userId);
73 if (user.SystemIdentifier != null)
74 systemIdentity = identityCache.LoadCachedIdentity(user);
77 if (user.LastPasswordUpdate.HasValue && user.LastPasswordUpdate > validAfter)
79 logger.LogDebug(
"Rejecting token for user {0} created before last password update: {1}", userId, user.LastPasswordUpdate.Value);
84 systemIdentity = null;
90 if (instanceId.HasValue)
92 instanceUser = await databaseContext.InstanceUsers
94 .Where(x => x.UserId == userId && x.InstanceId == instanceId && x.Instance.Online.Value)
95 .Include(x => x.Instance)
96 .FirstOrDefaultAsync(cancellationToken)
97 .ConfigureAwait(
false);
99 if (instanceUser == null)
100 logger.LogDebug(
"User {0} does not have permissions on instance {1}!", userId, instanceId.Value);
107 systemIdentity?.Dispose();
For creating and accessing authentication contexts
Manages Api.Models.Users for a scope
Use server authentication
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform ...
Represents the currently authenticated Api.Models.User
readonly IIdentityCache identityCache
The IIdentityCache for the AuthenticationContextFactory
readonly ILogger< AuthenticationContextFactory > logger
The ILogger for the AuthenticationContextFactory.
readonly IDatabaseContext databaseContext
The IDatabaseContext for the AuthenticationContextFactory
async Task CreateAuthenticationContext(long userId, long?instanceId, DateTimeOffset validAfter, CancellationToken cancellationToken)
Create an IAuthenticationContext to populate CurrentAuthenticationContext
AuthenticationContextFactory(IDatabaseContext databaseContext, IIdentityCache identityCache, ILogger< AuthenticationContextFactory > logger)
Construct an AuthenticationContextFactory
For caching ISystemIdentitys