Esempio n. 1
0
        public void TestCryptoString()
        {
            string key = CryptoString.MakeKey();
            string iv  = CryptoString.MakeIV();

            string test = "Test String";

            CryptoString cs = new CryptoString(key, iv);

            string crypto = cs.Encrypt(test);
            string plain  = cs.Decrypt(crypto);

            Assert.AreEqual(test, plain);
        }
Esempio n. 2
0
        /// <summary>
        /// Ripristino del token di autenticazione per l'utente specificato
        /// </summary>
        /// <param name="userId">Identificativo dell'utente proprietario del token</param>
        /// <param name="token">Token criptato</param>
        /// <returns>Token decriptato</returns>
        public override string Restore(string userId, string token)
        {
            if (IsAuthToken(token))
            {
                // Pulizia del token
                String cleanedToken = token.Replace(TOKEN_PREFIX, String.Empty);

                // Pulizia e della user id
                String cleanedUserId = userId.Trim().ToUpper();

                // Recupero delle informazioni di encryption dal DB
                CryptoString crypto = this.LoadToken(cleanedToken);

                String decryptedValue = String.Empty;
                try
                {
                    decryptedValue = crypto.Decrypt(cleanedToken);
                }
                catch (Exception ex)
                {
                    throw new DecryptionException(ex);
                }

                // Spacchettamento del token
                Dictionary <String, String> keyValuePairs = GetTokenKeyValuePairs(decryptedValue);

                // Controllo della validità del token
                bool expired = this.IsExpiredToken(keyValuePairs[TOKENEXPIRATIONDATE]);

                // Se il token è scaduto, viene cancellato il record dalla tabella
                if (expired)
                {
                    this.RemoveTokenFromDB(cleanedToken);
                }

                if (String.Compare(cleanedUserId, keyValuePairs[UID], true) == 0 && (!expired || this.DecodeTokenType(keyValuePairs[TOKENTYPE]) == AuthTokenManager.TokenTypeEnum.OneTime))
                {
                    return(keyValuePairs[SESSIONID]);
                }
                else
                {
                    throw new NoAuthenticationTokenForUserException(userId);
                }
            }
            else
            {
                throw new AuthenticationTokenNotValidException();
            }
        }
Esempio n. 3
0
        public void TestCryptoStringDefaultConstructor()
        {
            string key = CryptoString.MakeKey();
            string iv  = CryptoString.MakeIV();

            string test = "Test String";

            CryptoString cs = new CryptoString
            {
                Key = key,
                IV  = iv
            };

            string crypto = cs.Encrypt(test);
            string plain  = cs.Decrypt(crypto);

            Assert.AreEqual(test, plain);
        }
Esempio n. 4
0
        public void TestCryptoStringNoKeyEncrypt()
        {
            string test = "Test String";

            CryptoString cs = new CryptoString();

            try
            {
                string crypto = cs.Encrypt(test);
                string plain  = cs.Decrypt(crypto);

                Assert.Fail("Should have thrown an exception");
            }
            catch (Exception ex)
            {
                Assert.AreEqual("savedKey and savedIV must be non-null.", ex.Message);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Funzione per la rimozione del token specificato
        /// </summary>
        /// <param name="userId">Id dell'utente proprietario del token</param>
        /// <param name="token">Token da eliminare</param>
        public override void RemoveToken(String userId, String token)
        {
            if (IsAuthToken(token))
            {
                // Pulizia del token
                String cleanedToken = token.Replace(TOKEN_PREFIX, String.Empty);

                // Recupero delle informazioni di encryption dal DB
                CryptoString crypto = this.LoadToken(cleanedToken);

                string decryptedValue = string.Empty;

                try
                {
                    decryptedValue = crypto.Decrypt(cleanedToken);
                }
                catch (Exception ex)
                {
                    throw new DecryptionException(ex);
                }

                // Spacchettamento delle informazioni
                Dictionary <string, string> keyValuePairs = GetTokenKeyValuePairs(decryptedValue);

                // Se user id passato per parametro e user id presente nel token non corrispondono, non si può procedere
                if (!userId.Trim().ToUpper().Equals(keyValuePairs[UID]))
                {
                    throw new NoAuthenticationTokenForUserException(userId);
                }

                // Rimozione del token dal DB
                this.RemoveTokenFromDB(cleanedToken);
            }
            else
            {
                throw new AuthenticationTokenNotValidException();
            }
        }