Пример #1
0
        /// <summary>
        /// Generazione del token di autenticazione per l'utente specificato
        /// </summary>
        /// <param name="userId">User id dell'utente proprietario del token</param>
        /// <param name="tokenType">Tipo di token da generare</param>
        /// <param name="milliseconds">Durata del token espressa in millisecondi</param>
        /// <param name="keyLength">Lunghezza della chiave espressa in numero di caratteri</param>
        /// <param name="initializationVectorLength">Lunghezza dell'initialization vector espressa in numero di caratteri</param>
        /// <returns>Token criptato</returns>
        public override string Generate(string userId, AuthTokenManager.TokenTypeEnum tokenType, double milliseconds, AuthTokenManager.KeyLengthEnum keyLength, AuthTokenManager.KeyLengthEnum initializationVectorLength)
        {
            // Chiave privata, vettore di inizializzazione e la userid pulita
            String key, initializationVector, cleanedUserId;

            // Pulizia dello user id
            cleanedUserId = userId.Trim().ToUpper();

            // Decifratura lunghezza della chiave e dell'initialization vector
            int kLength  = this.DecodeKeyLength(keyLength);
            int ivLength = this.DecodeKeyLength(initializationVectorLength);

            // Generazione della chiavi
            key = KeyGeneratorHelper.BetterRandomString(kLength);
            initializationVector = KeyGeneratorHelper.BetterRandomString(ivLength);

            try
            {
                CryptoString crypto       = new CryptoString(key, initializationVector);
                string       encodedValue = crypto.Encrypt(String.Format(ENCRYPTED_VALUE_FORMAT, cleanedUserId, GetSessionId(), this.GetActualDateFromDB(), tokenType, this.GetExpirationDate(tokenType, milliseconds)));

                // Salvataggio token su db
                this.SaveToken(key, encodedValue, initializationVector, cleanedUserId);

                return(string.Format("{0}{1}", TOKEN_PREFIX, encodedValue));
            }
            catch (Exception ex)
            {
                throw new EncryptionException(ex);
            }
        }
Пример #2
0
        /// <summary>
        /// Funzione per la decodifica del tipo di token
        /// </summary>
        /// <param name="tokenType">Tipo token da decodificare</param>
        /// <returns>Tipo token decodificato</returns>
        private AuthTokenManager.TokenTypeEnum DecodeTokenType(String tokenType)
        {
            AuthTokenManager.TokenTypeEnum token = AuthTokenManager.TokenTypeEnum.OneTime;
            if (tokenType == AuthTokenManager.TokenTypeEnum.Temporal.ToString())
            {
                token = AuthTokenManager.TokenTypeEnum.Temporal;
            }
            else
            if (tokenType == AuthTokenManager.TokenTypeEnum.Explicit.ToString())
            {
                token = AuthTokenManager.TokenTypeEnum.Explicit;
            }

            return(token);
        }
Пример #3
0
        /// <summary>
        /// Funzione per calcolo della data di scadenza del token
        /// </summary>
        /// <param name="tokenType">Tipo di token</param>
        /// <param name="milliseconds">Durata in millisecondi del token</param>
        /// <returns>Data di scadenza del token</returns>
        private String GetExpirationDate(AuthTokenManager.TokenTypeEnum tokenType, double milliseconds)
        {
            // Di default la data di scadenza è oggi stesso
            DateTime expirationDate = DateTime.Now;

            switch (tokenType)
            {
            case AuthTokenManager.TokenTypeEnum.OneTime:         // Oggi
                expirationDate = this.GetActualDateFromDB();
                break;

            case AuthTokenManager.TokenTypeEnum.Temporal:        // Fra 'hours' ore
                expirationDate = this.GetActualDateFromDB().AddMilliseconds(milliseconds);
                break;

            case AuthTokenManager.TokenTypeEnum.Explicit:        // Esplicito
                expirationDate = DateTime.MaxValue;
                break;
            }

            return(expirationDate.ToString());
        }