예제 #1
0
        public CreateLinkResult CreateLink(string symbolicName, string userName)
        {
            using (var context = new RelayContext())
            {
                var password            = _passwordHash.GeneratePassword(_configuration.LinkPasswordLength);
                var passwordInformation = _passwordHash.CreatePasswordInformation(password);

                var link = new DbLink
                {
                    Id           = Guid.NewGuid(),
                    Password     = passwordInformation.Hash,
                    Salt         = passwordInformation.Salt,
                    Iterations   = passwordInformation.Iterations,
                    SymbolicName = symbolicName,
                    UserName     = userName,
                    CreationDate = DateTime.UtcNow
                };

                context.Links.Add(link);
                context.SaveChanges();

                var result = new CreateLinkResult
                {
                    Id       = link.Id,
                    Password = Convert.ToBase64String(password)
                };

                return(result);
            }
        }
예제 #2
0
        public bool Update(Guid id, string password)
        {
            if ((id == Guid.Empty) ||
                (String.IsNullOrWhiteSpace(password)))
            {
                return(false);
            }

            using (var context = new RelayContext())
            {
                var dbUser = context.Users.SingleOrDefault(u => u.Id == id);

                if (dbUser == null)
                {
                    return(false);
                }

                var passwordInformation = _passwordHash.CreatePasswordInformation(Encoding.UTF8.GetBytes(password));

                dbUser.Iterations = passwordInformation.Iterations;
                dbUser.Salt       = passwordInformation.Salt;
                dbUser.Password   = passwordInformation.Hash;

                context.Entry(dbUser).State = EntityState.Modified;

                return(context.SaveChanges() == 1);
            }
        }
예제 #3
0
        public Guid Create(string userName, string password)
        {
            if ((String.IsNullOrWhiteSpace(userName)) || (String.IsNullOrWhiteSpace(password)))
            {
                return(Guid.Empty);
            }

            var passwordInformation = _passwordHash.CreatePasswordInformation(Encoding.UTF8.GetBytes(password));

            var dbUser = new DbUser
            {
                Id           = Guid.NewGuid(),
                Iterations   = passwordInformation.Iterations,
                Password     = passwordInformation.Hash,
                Salt         = passwordInformation.Salt,
                UserName     = userName,
                CreationDate = DateTime.Now,
            };

            using (var context = new RelayContext())
            {
                context.Users.Add(dbUser);
                context.SaveChanges();

                return(dbUser.Id);
            }
        }
예제 #4
0
        public void DeleteLink(Guid linkId)
        {
            _logger?.Verbose("Removing link. link-id={LinkId}", linkId);

            try
            {
                using (var context = new RelayContext())
                {
                    var itemToDelete = new DbLink
                    {
                        Id = linkId,
                    };

                    context.Links.Attach(itemToDelete);
                    context.Links.Remove(itemToDelete);

                    context.SaveChanges();
                }
            }
            catch (DbUpdateConcurrencyException) { }
            catch (Exception ex)
            {
                _logger?.Error(ex, "Error while removing a link. link-id={LinkId}", linkId);
            }
        }
예제 #5
0
        public bool UpdateLink(LinkDetails link)
        {
            using (var context = new RelayContext())
            {
                var linkEntity = context.Links.SingleOrDefault(p => p.Id == link.Id);

                if (linkEntity == null)
                {
                    return(false);
                }

                linkEntity.CreationDate = link.CreationDate;
                linkEntity.AllowLocalClientRequestsOnly        = link.AllowLocalClientRequestsOnly;
                linkEntity.ForwardOnPremiseTargetErrorResponse = link.ForwardOnPremiseTargetErrorResponse;
                linkEntity.IsDisabled   = link.IsDisabled;
                linkEntity.MaximumLinks = link.MaximumLinks;
                linkEntity.SymbolicName = link.SymbolicName;
                linkEntity.UserName     = link.UserName;

                linkEntity.TokenRefreshWindow         = link.TokenRefreshWindow;
                linkEntity.HeartbeatInterval          = link.HeartbeatInterval;
                linkEntity.ReconnectMinWaitTime       = link.ReconnectMinWaitTime;
                linkEntity.ReconnectMaxWaitTime       = link.ReconnectMaxWaitTime;
                linkEntity.AbsoluteConnectionLifetime = link.AbsoluteConnectionLifetime;
                linkEntity.SlidingConnectionLifetime  = link.SlidingConnectionLifetime;

                context.Entry(linkEntity).State = EntityState.Modified;

                return(context.SaveChanges() == 1);
            }
        }
