Пример #1
0
        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var clientid = context.Ticket.Properties.Dictionary[clientPropertyKey];

            if (string.IsNullOrEmpty(clientid))
            {
                return;
            }

            var refreshTokenId = Guid.NewGuid().ToString("n");

            var refreshTokenLifeTime = context.OwinContext.Get <string>("as:clientRefreshTokenLifeTime");

            var token = new RefreshToken()
            {
                Id         = HashMaker.GetHash(refreshTokenId),
                ClientId   = clientid,
                Subject    = context.Ticket.Identity.Name,
                IssuedUtc  = DateTime.UtcNow,
                ExpiresUtc = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime))
            };

            context.Ticket.Properties.IssuedUtc  = token.IssuedUtc;
            context.Ticket.Properties.ExpiresUtc = token.ExpiresUtc;

            token.ProtectedTicket = context.SerializeTicket();

            var result = await authRepository.AddRefreshToken(token);

            if (result)
            {
                context.SetToken(refreshTokenId);
            }
        }
Пример #2
0
        public Tuple <ResultCode, UserInfoDb> Refine(string userInput)
        {
            var hashMaker  = new HashMaker();
            var resultCode = ResultCode.SUCCESS;
            var userInfoDb = new UserInfoDb();

            try
            {
                // Parsing
                var signInRequest = JsonConvert.DeserializeObject <RequestSignIn>(userInput);

                // Refining
                var    salt       = hashMaker.GetSalt();
                var    pwd        = signInRequest.Password;
                string saltAndPwd = string.Concat(pwd, salt);
                string hashedPwd  = hashMaker.GetSha256Hash(saltAndPwd);

                // Mapping
                userInfoDb.UserId         = signInRequest.UserId.ToLower();
                userInfoDb.Email          = signInRequest.Email;
                userInfoDb.Salt           = salt;
                userInfoDb.HashedPassword = hashedPwd;
                userInfoDb.HeroCollection = new List <int>();

                var ret = new Tuple <ResultCode, UserInfoDb>(resultCode, userInfoDb);

                return(ret);
            }
            catch (Exception)
            {
                resultCode = ResultCode.LCN0002;
                var ret = new Tuple <ResultCode, UserInfoDb>(resultCode, userInfoDb);
                return(ret);
            }
        }
Пример #3
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity <User>().ToTable("Users");
            builder.Entity <User>().HasKey(p => p.Id);
            builder.Entity <User>().Property(p => p.Email).IsRequired();
            builder.Entity <User>().Property(p => p.Phone).IsRequired();
            builder.Entity <User>().Property(p => p.GivenName).IsRequired();
            builder.Entity <User>().Property(p => p.Surname).IsRequired();
            builder.Entity <User>().Property(p => p.PasswordHash).IsRequired();
            builder.Entity <User>().Property(p => p.MiddleName);
            builder.Entity <User>().HasMany(p => p.Files).WithOne(p => p.Owner).HasForeignKey(p => p.OwnerID);

            builder.Entity <User>().HasData(
                new User {
                Id           = Guid.NewGuid(),
                GivenName    = "Egor",
                Surname      = "Solodaev",
                MiddleName   = "",
                Email        = "*****@*****.**",
                Phone        = "+13371488",
                PasswordHash = HashMaker.GetHash("mypassword")
            }
                );

            builder.Entity <FileRecord>().ToTable("Files");
            builder.Entity <FileRecord>().HasKey(p => p.Id);
            builder.Entity <FileRecord>().Property(p => p.Id).IsRequired().ValueGeneratedOnAdd();
            builder.Entity <FileRecord>().Property(p => p.Name).IsRequired();
            builder.Entity <FileRecord>().Property(p => p.Path).IsRequired();
        }
Пример #4
0
        public ChainHashTable()
        {
            var capacity = _primeNumber.Next();

            _table     = new List <Pair <TKey, TValue> > [capacity];
            _hashMaker = new HashMaker <TKey>(capacity);
        }
Пример #5
0
        public ChainHashTable(int primeNum)
        {
            var capacity = _primeNumber.Next();

            while (capacity < primeNum)
            {
                capacity = _primeNumber.Next();
            }
            _table     = new List <Pair <TKey, TValue> > [primeNum];
            _hashMaker = new HashMaker <TKey>(primeNum);
        }
