Esempio n. 1
0
        private bool ValidateCredentials(string password, Guid userId)
        {
            var user = UserRepository.GetById(userId);

            if (string.IsNullOrEmpty(user?.PasswordHash))
            {
                return(false);
            }
            var hashPassword =
                HashCryptographyHelper.GetSaltPassword(HashCryptographyHelper.GetSha512Hash(password),
                                                       user.Salt);

            return(hashPassword.Equals(user.PasswordHash));
        }
Esempio n. 2
0
        //private IBaseItemRepository BaseItemRepository { get; }
        //private IGroupToUserRepository GroupToUserRepository { get; }
        //private IGroupRepository GroupRepository { get; }
        //private ITokenHelper TokenHelper { get; }

        RegistrationResultModel IAuthHelper.RegistrationUser(RegistrationRequestModel <SiteAuthModel> model)
        {
            var salt      = HashCryptographyHelper.GetSalt();
            var userModel = new UserModel
            {
                Email        = model.Data.Email,
                PasswordHash = !string.IsNullOrEmpty(model.Data.Password)
                    ? HashCryptographyHelper.GetSaltPassword(
                    HashCryptographyHelper.GetSha512Hash(model.Data.Password), salt
                    )
                    : null,
                Salt = salt
            };

            return(Registration(userModel, true, model.Groups));
        }
Esempio n. 3
0
        private UserSessionModel CreateSession(Guid userId, IEnumerable <Enums.Group> groups)
        {
            var expirationPeriod        = ConfigurationHelper.AccessTokenExpiratedPeriod;
            var expirationRefreshPeriod = ConfigurationHelper.RefreshTokenExpiratedPeriod;
            var userCacheModel          = new UserAuthModel
            {
                UserId = userId,
                Groups = groups.ToArray()
            };
            var accessToken = new AccessTokenModel
            {
                AccessToken    = HashCryptographyHelper.GetSha512Hash(HashCryptographyHelper.GetPassword(16)),
                ExpirationTime = DateTime.Now.Add(expirationPeriod).UnixDateTime(),
                UserId         = userId
            };

            var refreshToken = new RefreshTokenModel
            {
                RefreshToken   = HashCryptographyHelper.GetSha512Hash(HashCryptographyHelper.GetPassword(16)),
                AccessToken    = TokenHelper.CreateJwtToken(accessToken, ConfigurationHelper.JwtPublicKey),
                ExpirationTime = DateTime.Now.Add(expirationRefreshPeriod).UnixDateTime()
            };
            var model = new UserSessionModel
            {
                UserModel      = userCacheModel,
                AccessToken    = TokenHelper.CreateJwtToken(accessToken, ConfigurationHelper.JwtPublicKey),
                RefreshToken   = TokenHelper.CreateJwtToken(refreshToken, ConfigurationHelper.JwtPublicKey),
                ExpirationTime = accessToken.ExpirationTime
            };

            CacheStoreHelper.Add(CommonConstants.AccessTokenPrefix + model.AccessToken, userCacheModel,
                                 expirationPeriod);
            CacheStoreHelper.Add(CommonConstants.RefreshTokenPrefix + model.RefreshToken, userCacheModel,
                                 expirationRefreshPeriod);
            return(model);
        }