public CompositeTypeDecrypt Decrypt(string tokenUser, string tokenApi, List <byte[]> files)
        {
            #region Verification
            // Check Token User
            using (var dbContext = new UserTokenDBContext())
                if (!new DatabaseTokenValidator(dbContext).IsValid(tokenUser))
                {
                    return new CompositeTypeDecrypt {
                               IsDecrypted = false, InfoMessage = "User token invalid"
                    }
                }
            ;

            // Check number of files
            if (files.Count == 0)
            {
                return new CompositeTypeDecrypt {
                           IsDecrypted = false, InfoMessage = "No file selected"
                }
            }
            ;

            #endregion

            var           final       = new CompositeTypeDecrypt();
            List <string> filesString = ConvertFromByteArrayToString(files);
            //List<string> filesString = ConvertBinaryToString(files);

            // Files decryption
            var decryption = new DecryptXOR <string>();
            final.IsDecrypted = decryption.DoWork(filesString);

            if (final.IsDecrypted)
            {
                var response = JsonConvert.DeserializeObject <Model.DecryptMessageResponse>(DecryptXOR <string> .FinalMessage);

                final.Email      = response.Mail;
                final.Key        = response.Key;
                final.Confidence = response.Confidence;
                // Send mail
                var userMail = new UserTokenDBContext().Token.Where(t => t.Token1.Equals(tokenUser)).Select(u => u.User.Mail).FirstOrDefault();
                Communication <string> .SendMail(userMail, "Subject", "The mail you are looking for is " + final.Email + "using the key : " + final.Key);
            }

            final.InfoMessage = decryption.InformationMessage;
            DecryptXOR <string> .IsDecrypted = false;
            return(final);
        }
        public List <string> GetCurrentUsers(string tokenUser, string tokenApi)
        {
            // Check Token User
            using (var dbContext = new UserTokenDBContext())
                if (!new DatabaseTokenValidator(dbContext).IsValid(tokenUser))
                {
                    return(null);
                }
            List <string> currentUsers = new List <string>();

            using (var dbContext = new UserTokenDBContext())
            {
                var validator = new Business.DatabaseTokenValidator(dbContext);
                currentUsers = dbContext.Token.Where(t => DbFunctions.DiffSeconds(t.CreateTime, DateTime.Now) < DefaultSecondsUntilTokenExpires).Select(t => t.User.Username).ToList();
            }
            return(currentUsers);
        }
Ejemplo n.º 3
0
        public CompositeTypeAuthenticate Authenticate(string username, string password, string tokenApi)
        {
            var         final = new CompositeTypeAuthenticate();
            Credentials creds = new Credentials(username, password, tokenApi);

            //Check in DB if user is valid
            using (var dbContext = new UserTokenDBContext())
            {
                ICredentialsValidator validator = new DatabaseCredentialsValidator(dbContext);
                if (validator.IsValid(creds))
                {
                    final.IsAuthentified = true;
                    final.TokenUser      = new DatabaseTokenBuilder(dbContext).Build(creds);
                }
                else
                {
                    final.IsAuthentified = false;
                    //throw new InvalidCredentialException("Invalid credentials");
                }
            }
            return(final);
        }
Ejemplo n.º 4
0
 public DatabaseTokenValidator(UserTokenDBContext dbContext)
 {
     _DBContext = dbContext;
 }
Ejemplo n.º 5
0
 public DatabaseTokenBuilder(UserTokenDBContext dbContext)
 {
     _DBContext = dbContext;
 }
Ejemplo n.º 6
0
 public DatabaseCredentialsValidator(UserTokenDBContext dbContext)
 {
     _DBContext = dbContext;
 }