Exemplo n.º 1
0
        public void Add(Entities.Domain domain)
        {
            using (SqlConnection connection = new SqlConnection(Settings.Current.StorageSource))
            {
                connection.Open();

                using (SqlCommand command = new SqlCommand("Zesty_Domain_Add", connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;

                    command.Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@id", Value = domain.Id
                    });
                    command.Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@name", Value = domain.Name
                    });
                    command.Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@parent", Value = domain.ParentDomainId == Guid.Empty ? DBNull.Value : (Object)domain.ParentDomainId
                    });

                    command.ExecuteNonQuery();
                }
            }
        }
Exemplo n.º 2
0
        public override ApiHandlerOutput Process(ApiInputHandler input)
        {
            DomainRequest request = GetEntity <DomainRequest>(input);

            List <Entities.Domain> domains = Business.User.GetDomains(Context.Current.User.Username);

            Entities.Domain domain = domains.Where(x => x.Id.ToString().ToLower() == request.Domain.ToLower() || x.Name.ToLower() == request.Domain.ToLower()).FirstOrDefault();

            if (domain == null)
            {
                domain = NestSearch(domains, request.Domain);

                if (domain == null)
                {
                    throw new ApiNotFoundException(request.Domain);
                }
            }

            Business.User.SetDomain(Context.Current.User.Id, domain.Id);

            Context.Current.User.DomainId = domain.Id;
            Context.Current.User.Domain   = domain;

            DomainResponse response = new DomainResponse()
            {
                User = Context.Current.User
            };

            input.Context.Session.Set(Context.Current.User);

            return(GetOutput(response));
        }
Exemplo n.º 3
0
        public IActionResult Domain(DomainRequest request)
        {
            ValidateEntity <DomainRequest>(request);

            List <Entities.Domain> domains = Business.User.GetDomains(Context.Current.User.Username);

            Entities.Domain domain = domains.Where(x => x.Id.ToString().ToLower() == request.Domain.ToLower() || x.Name.ToLower() == request.Domain.ToLower()).FirstOrDefault();

            if (domain == null)
            {
                domain = NestSearch(domains, request.Domain);

                if (domain == null)
                {
                    throw new ApiNotFoundException(request.Domain);
                }
            }

            Business.User.SetDomain(Context.Current.User.Id, domain.Id);

            Context.Current.User.DomainId = domain.Id;
            Context.Current.User.Domain   = domain;

            DomainResponse response = new DomainResponse()
            {
                User = Context.Current.User
            };

            HttpContext.Session.Set(Context.Current.User);

            return(GetOutput(response));
        }
Exemplo n.º 4
0
        public IActionResult Domain(AddDomainRequest request)
        {
            ValidateEntity <AddDomainRequest>(request);

            Entities.Domain domain = new Entities.Domain()
            {
                Id             = Guid.NewGuid(),
                Name           = request.Name,
                ParentDomainId = String.IsNullOrWhiteSpace(request.Parent) ? Guid.Empty : Guid.Parse(request.Parent)
            };

            Business.Domain.Add(domain);

            return(GetOutput(new AddDomainResponse()
            {
                Domain = domain
            }, 201));
        }
Exemplo n.º 5
0
        public override ApiHandlerOutput Process(ApiInputHandler input)
        {
            AddRequest request = GetEntity <AddRequest>(input);

            Entities.Domain domain = new Entities.Domain()
            {
                Id             = Guid.NewGuid(),
                Name           = request.Name,
                ParentDomainId = String.IsNullOrWhiteSpace(request.Parent) ? Guid.Empty : Guid.Parse(request.Parent)
            };

            Business.Domain.Add(domain);

            return(GetOutput(new AddResponse()
            {
                Domain = domain
            }));
        }
Exemplo n.º 6
0
        private Entities.Domain NestSearch(List <Entities.Domain> domains, string domain)
        {
            foreach (Entities.Domain d in domains)
            {
                if (d.Id.ToString() == domain || d.Name == domain)
                {
                    return(d);
                }

                Entities.Domain inner = NestSearch(d.Childs, domain);

                if (inner != null)
                {
                    return(inner);
                }
            }

            return(null);
        }
Exemplo n.º 7
0
 internal static void Add(Entities.Domain domain)
 {
     storage.Add(domain);
 }
