コード例 #1
0
        public virtual string GetFormEncodedQueryFormat(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
            bool skipDelimiter = true;

            foreach (string key in message.Parameters.Keys)
            {
                if (message.Parameters[key] != null)
                {
                    if (!skipDelimiter)
                    {
                        strBuilder.Append("&");
                    }
                    strBuilder.Append(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}", new object[]
                    {
                        key,
                        WebUtility.UrlEncode(message.Parameters[key])
                    }));
                    skipDelimiter = false;
                }
            }
            return(strBuilder.ToString());
        }
コード例 #2
0
        /// <summary>
        /// Generates a Uri to redirect the user's browser to, along with some OAuth paramters in the query string.
        /// </summary>
        /// <param name="message">The incoming request message.</param>
        /// <param name="authorizationCode">The authorization code.</param>
        /// <returns>The redirect Uri.</returns>
        public static string GetCodeResponseUri(this OAuthMessage message, string authorizationCode)
        {
            string state;

            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            string redirectUri = message.Parameters[OAuthConstants.RedirectUri];

            if (redirectUri == null)
            {
                throw new InvalidOperationException(OAuthConstants.RedirectUri + " cannot be null");
            }

            Dictionary <string, string> responseParameters = new Dictionary <string, string>();

            responseParameters.Add(OAuthConstants.Code, authorizationCode);
            state = message.Parameters[OAuthConstants.State];
            if (state != null)
            {
                responseParameters.Add(OAuthConstants.State, state);
            }

            return(redirectUri + CreateQueryString(responseParameters));
        }
コード例 #3
0
        public virtual void Write(OAuthMessage message, System.IO.Stream requestStream)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (requestStream == null)
            {
                throw new ArgumentNullException("requestStream");
            }

            System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(requestStream);
            AccessTokenRequest     atRequestMsg = message as AccessTokenRequest;

            if (atRequestMsg != null)
            {
                streamWriter.Write(this.GetFormEncodedQueryFormat(message));
                streamWriter.Flush();
                return;
            }

            AccessTokenResponse atResponseMsg = message as AccessTokenResponse;

            if (atResponseMsg != null)
            {
                streamWriter.Write(this.GetJsonEncodedFormat(message));
                streamWriter.Flush();
                return;
            }

            throw new OAuthMessageException(string.Format(Resources.ID3724, message.GetType()));
        }
コード例 #4
0
        public virtual string GetJsonEncodedFormat(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var serializedMessage = JsonConvert.SerializeObject(message.Parameters);

            // TODO: replace token of array to object...
            return(serializedMessage);
        }
コード例 #5
0
        public virtual string GetQueryStringFormat(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
            strBuilder.Append(message.BaseUri.AbsoluteUri);
            strBuilder.Append("?");
            strBuilder.Append(this.GetFormEncodedQueryFormat(message));
            return(strBuilder.ToString());
        }
コード例 #6
0
        public async virtual void Write(OAuthMessage message, HttpWebRequest request)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Method      = this.GetHttpMethod(message);
            request.ContentType = this.GetHttpContentType(message);
            this.Write(message, await request.GetRequestStreamAsync());
        }
コード例 #7
0
        protected virtual OAuthMessage CreateTypedOAuthMessageFromParameters(Uri baseUri, Dictionary <string, string> parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            OAuthMessage request = null;

            if (parameters.ContainsKey("response_type") && (parameters["response_type"] == "code" || parameters["response_type"] == "token"))
            {
                request = new EndUserAuthorizationRequest(baseUri);
            }
            if ((parameters.ContainsKey("code") && !string.IsNullOrEmpty(parameters["code"])) || (parameters.ContainsKey("access_token") && !string.IsNullOrEmpty(parameters["access_token"]) && parameters.ContainsKey("refresh_token") && string.IsNullOrEmpty(parameters["refresh_token"])))
            {
                request = new EndUserAuthorizationResponse(baseUri);
            }
            if (parameters.ContainsKey("error") && !string.IsNullOrEmpty(parameters["error"]))
            {
                request = new EndUserAuthorizationFailedResponse(baseUri);
            }
            if (parameters.ContainsKey("grant_type") && !string.IsNullOrEmpty(parameters["grant_type"]) && parameters["grant_type"] == "authorization_code")
            {
                request = new AccessTokenRequestWithAuthorizationCode(baseUri);
            }
            if (parameters.ContainsKey("access_token") && !string.IsNullOrEmpty(parameters["access_token"]))
            {
                request = new AccessTokenResponse(baseUri);
            }
            if (request == null)
            {
                throw new OAuthMessageSerializationException(Resources.ID3723);
            }

            foreach (var parameter in parameters)
            {
                request.Parameters.Add(parameter.Key, parameter.Value);
            }
            request.Validate();
            return(request);
        }
