Ejemplo n.º 1
0
        /// <summary>
        /// Splits a ampersand-separated list of key-value pairs, decodes the keys and
        /// values, and adds them to a NameValueCollection. Keys and values are separated
        /// by equals signs.
        /// </summary>
        /// <param name="input">The key-value pair list</param>
        /// <returns>A name value collection, which may be empty.</returns>
        /// <exception cref="System.FormatException">
        /// If the string is not a series of key-value pairs separated by ampersands,
        /// or if one of the keys is null or empty, or if one of the keys or values is
        /// not properly encoded.
        /// </exception>
        public static NameValueCollection SplitAndDecode(string input)
        {
            NameValueCollection parameters = new NameValueCollection();

            if (string.IsNullOrEmpty(input))
            {
                return(parameters);
            }

            foreach (string pair in input.Split('&'))
            {
                string[] parts = pair.Split('=');

                if (parts.Length != 2)
                {
                    throw new FormatException("Pair is not a key-value pair");
                }

                string key = Rfc3986.Decode(parts[0]);
                if (string.IsNullOrEmpty(key))
                {
                    throw new FormatException("Key cannot be null or empty");
                }

                string value = Rfc3986.Decode(parts[1]);

                parameters.Add(key, value);
            }

            return(parameters);
        }
Ejemplo n.º 2
0
        /*
         * Check for an OAuth Authorization HTTP header and, if present, parse it
         * and add it to the collection
         */
        private static NameValueCollection ParseAuthHeader(string authHeader)
        {
            if (!string.IsNullOrEmpty(authHeader))
            {
                NameValueCollection @params = new NameValueCollection();

                // Check for OAuth auth-scheme
                Match authSchemeMatch = Constants.OAuthCredentialsRegex.Match(authHeader);
                if (authSchemeMatch.Success)
                {
                    // We have OAuth credentials in the Authorization header; parse the parts
                    // Sad-to-say, but this code is much simpler than the regex for it!
                    string[] authParameterValuePairs = authHeader.Substring(authSchemeMatch.Length)
                                                       .Split(',');

                    foreach (string authParameterValuePair in authParameterValuePairs)
                    {
                        string[] parts = authParameterValuePair.Trim().Split('=');

                        if (parts.Length == 2)
                        {
                            string parameter = parts[0];
                            string value     = parts[1];

                            if (value.StartsWith("\"", StringComparison.Ordinal) && value.EndsWith("\"", StringComparison.Ordinal))
                            {
                                value = value.Substring(1, value.Length - 2);

                                try
                                {
                                    value = Constants.StringEscapeSequence.Replace(
                                        value,
                                        (Match match) =>
                                    {
                                        Group group = match.Groups[1];
                                        if (group.Length == 1)
                                        {
                                            switch (group.Value)
                                            {
                                            case "\"": return("\"");

                                            case "'": return("'");

                                            case "\\": return("\\");

                                            case "0": return("\0");

                                            case "a": return("\a");

                                            case "b": return("\b");

                                            case "f": return("\f");

                                            case "n": return("\n");

                                            case "r": return("\r");

                                            case "t": return("\t");

                                            case "v": return("\v");
                                            }
                                        }

                                        return(string.Format(
                                                   CultureInfo.InvariantCulture,
                                                   "{0}",
                                                   char.Parse(group.Value)));
                                    });
                                }
                                catch (FormatException)
                                {
                                    continue;
                                }

                                // Add the parameter and value
                                @params.Add(Rfc3986.Decode(parameter), Rfc3986.Decode(value));
                            }
                        }
                    }
                }

                return(@params);
            }

            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// De-serializes a SimpleToken from its serialized form.
        /// </summary>
        /// <param name="serializedForm">Serialized token</param>
        /// <returns>Token</returns>
        /// <exception cref="System.ArgumentException">
        /// if <paramref name="serializedForm"/> is null or empty
        /// </exception>
        /// <exception cref="System.FormatException">
        /// if <paramref name="serializedForm"/> is not a valid serialized form
        /// </exception>
        public static OAuthToken Deserialize(string serializedForm)
        {
            if (string.IsNullOrEmpty(serializedForm))
            {
                throw new ArgumentException("serializedForm argument must not be null or empty", "serializedForm");
            }

            if (!serializedForm.StartsWith("[", StringComparison.Ordinal))
            {
                throw new FormatException("Serialized SimpleToken must start with [");
            }

            if (!serializedForm.EndsWith("]", StringComparison.Ordinal))
            {
                throw new FormatException("Serialized SimpleToken must end with ]");
            }

            string[] parts = serializedForm.Substring(1, serializedForm.Length - 2)
                             .Split(new char[] { '|' }, StringSplitOptions.None);

            if (parts.Length != 4)
            {
                throw new FormatException("Serialized SimpleToken must consist of 4 pipe-separated fields");
            }

            if (string.IsNullOrEmpty(parts[0]))
            {
                throw new FormatException("Error deserializing SimpleToken.Type (field 0): cannot be null or empty");
            }

            TokenType type;

            try
            {
                type = (TokenType)Enum.Parse(typeof(TokenType), Rfc3986.Decode(parts[0]), true);
            }
            catch (Exception e)
            {
                throw new FormatException("Error deserializing SimpleToken.Type (field 0)", e);
            }

            if (string.IsNullOrEmpty(parts[1]))
            {
                throw new FormatException("Error deserializing SimpleToken.Token (field 1): cannot be null or empty");
            }

            string token;

            try
            {
                token = Rfc3986.Decode(parts[1]);
            }
            catch (Exception e)
            {
                throw new FormatException("Error deserializing SimpleToken.Token (field 1)", e);
            }

            string secret;

            try
            {
                secret = Rfc3986.Decode(parts[2]);
            }
            catch (Exception e)
            {
                throw new FormatException("Error deserializing SimpleToken.Secret (field 2)", e);
            }

            string consumerKey;

            try
            {
                consumerKey = Rfc3986.Decode(parts[3]);
            }
            catch (Exception e)
            {
                throw new FormatException("Error deserializing SimpleToken.ConsumerKey (field 3)", e);
            }

            return(new OAuthToken(type, token, secret, consumerKey));
        }