Exemplo n.º 8
0
        public override ApiHandlerOutput Process(ApiInputHandler input)
        {
            LoginRequest request = GetEntity <LoginRequest>(input);

            LoginOutput loginOutput = Business.User.Login(request.Username, request.Password);

            if (loginOutput.Result == LoginResult.Failed)
            {
                throw new ApiAccessDeniedException(Messages.LoginFailed);
            }
            else if (loginOutput.Result == LoginResult.PasswordExpired)
            {
                throw new ApiAccessDeniedException(Messages.PasswordExpired);
            }

            if (!string.IsNullOrEmpty(request.Domain))
            {
                List <Entities.Domain> domains = Business.User.GetDomains(loginOutput.User.Username);

                Entities.Domain domain = domains.Where(x => x.Id.ToString().ToLower() == request.Domain.ToLower() || x.Name.ToLower() == request.Domain.ToLower()).FirstOrDefault();

                if (domain == null)
                {
                    domain = NestSearch(domains, request.Domain);

                    if (domain == null)
                    {
                        throw new ApiNotFoundException(request.Domain);
                    }
                }

                Business.User.SetDomain(loginOutput.User.Id, domain.Id);

                loginOutput.User.DomainId = domain.Id;
                loginOutput.User.Domain   = domain;
            }

            if (loginOutput.User.DomainId != Guid.Empty && Business.Domain.HasTwoFactorAuthentication(loginOutput.User.DomainId))
            {
                IAuthProcessor processor = new Skebby();

                processor.GenerateOtp(loginOutput.User.Id, loginOutput.User.DomainId);

                LoginTwoFactorResponse twoFactorResponse = new LoginTwoFactorResponse()
                {
                    Domain = loginOutput.User.DomainId
                };

                return(GetOutput(twoFactorResponse));
            }

            LoginResponse response = new LoginResponse()
            {
                Output = loginOutput
            };

            if (request.Bearer == "true" && loginOutput.User != null)
            {
                string secret = HashHelper.GetSha256(request.Password);

                var p = loginOutput.User.Properties;

                loginOutput.User.Properties.Clear();

                string token = JwtBuilder.Create()
                               .WithAlgorithm(new HMACSHA256Algorithm())
                               .WithSecret(secret)
                               .AddClaim("exp", DateTimeOffset.UtcNow.AddHours(12).ToUnixTimeSeconds())
                               .AddClaim("user", response.Output.User)
                               .Encode();

                logger.Debug($"token generated: {token}");

                Business.User.SaveBearer(loginOutput.User.Id, token);

                response.Bearer = token;

                loginOutput.User.Properties = p;
            }

            input.Context.Session.Set(response.Output.User);

            return(GetOutput(response));
        }
Exemplo n.º 9
0
        public override ApiHandlerOutput Process(ApiInputHandler input)
        {
            OneTimePasswordRequest request = GetEntity <OneTimePasswordRequest>(input);

            if (!Business.OneTimePassword.Exists(request.Username, Guid.Parse(request.Domain), request.Otp))
            {
                throw new ApiAccessDeniedException(Messages.LoginFailed);
            }

            LoginOutput loginOutput = new LoginOutput();

            loginOutput.User = Business.User.Get(request.Username);

            if (loginOutput.User == null)
            {
                ThrowInvalidArgument();
            }

            List <Entities.Domain> domains = Business.User.GetDomains(loginOutput.User.Username);

            Entities.Domain domain = domains.Where(x => x.Id.ToString().ToLower() == request.Domain.ToLower() || x.Name.ToLower() == request.Domain.ToLower()).FirstOrDefault();

            if (domain == null)
            {
                domain = NestSearch(domains, request.Domain);

                if (domain == null)
                {
                    throw new ApiNotFoundException(request.Domain);
                }
            }

            Business.User.SetDomain(loginOutput.User.Id, domain.Id);

            loginOutput.User.DomainId = domain.Id;
            loginOutput.User.Domain   = domain;

            LoginResponse response = new LoginResponse()
            {
                Output = loginOutput
            };

            if (request.Bearer == "true" && loginOutput.User != null)
            {
                string secret = HashHelper.GetSha256(request.Password);

                var p = loginOutput.User.Properties;

                loginOutput.User.Properties.Clear();

                string token = JwtBuilder.Create()
                               .WithAlgorithm(new HMACSHA256Algorithm())
                               .WithSecret(secret)
                               .AddClaim("exp", DateTimeOffset.UtcNow.AddHours(12).ToUnixTimeSeconds())
                               .AddClaim("user", response.Output.User)
                               .Encode();

                logger.Debug($"token generated: {token}");

                Business.User.SaveBearer(loginOutput.User.Id, token);

                response.Bearer = token;

                loginOutput.User.Properties = p;
            }

            input.Context.Session.Set(response.Output.User);

            return(GetOutput(response));
        }