예제 #6
0
        public void LogRequest(RequestLogEntry requestLogEntry)
        {
            using (var context = new RelayContext())
            {
                var link = new DbLink
                {
                    Id = requestLogEntry.LinkId
                };

                context.Links.Attach(link);

                context.RequestLogEntries.Add(new DbRequestLogEntry
                {
                    Id                        = Guid.NewGuid(),
                    ContentBytesIn            = requestLogEntry.ContentBytesIn,
                    ContentBytesOut           = requestLogEntry.ContentBytesOut,
                    OnPremiseConnectorInDate  = requestLogEntry.OnPremiseConnectorInDate,
                    OnPremiseConnectorOutDate = requestLogEntry.OnPremiseConnectorOutDate,
                    HttpStatusCode            = requestLogEntry.HttpStatusCode,
                    OnPremiseTargetInDate     = requestLogEntry.OnPremiseTargetInDate,
                    OnPremiseTargetKey        = requestLogEntry.OnPremiseTargetKey,
                    OnPremiseTargetOutDate    = requestLogEntry.OnPremiseTargetOutDate,
                    LocalUrl                  = requestLogEntry.LocalUrl ?? "/",
                    OriginId                  = requestLogEntry.OriginId,
                    Link                      = link,
                    LinkId                    = requestLogEntry.LinkId,
                });

                context.SaveChanges();
            }
        }
예제 #7
0
        private void UnlockUser(RelayContext ctx, DbUser user)
        {
            user.LastFailedLoginAttempt = null;
            user.FailedLoginAttempts    = null;

            ctx.Entry(user).State = EntityState.Modified;
            ctx.SaveChanges();

            _logger?.Information("Unlocking user account {UserName}", user.UserName);
        }
예제 #8
0
        private void RecordFailedLoginAttempt(RelayContext ctx, DbUser user)
        {
            user.LastFailedLoginAttempt = DateTime.UtcNow;
            user.FailedLoginAttempts    = user.FailedLoginAttempts.GetValueOrDefault() + 1;

            ctx.Entry(user).State = EntityState.Modified;
            ctx.SaveChanges();

            _logger?.Information("User {UserName} failed logging in for {FailedLoginAttempts} attempts",
                                 user.UserName,
                                 user.FailedLoginAttempts
                                 );
        }
예제 #9
0
        public void DeleteLink(Guid linkId)
        {
            using (var context = new RelayContext())
            {
                var itemToDelete = new DbLink
                {
                    Id = linkId
                };

                context.Links.Attach(itemToDelete);
                context.Links.Remove(itemToDelete);

                context.SaveChanges();
            }
        }
예제 #10
0
        public bool Delete(Guid id)
        {
            using (var context = new RelayContext())
            {
                var dbUser = new DbUser
                {
                    Id = id,
                };

                context.Users.Attach(dbUser);
                context.Users.Remove(dbUser);

                return(context.SaveChanges() == 1);
            }
        }
예제 #11
0
        public void DeleteAllConnectionsForOrigin(Guid originId)
        {
            _logger?.Verbose("Deleting all active connections");

            try
            {
                using (var context = new RelayContext())
                {
                    var invalidConnections = context.ActiveConnections.Where(ac => ac.OriginId == originId).ToList();
                    context.ActiveConnections.RemoveRange(invalidConnections);

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, "Error during deleting of all active connections");
            }
        }
예제 #12
0
        public bool Disable(Guid id)
        {
            using (var context = new RelayContext())
            {
                var dbTraceConfiguration = context.TraceConfigurations.Include(t => t.Link).SingleOrDefault(t => t.Id == id);

                if (dbTraceConfiguration == null)
                {
                    return(false);
                }

                if (dbTraceConfiguration.EndDate < DateTime.UtcNow)
                {
                    return(true);
                }

                dbTraceConfiguration.EndDate = DateTime.UtcNow;

                context.Entry(dbTraceConfiguration).State = EntityState.Modified;

                return(context.SaveChanges() == 1);
            }
        }
예제 #13
0
        public bool UpdateLink(Link linkId)
        {
            using (var context = new RelayContext())
            {
                var itemToUpdate = context.Links.SingleOrDefault(p => p.Id == linkId.Id);

                if (itemToUpdate == null)
                {
                    return(false);
                }

                itemToUpdate.CreationDate = linkId.CreationDate;
                itemToUpdate.AllowLocalClientRequestsOnly        = linkId.AllowLocalClientRequestsOnly;
                itemToUpdate.ForwardOnPremiseTargetErrorResponse = linkId.ForwardOnPremiseTargetErrorResponse;
                itemToUpdate.IsDisabled   = linkId.IsDisabled;
                itemToUpdate.MaximumLinks = linkId.MaximumLinks;
                itemToUpdate.SymbolicName = linkId.SymbolicName;
                itemToUpdate.UserName     = linkId.UserName;

                context.Entry(itemToUpdate).State = EntityState.Modified;

                return(context.SaveChanges() == 1);
            }
        }
예제 #14
0
        public void Create(TraceConfiguration traceConfiguration)
        {
            using (var context = new RelayContext())
            {
                var link = new DbLink
                {
                    Id = traceConfiguration.LinkId
                };

                context.Links.Attach(link);

                context.TraceConfigurations.Add(new DbTraceConfiguration
                {
                    CreationDate = DateTime.UtcNow,
                    EndDate      = traceConfiguration.EndDate,
                    Id           = Guid.NewGuid(),
                    Link         = link,
                    LinkId       = traceConfiguration.LinkId,
                    StartDate    = traceConfiguration.StartDate
                });

                context.SaveChanges();
            }
        }