/// <summary>
 /// Initializes a new instance of the <see cref="SecurityToken"/> class.
 /// </summary>
 /// <param name="settings">The settings to apply to this instance.</param>
 SecurityToken(SecurityTokenSettings settings)
 {
     Validator.ThrowIfNull(settings, nameof(settings));
     Settings   = settings;
     UtcCreated = DateTime.UtcNow;
     Token      = StringUtility.CreateRandomString(settings.LengthOfToken);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="SecurityToken"/> class.
        /// </summary>
        /// <param name="securityToken">The security token as its string equivalent.</param>
        SecurityToken(string securityToken)
        {
            Validator.ThrowIfNullOrEmpty(securityToken, nameof(securityToken));
            Validator.ThrowIfLowerThan(securityToken.Length, 34, nameof(securityToken));

            string[] tokenSegments = StringUtility.Split(securityToken, ";");
            Validator.ThrowIfLowerThan(tokenSegments.Length, 4, nameof(securityToken));
            Validator.ThrowIfGreaterThan(tokenSegments.Length, 5, nameof(securityToken));

            TimeSpan timeToLive = new TimeSpan(long.Parse(tokenSegments[0].Trim('"'), CultureInfo.InvariantCulture));
            DateTime utcCreated = DateTime.Parse(tokenSegments[1].Trim('"'), CultureInfo.InvariantCulture).ToUniversalTime();
            string   token      = tokenSegments[2].Trim('"');
            string   reference  = tokenSegments[3].Trim('"');

            SecurityTokenSettings settings = new SecurityTokenSettings(timeToLive, token.Length, reference);

            Settings   = settings;
            Token      = token;
            UtcCreated = utcCreated;
        }
 /// <summary>
 /// Creates and returns a new <see cref="SecurityToken"/> from the specified <paramref name="settings"/>.
 /// </summary>
 /// <param name="settings">The settings to apply to the <see cref="SecurityToken"/> instance.</param>
 /// <returns>A new <see cref="SecurityToken"/> instance.</returns>
 public static SecurityToken Create(SecurityTokenSettings settings)
 {
     return(new SecurityToken(settings));
 }
        /// <summary>
        /// Creates a security token easily adopted into various services in the format of the <see cref="SecurityToken.ToString()"/> method.
        /// The token itself is encrypted using the <see cref="AdvancedEncryptionStandardUtility.Encrypt"/> method.
        /// </summary>
        /// <param name="settings">The to apply to <see cref="SecurityToken"/> instance.</param>
        /// <param name="securityKey">The key to use in the encryption algorithm.</param>
        /// <param name="securityInitializationVector">The initialization vector (IV) to use in the encryption algorithm.</param>
        /// <returns>A security token easily adopted into various services in the format of the <see cref="SecurityToken.ToString()"/> method.</returns>
        public static byte[] CreateEncryptedSecurityToken(SecurityTokenSettings settings, byte[] securityKey, byte[] securityInitializationVector)
        {
            SecurityToken token = SecurityToken.Create(settings);

            return(CreateEncryptedSecurityToken(token, securityKey, securityInitializationVector));
        }
 /// <summary>
 /// Creates a security token easily adopted into various services in the format of the <see cref="SecurityToken.ToString()"/> method.
 /// The token itself is encrypted using the <see cref="AdvancedEncryptionStandardUtility.Encrypt"/>
 /// </summary>
 /// <param name="settings">The to apply to <see cref="SecurityToken"/> instance.</param>
 /// <param name="securityKey">The key to use in the encryption algorithm.</param>
 /// <param name="securityInitializationVector">The initialization vector (IV) to use in the encryption algorithm.</param>
 /// <returns>A security token easily adopted into various services in the format of the <see cref="SecurityToken.ToString()"/> method.</returns>
 public static byte[] CreateEncryptedSecurityToken(SecurityTokenSettings settings, string securityKey, string securityInitializationVector)
 {
     return(CreateEncryptedSecurityToken(settings, securityKey, securityInitializationVector, Encoding.UTF8));
 }