public void Test_CreateCustomer()
        {
            var options = new DbContextOptionsBuilder <CustomerContext>()
                          .UseInMemoryDatabase(databaseName: "TestDb25")
                          .Options;

            using (var context = new CustomerContext(options))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                CustomerRepository custRepo = new CustomerRepository(context);
                CustomerLogic      cl       = new CustomerLogic(custRepo);

                RawCustomer rCust = new RawCustomer();
                rCust.Email    = "*****@*****.**";
                rCust.Fname    = "Testing";
                rCust.Lname    = "Test";
                rCust.Password = "******";

                Customer newCust = cl.CreateCustomer(rCust);

                Assert.Equal(rCust.Email, newCust.Email);
            }
        }
        public ActionResult <Customer> Register(RawCustomer rawCustomer)
        {
            Customer customer = new Customer();

            if (!ModelState.IsValid)
            {
                return(StatusCode(400, "Bad Request"));
            }

            if (customer == null)
            {
                return(StatusCode(409, "Not Acceptable"));
            }
            return(customer);
        }
 public ActionResult <string> Login([FromBody] RawCustomer obj)
 {
     if (!ModelState.IsValid)
     {
         return(StatusCode(400, "Failed to create models"));
     }
     else
     {
         Customer curCust = customerLogic.LoginCheck(obj);
         if (curCust is null)
         {
             return(StatusCode(450, "Invalid information"));
         }
         return(JsonSerializer.Serialize <Customer>(curCust));
     }
 }
Exemplo n.º 4
0
        public Customer CreateCustomer(RawCustomer obj)
        {
            Customer newCustomer;

            if (customerRepo.IsExistingAccount(obj.Email.ToLower()))
            {
                return(null);
            }
            else
            {
                newCustomer = mapper.CustomerMapper(obj);

                customerRepo.AddNewCustomer(newCustomer);
            }

            return(newCustomer);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Maps a RawCustomer to Customer(javascript -> database)
        /// Hashes the inputted password and stores the hash and salt
        /// </summary>
        /// <param name="obj">RawCustomer</param>
        /// <returns></returns>
        public Customer CustomerMapper(RawCustomer obj)
        {
            using (var hmac = new HMACSHA512())
            {
                Customer newCustomer = new Customer();
                newCustomer.Fname       = obj.Fname;
                newCustomer.Lname       = obj.Lname;
                newCustomer.Email       = obj.Email.ToLower();
                newCustomer.LastStore   = Guid.Empty;
                newCustomer.StoreManger = Guid.Empty;

                newCustomer.PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(obj.Password)); //this returns a byte[] representing the password
                newCustomer.PasswordSalt = hmac.Key;                                               // this assigns the randomly generated Key (comes with the HMAC instance) to the salt variable of the user instance,

                return(newCustomer);
            }
        }
 public ActionResult <string> Create([FromBody] RawCustomer obj)
 {
     if (!ModelState.IsValid)
     {
         return(StatusCode(400, "Failed to create models"));
     }
     else
     {
         Customer cust = customerLogic.CreateCustomer(obj);
         if (cust is null)
         {
             return(StatusCode(450, "Failed to create. Possibly already exists"));
         }
         return(JsonSerializer.Serialize <Customer>(cust));
     }
     //return StatusCode(200, "Success or nothing happened");
 }
        public Customer Register(RawCustomer rawCustomer)
        {
            if (_repolayer.UserExists(rawCustomer.Username) == true)
            {
                return(null);
            }
            else
            {
                Customer newCustomer = mapper.GetANewCustomerWithHashedPassword(rawCustomer.Password);

                newCustomer.Fname    = rawCustomer.Fname;
                newCustomer.Lname    = rawCustomer.Lname;
                newCustomer.Username = rawCustomer.Username;
                Customer registeredCustomer = _repolayer.Register(newCustomer);
                return(registeredCustomer);
            }
        }
        public void Test_LoginCheck()
        {
            var options = new DbContextOptionsBuilder <CustomerContext>()
                          .UseInMemoryDatabase(databaseName: "TestDb26")
                          .Options;

            using (var context = new CustomerContext(options))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                CustomerRepository custRepo = new CustomerRepository(context);
                CustomerLogic      cl       = new CustomerLogic(custRepo);

                string   password    = "******";
                Customer newCustomer = new Customer();
                using (var hmac = new HMACSHA512())
                {
                    newCustomer.Fname       = "Testing";
                    newCustomer.Lname       = "Test";
                    newCustomer.Email       = "*****@*****.**".ToLower();
                    newCustomer.LastStore   = Guid.Empty;
                    newCustomer.StoreManger = Guid.Empty;

                    newCustomer.PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password));
                    newCustomer.PasswordSalt = hmac.Key;
                }

                context.Add <Customer>(newCustomer);
                context.SaveChanges();

                RawCustomer rCust = new RawCustomer();
                rCust.Email    = "*****@*****.**";
                rCust.Fname    = "Testing";
                rCust.Lname    = "Test";
                rCust.Password = password;

                Customer newCust = cl.LoginCheck(rCust);

                Assert.Equal(newCustomer.Email, newCust.Email);
            }
        }
Exemplo n.º 9
0
        public Customer LoginCheck(RawCustomer obj)
        {
            if (!customerRepo.IsExistingAccount(obj.Email.ToLower()))
            {
                return(null);
            }
            else
            {
                byte[] originalSalt     = customerRepo.GetPasswordSalt(obj.Email.ToLower());
                byte[] originalPassword = customerRepo.GetHashedPassword(obj.Email.ToLower());
                byte[] currentPassword  = mapper.PasswordHash(obj.Password, originalSalt);

                if (CompareHash(originalPassword, currentPassword))
                {
                    return(customerRepo.GetCustomerByEmail(obj.Email.ToLower()));
                }
                else
                {
                    return(null);
                }
            }
        }