public string GenerateTemporalToken(String userId, double milliseconds, AuthTokenManager.KeyLengthEnum keyLength, AuthTokenManager.KeyLengthEnum initializationVectorLength) { this.InitializeSecurityToken(); try { return(_authTokenManager.Generate( userId, AuthTokenManager.TokenTypeEnum.Temporal, milliseconds, keyLength, initializationVectorLength)); } catch (Exception e) { throw SoapExceptionFactory.Create(e); } }
public string GenerateOneTimeToken(string userId, AuthTokenManager.KeyLengthEnum keyLength, AuthTokenManager.KeyLengthEnum initializationVectorLength) { this.InitializeSecurityToken(); try { return(_authTokenManager.Generate( userId, AuthTokenManager.TokenTypeEnum.OneTime, TimeSpan.Zero.TotalMilliseconds, keyLength, initializationVectorLength)); } catch (Exception e) { throw SoapExceptionFactory.Create(e); } }
/// <summary> /// Funzione per l'interpretazione del numero di caratteri di cui deve essere composta la chiave /// </summary> /// <param name="keyLength">Lunghezza della chiave da interpretare</param> /// <returns>Numero di caratteri decodificato</returns> private int DecodeKeyLength(AuthTokenManager.KeyLengthEnum keyLength) { int length; switch (keyLength) { case AuthTokenManager.KeyLengthEnum.Sixteen: length = 16; break; case AuthTokenManager.KeyLengthEnum.ThirtyTwo: length = 32; break; default: length = 16; break; } return(length); }
/// <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); } }