diff --git a/TinyOAuth1/AccessTokenInfo.cs b/TinyOAuth1/AccessTokenInfo.cs index 2a62f36..d513aff 100644 --- a/TinyOAuth1/AccessTokenInfo.cs +++ b/TinyOAuth1/AccessTokenInfo.cs @@ -1,8 +1,11 @@ +using System.Collections.Generic; + namespace TinyOAuth1 { public class AccessTokenInfo { public string AccessToken { get; set; } public string AccessTokenSecret { get; set; } + public IDictionary AdditionalParams { get; set; } } } \ No newline at end of file diff --git a/TinyOAuth1/RequestTokenInfo.cs b/TinyOAuth1/RequestTokenInfo.cs index acdcc24..851961a 100644 --- a/TinyOAuth1/RequestTokenInfo.cs +++ b/TinyOAuth1/RequestTokenInfo.cs @@ -1,8 +1,11 @@ -namespace TinyOAuth1 +using System.Collections.Generic; + +namespace TinyOAuth1 { public class RequestTokenInfo { public string RequestToken { get; set; } public string RequestTokenSecret { get; set; } + public IDictionary AdditionalParams { get; set; } } } \ No newline at end of file diff --git a/TinyOAuth1/TinyOAuth.cs b/TinyOAuth1/TinyOAuth.cs index 5df270b..17d1ed4 100644 --- a/TinyOAuth1/TinyOAuth.cs +++ b/TinyOAuth1/TinyOAuth.cs @@ -1,10 +1,13 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; +using System.Web; +using TinyOAuth1.Utility; namespace TinyOAuth1 { @@ -95,7 +98,7 @@ As the HTTP POST request body with a content-type of application/x-www-form-urle Added to the URLs in the query part (as defined by [RFC3986] section 3). In addition to these defined methods, future extensions may describe alternate methods for sending the OAuth Protocol Parameters. The methods for sending other request parameters are left undefined, but SHOULD NOT use the OAuth HTTP Authorization Scheme header. - */ + */ /* 5.4.1. Authorization Header @@ -267,30 +270,19 @@ No additional Service Provider specific parameters are allowed when requesting a if (!string.IsNullOrEmpty(responseText)) { - string oauthToken = null; - string oauthTokenSecret = null; - var keyValPairs = responseText.Split('&'); + var keyValPairs = HttpUtility.ParseQueryString(responseText); - for (var i = 0; i < keyValPairs.Length; i++) - { - var splits = keyValPairs[i].Split('='); - switch (splits[0]) - { - case "oauth_token": - oauthToken = splits[1]; - break; - case "oauth_token_secret": - oauthTokenSecret = splits[1]; - break; - } - } + var oauthToken = keyValPairs.GetValues("oauth_token")?.SingleOrDefault(); + var oauthTokenSecret = keyValPairs.GetValues("oauth_token_secret")?.SingleOrDefault(); return new AccessTokenInfo { AccessToken = oauthToken, - AccessTokenSecret = oauthTokenSecret + AccessTokenSecret = oauthTokenSecret, + AdditionalParams = keyValPairs.ToDictionary() }; } + throw new Exception("Empty response text when getting the access token"); } @@ -342,36 +334,21 @@ public async Task GetRequestTokenAsync() // oauth_token_secret: //The Token Secret. - string oauthToken = null; - string oauthTokenSecret = null; //string oauthAuthorizeUrl = null; - var keyValPairs = responseText.Split('&'); + var keyValPairs = HttpUtility.ParseQueryString(responseText); - for (var i = 0; i < keyValPairs.Length; i++) - { - var splits = keyValPairs[i].Split('='); - switch (splits[0]) - { - case "oauth_token": - oauthToken = splits[1]; - break; - case "oauth_token_secret": - oauthTokenSecret = splits[1]; - break; - // TODO: Handle this one? - //case "xoauth_request_auth_url": - // oauthAuthorizeUrl = splits[1]; - // break; - } - } + var oauthToken = keyValPairs.GetValues("oauth_token")?.SingleOrDefault(); + var oauthTokenSecret = keyValPairs.GetValues("oauth_token_secret")?.SingleOrDefault(); return new RequestTokenInfo { RequestToken = oauthToken, - RequestTokenSecret = oauthTokenSecret + RequestTokenSecret = oauthTokenSecret, + AdditionalParams = keyValPairs.ToDictionary() }; } + throw new Exception("Empty response text when getting the request token"); } diff --git a/TinyOAuth1/TinyOAuth1.csproj b/TinyOAuth1/TinyOAuth1.csproj index 559292f..964c387 100644 --- a/TinyOAuth1/TinyOAuth1.csproj +++ b/TinyOAuth1/TinyOAuth1.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 @@ -12,13 +12,13 @@ Copyright 2017 © Johan Otterud. All rights reserved. oauth 1.0a oauth1 authorization 1.0 authentication true - 1.1.0 + 1.2.0 https://raw.githubusercontent.com/johot/TinyOAuth1/master/LICENSE.md https://github.com/johot/TinyOAuth1 - 1.1.0.0 - 1.1.0.0 + 1.2.0.0 + 1.2.0.0 diff --git a/TinyOAuth1/Utility/NameValueCollectionExtensions.cs b/TinyOAuth1/Utility/NameValueCollectionExtensions.cs new file mode 100644 index 0000000..b554c74 --- /dev/null +++ b/TinyOAuth1/Utility/NameValueCollectionExtensions.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using System.Collections.Specialized; + +namespace TinyOAuth1.Utility +{ + internal static class NameValueCollectionExtensions + { + internal static IDictionary ToDictionary(this NameValueCollection col) + { + var dict = new Dictionary(); + foreach (var k in col.AllKeys) + { + dict.Add(k, col[k]); + } + return dict; + } + } +} \ No newline at end of file