コード例 #1
0
        /// <inheritdoc />
        public async Task CreateAuthenticationContext(long userId, long?instanceId, DateTimeOffset validAfter, CancellationToken cancellationToken)
        {
            if (CurrentAuthenticationContext != null)
            {
                throw new InvalidOperationException("Authentication context has already been loaded");
            }

            var userQuery = databaseContext.Users.Where(x => x.Id == userId)
                            .Include(x => x.CreatedBy)
                            .FirstOrDefaultAsync(cancellationToken);

            var instanceUserQuery = instanceId.HasValue ? databaseContext.InstanceUsers
                                    .Where(x => x.UserId == userId && x.InstanceId == instanceId && x.Instance.Online.Value)
                                    .Include(x => x.Instance)
                                    .FirstOrDefaultAsync(cancellationToken) : Task.FromResult <InstanceUser>(null);

            var user = await userQuery.ConfigureAwait(false);

            if (user == default)
            {
                CurrentAuthenticationContext = new AuthenticationContext();
                return;
            }

            ISystemIdentity systemIdentity;

            if (user.SystemIdentifier != null)
            {
                systemIdentity = identityCache.LoadCachedIdentity(user);
            }
            else
            {
                if (user.LastPasswordUpdate.HasValue && user.LastPasswordUpdate > validAfter)
                {
                    CurrentAuthenticationContext = new AuthenticationContext();
                    return;
                }

                systemIdentity = null;
            }

            try
            {
                var instanceUser = await instanceUserQuery.ConfigureAwait(false);

                CurrentAuthenticationContext = new AuthenticationContext(systemIdentity, user, instanceUser);
            }
            catch
            {
                systemIdentity?.Dispose();
                throw;
            }
        }
コード例 #2
0
        /// <inheritdoc />
        public async Task CreateAuthenticationContext(long userId, long?instanceId, DateTimeOffset validBefore, CancellationToken cancellationToken)
        {
            if (CurrentAuthenticationContext != null)
            {
                throw new InvalidOperationException("Authentication context has already been loaded");
            }

            var userQuery = databaseContext.Users.Where(x => x.Id == userId).FirstOrDefaultAsync(cancellationToken);

            var instanceUser = instanceId.HasValue ? (await databaseContext.InstanceUsers
                                                      .Where(x => x.UserId == userId && x.InstanceId == instanceId && x.Instance.Online.Value)
                                                      .Include(x => x.Instance)
                                                      .FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false)) : null;

            var user = await userQuery.ConfigureAwait(false);

            if (user == default)
            {
                CurrentAuthenticationContext = new AuthenticationContext();
                return;
            }

            ISystemIdentity systemIdentity;

            if (user.SystemIdentifier != null)
            {
                systemIdentity = identityCache.LoadCachedIdentity(user);
                if (systemIdentity == null)
                {
                    throw new InvalidOperationException("Cached system identity has expired!");
                }
            }
            else
            {
                if (user.LastPasswordUpdate.HasValue && user.LastPasswordUpdate > validBefore)
                {
                    CurrentAuthenticationContext = new AuthenticationContext();
                    return;
                }
                systemIdentity = null;
            }

            CurrentAuthenticationContext = new AuthenticationContext(systemIdentity, user, instanceUser);
        }
コード例 #3
0
        /// <inheritdoc />
        public async Task CreateAuthenticationContext(long userId, long?instanceId, DateTimeOffset validAfter, CancellationToken cancellationToken)
        {
            if (CurrentAuthenticationContext != null)
            {
                throw new InvalidOperationException("Authentication context has already been loaded");
            }

            var user = await databaseContext
                       .Users
                       .AsQueryable()
                       .Where(x => x.Id == userId)
                       .Include(x => x.CreatedBy)
                       .FirstOrDefaultAsync(cancellationToken)
                       .ConfigureAwait(false);

            if (user == default)
            {
                logger.LogWarning("Unable to find user with ID {0}!", userId);
                CurrentAuthenticationContext = new AuthenticationContext();
                return;
            }

            ISystemIdentity systemIdentity;

            if (user.SystemIdentifier != null)
            {
                systemIdentity = identityCache.LoadCachedIdentity(user);
            }
            else
            {
                if (user.LastPasswordUpdate.HasValue && user.LastPasswordUpdate > validAfter)
                {
                    logger.LogDebug("Rejecting token for user {0} created before last password update: {1}", userId, user.LastPasswordUpdate.Value);
                    CurrentAuthenticationContext = new AuthenticationContext();
                    return;
                }

                systemIdentity = null;
            }

            try
            {
                InstanceUser instanceUser = null;
                if (instanceId.HasValue)
                {
                    instanceUser = await databaseContext.InstanceUsers
                                   .AsQueryable()
                                   .Where(x => x.UserId == userId && x.InstanceId == instanceId)
                                   .Include(x => x.Instance)
                                   .FirstOrDefaultAsync(cancellationToken)
                                   .ConfigureAwait(false);

                    if (instanceUser == null)
                    {
                        logger.LogDebug("User {0} does not have permissions on instance {1}!", userId, instanceId.Value);
                    }
                }

                CurrentAuthenticationContext = new AuthenticationContext(systemIdentity, user, instanceUser);
            }
            catch
            {
                systemIdentity?.Dispose();
                throw;
            }
        }