Пример #6
0
        public async Task <TokenResponse> CreateAccessTokenByPhoneAsync(string phone, string password)
        {
            var user = await _userService.FindUserByPhoneAsync(phone);

            if (user == null || !HashMaker.VerifyHash(password, user.PasswordHash))
            {
                return(new TokenResponse(false, "Invalid credentials.", null));
            }

            var token = _tokenHandler.CreateAccessToken(user);

            return(new TokenResponse(true, null, token));
        }
Пример #7
0
        public string GetHash(HashType hashType)
        {
            CheckInternalState();

            string     output = string.Empty;
            IHashMaker hasher = new HashMaker();

            switch (hashType)
            {
            case HashType.MD5:
                if (Settings.Default.HoldBufferInMemory)
                {
                    output = hasher.GetMD5(_InternalBuffer);
                }
                else
                {
                    output = hasher.GetMD5(_InternalFilePath);
                }
                break;

            case HashType.SHA1:
                if (Settings.Default.HoldBufferInMemory)
                {
                    output = hasher.GetSHA1(_InternalBuffer);
                }
                else
                {
                    output = hasher.GetSHA1(_InternalFilePath);
                }
                break;

            case HashType.SHA256:
                if (Settings.Default.HoldBufferInMemory)
                {
                    output = hasher.GetSHA256(_InternalBuffer);
                }
                else
                {
                    output = hasher.GetSHA256(_InternalFilePath);
                }
                break;

            case HashType.PreHash:
                output = hasher.GetPreHash(_InternalFilePath);
                break;
            }

            return(output);
        }
Пример #8
0
        public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
        {
            var allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin");

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

            var hashedTokenId = HashMaker.GetHash(context.Token);

            var refreshToken = await authRepository.FindRefreshToken(hashedTokenId);

            if (refreshToken != null)
            {
                // Get protectedTicket from refreshToken class
                context.DeserializeTicket(refreshToken.ProtectedTicket);

                var result = await authRepository.RemoveRefreshToken(hashedTokenId);
            }
        }
Пример #9
0
        static void Main(string[] args)
        {
            int totalProcessed = 0;
            int totalHashed    = 0;
            int totalFailed    = 0;


            IEnumerable <FilesDueForHash_Result> filesToHash = GetNextFiles();

            do
            {
                Parallel.ForEach(filesToHash, fileToHash =>
                {
                    Console.WriteLine("Processing: " + fileToHash.FullPath);

                    try
                    {
                        if (File.Exists(fileToHash.FullPath) &&
                            fileToHash.Length > 0)
                        {
                            Stream fileStream = File.OpenRead(fileToHash.FullPath);

                            IHashMaker hasher = new HashMaker();

                            string preHash = hasher.GetPreHash(fileToHash.FullPath);
                            string md5     = hasher.GetMD5(fileStream);
                            string sha1    = hasher.GetSHA1(fileStream);

                            using (DbModelContainer db = new DbModelContainer())
                            {
                                db.Database.CommandTimeout = 60;

                                TrackedFile fileToUpdate = db.TrackedFiles.Find(fileToHash.Id);
                                fileToUpdate.PreHash     = preHash;
                                fileToUpdate.MD5         = md5;
                                fileToUpdate.SHA1        = sha1;

                                db.SaveChanges();
                            }

                            Interlocked.Increment(ref totalHashed);
                        }
                    }
                    catch (Exception)
                    {
                        //mark it as failed
                        Interlocked.Increment(ref totalFailed);
                    }
                    finally
                    {
                        Interlocked.Increment(ref totalProcessed);

                        using (DbModelContainer db = new DbModelContainer())
                        {
                            db.Database.CommandTimeout = 60;
                            TrackedFile file           = db.TrackedFiles.Find(fileToHash.Id);
                            file.HashAttempted         = DateTime.Now;
                            db.SaveChanges();
                        }
                    }
                });

                filesToHash = GetNextFiles();
            } while (filesToHash.Count() > 0);
        }