/// <summary> /// Join the name-value pairs into a string seperated with ampersands. /// Each name and value is first RFC 3986 encoded and values are separated /// from names with equal signs. /// </summary> /// <param name="values">The name value collection to encode and join</param> /// <returns>An RFC 3986 compliant string</returns> public static string EncodeAndJoin(NameValueCollection values) { if (values == null) { return(string.Empty); } StringBuilder enc = new StringBuilder(); bool first = true; foreach (string key in values.Keys) { string encKey = Rfc3986.Encode(key); foreach (string value in values.GetValues(key)) { if (!first) { enc.Append("&"); } else { first = false; } enc.Append(encKey).Append("=").Append(Rfc3986.Encode(value)); } } return(enc.ToString()); }
/// <summary> /// Creates a normalized representation of the parameters for use in the signature base string. /// </summary> /// <param name="excludedParameters">Names of parameters to exclude from the normalized string.</param> /// <returns>The signature-base normalized representation of the parameters.</returns> public string ToNormalizedString(params string[] excludedParameters) { var @params = new List <KeyValuePair <string, string> >(); // Add OAuth parameters whose values are not null except excluded parameters foreach (var param in this.parameters.Keys) { if (this.parameters[param] != null && Array.IndexOf <string>(excludedParameters, param) < 0) { @params.Add(new KeyValuePair <string, string>(Rfc3986.Encode(param), Rfc3986.Encode(this.parameters[param]))); } } // Add all additional parameters foreach (var param in this.AdditionalParameters.AllKeys) { foreach (var value in this.AdditionalParameters.GetValues(param) ?? new string[] { }) { @params.Add(new KeyValuePair <string, string>(Rfc3986.Encode(param), Rfc3986.Encode(value))); } } // Sort parameters into lexicographic order (by key and value) @params.Sort( (left, right) => left.Key.Equals(right.Key, StringComparison.Ordinal) ? string.Compare(left.Value, right.Value, StringComparison.Ordinal) : string.Compare(left.Key, right.Key, StringComparison.Ordinal)); // Concatenate and encode string equals = "="; string ampersand = "&"; StringBuilder parms = new StringBuilder(); bool first = true; foreach (var pair in @params) { if (first) { first = false; } else { parms.Append(ampersand); } parms.Append(pair.Key).Append(equals).Append(pair.Value); } return(parms.ToString()); }
/// <summary> /// Serializes a SimpleToken to a string representation. /// </summary> /// <param name="token">Token</param> /// <returns>String serialization of the token</returns> /// <exception cref="System.ArgumentNullException">if <paramref name="token"/> is <c>null</c></exception> public static string Serialize(OAuthToken token) { if (token == null) { throw new ArgumentNullException("token"); } return("[" + Rfc3986.Encode(Enum.Format(typeof(TokenType), token.Type, "G")) + "|" + Rfc3986.Encode(token.Token) + "|" + Rfc3986.Encode(token.Secret) + "|" + Rfc3986.Encode(token.ConsumerKey) + "]"); }
private static void EncodeHeaderValue(StringBuilder buffer, string key, string value, string separator, bool quote) { buffer.Append(separator); buffer.Append(Rfc3986.Encode(key)); buffer.Append("="); if (quote) { buffer.Append('"'); } buffer.Append(Rfc3986.Encode(value)); if (quote) { buffer.Append('"'); } }
public static string Create(string httpMethod, Uri requestUrl, OAuthParameters parameters) { StringBuilder sigbase = new StringBuilder(); // Http header sigbase.Append(Rfc3986.Encode(httpMethod)).Append("&"); // Normalized request URL sigbase.Append(Rfc3986.Encode(requestUrl.Scheme)); sigbase.Append(Rfc3986.Encode("://")); sigbase.Append(Rfc3986.Encode(requestUrl.Authority.ToLowerInvariant())); sigbase.Append(Rfc3986.Encode(requestUrl.AbsolutePath)); sigbase.Append("&"); // Normalized parameters sigbase.Append(Rfc3986.Encode(parameters.ToNormalizedString( Constants.RealmParameter, Constants.SignatureParameter, Constants.TokenSecretParameter))); return(sigbase.ToString()); }
/// <summary> /// Throws an exception indicating unexpected parameter(s) were received. /// </summary> /// /// <remarks> /// <para> /// The <see cref="Problem">problem type</see> is /// <see cref="OAuthRequestExceptionProblemTypes.ParameterRejected">parameter_rejected</see>. /// </para> /// /// <para> /// The <see cref="AdditionalParameter">additional parameter</see> /// (<see cref="OAuthRequestExceptionParameters.ParametersRejected">oauth_parameters_rejected</see>) /// of the exception consists of a set of parameters, encoded as they would be /// in a URL query string. These are parameters that the sender recently /// received but doesn't understand. Note that these parameters will be /// percent-encoded twice: once to form a query string and again because /// the query string is the value of <see cref="OAuthRequestExceptionParameters.ParametersRejected">oauth_parameters_rejected</see>. /// </para> /// /// <para> /// The <paramref name="advice"/> parameter, if supplied, will be /// stored in the <see cref="Advice"/> property. /// </para> /// /// <para> /// The <see cref="Exception.Source"/> will be <see cref="OAuthRequestExceptionSources.Local">local</see>. /// </para> /// </remarks> /// /// <param name="parameters">The parameters that are rejected</param> /// <param name="advice">(Optional) Plain text advice for the user /// of the consumer</param> /// /// <exception cref="System.ArgumentException"> /// If <paramref name="parameters"/> is null or empty. /// </exception> /// <exception cref="OAuth.Net.Common.OAuthRequestException"> /// On success /// </exception> public static void ThrowParametersRejected(string[] parameters, string advice) { if (parameters == null || parameters.Length == 0) { throw new ArgumentException("parameters argument cannot be null or of length 0", "parameters"); } StringBuilder rejectedParameters = new StringBuilder(); bool first = true; foreach (string parameter in parameters) { if (!first) { rejectedParameters.Append("&"); } else { first = false; } rejectedParameters.Append(Rfc3986.Encode(parameter)); } throw new OAuthRequestException() { Problem = OAuthRequestExceptionProblemTypes.ParameterRejected, AdditionalParameter = new KeyValuePair <string, string>( OAuthRequestExceptionParameters.ParametersRejected, rejectedParameters.ToString()), Advice = advice, Source = OAuthRequestExceptionSources.Local }; }