コード例 #8
0
        public virtual string GetHttpContentType(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            AccessTokenRequest atRequestMessage = message as AccessTokenRequest;

            if (atRequestMessage != null)
            {
                return("application/x-www-form-urlencoded");
            }

            AccessTokenResponse atResponseMessage = message as AccessTokenResponse;

            if (atResponseMessage != null)
            {
                return("application/json");
            }
            return("text/plain; charset=us-ascii");
        }
コード例 #9
0
        public virtual string GetHttpMethod(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            AccessTokenRequest atRequestMessage = message as AccessTokenRequest;

            if (atRequestMessage != null)
            {
                return("POST");
            }

            AccessTokenResponse atResponseMessage = message as AccessTokenResponse;

            if (atResponseMessage != null)
            {
                return("POST");
            }

            return("GET");
        }
コード例 #10
0
        public virtual string GetHttpContentType(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            AccessTokenRequest atRequestMessage = message as AccessTokenRequest;
            if (atRequestMessage != null)
            {
                return "application/x-www-form-urlencoded";
            }

            AccessTokenResponse atResponseMessage = message as AccessTokenResponse;
            if (atResponseMessage != null)
            {
                return "application/json";
            }
            return "text/plain; charset=us-ascii";
        }
コード例 #11
0
        public virtual string GetHttpMethod(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            AccessTokenRequest atRequestMessage = message as AccessTokenRequest;
            if (atRequestMessage != null)
            {
                return "POST";
            }

            AccessTokenResponse atResponseMessage = message as AccessTokenResponse;
            if (atResponseMessage != null)
            {
                return "POST";
            }

            return "GET";
        }
コード例 #12
0
        public virtual string GetJsonEncodedFormat(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var serializedMessage = JsonConvert.SerializeObject(message.Parameters);

            // TODO: replace token of array to object...
            return serializedMessage;
        }
コード例 #13
0
 public virtual string GetFormEncodedQueryFormat(OAuthMessage message)
 {
     if (message == null)
     {
         throw new ArgumentNullException("message");
     }
     System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
     bool skipDelimiter = true;
     foreach (string key in message.Parameters.Keys)
     {
         if (message.Parameters[key] != null)
         {
             if (!skipDelimiter)
             {
                 strBuilder.Append("&");
             }
             strBuilder.Append(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}", new object[]
                                                                                                               {
                                                                                                                   key, 
                                                                                                                   WebUtility.UrlEncode(message.Parameters[key])
                                                                                                               }));
             skipDelimiter = false;
         }
     }
     return strBuilder.ToString();
 }
コード例 #14
0
        public virtual string GetQueryStringFormat(OAuthMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
            strBuilder.Append(message.BaseUri.AbsoluteUri);
            strBuilder.Append("?");
            strBuilder.Append(this.GetFormEncodedQueryFormat(message));
            return strBuilder.ToString();
        }
コード例 #15
0
        public virtual void Write(OAuthMessage message, System.IO.Stream requestStream)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (requestStream == null)
            {
                throw new ArgumentNullException("requestStream");
            }

            System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(requestStream);
            AccessTokenRequest atRequestMsg = message as AccessTokenRequest;
            if (atRequestMsg != null)
            {
                streamWriter.Write(this.GetFormEncodedQueryFormat(message));
                streamWriter.Flush();
                return;
            }

            AccessTokenResponse atResponseMsg = message as AccessTokenResponse;
            if (atResponseMsg != null)
            {
                streamWriter.Write(this.GetJsonEncodedFormat(message));
                streamWriter.Flush();
                return;
            }

            throw new OAuthMessageException(string.Format(Resources.ID3724, message.GetType()));
        }
コード例 #16
0
        public async virtual void Write(OAuthMessage message, HttpWebRequest request)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Method = this.GetHttpMethod(message);
            request.ContentType = this.GetHttpContentType(message);
            this.Write(message, await request.GetRequestStreamAsync());
        }