Skip to content

Commit dfe8377

Browse files
Jason Dryhurst-SmithJason Dryhurst-Smith
authored andcommitted
expose any additional params passed out of the oauth responses
review comments and do not throw if oauth values are not present in response tabs not spaces
1 parent e44f5d1 commit dfe8377

File tree

5 files changed

+45
-44
lines changed

5 files changed

+45
-44
lines changed

TinyOAuth1/AccessTokenInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
using System.Collections.Generic;
2+
13
namespace TinyOAuth1
24
{
35
public class AccessTokenInfo
46
{
57
public string AccessToken { get; set; }
68
public string AccessTokenSecret { get; set; }
9+
public IDictionary<string, string> AdditionalParams { get; set; }
710
}
811
}

TinyOAuth1/RequestTokenInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
namespace TinyOAuth1
1+
using System.Collections.Generic;
2+
3+
namespace TinyOAuth1
24
{
35
public class RequestTokenInfo
46
{
57
public string RequestToken { get; set; }
68
public string RequestTokenSecret { get; set; }
9+
public IDictionary<string, string> AdditionalParams { get; set; }
710
}
811
}

TinyOAuth1/TinyOAuth.cs

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Net.Http;
45
using System.Net.Http.Headers;
56
using System.Security.Cryptography;
67
using System.Text;
78
using System.Threading.Tasks;
9+
using System.Web;
10+
using TinyOAuth1.Utility;
811

912
namespace TinyOAuth1
1013
{
@@ -95,7 +98,7 @@ As the HTTP POST request body with a content-type of application/x-www-form-urle
9598
Added to the URLs in the query part (as defined by [RFC3986] section 3).
9699
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.
97100
98-
*/
101+
*/
99102

100103
/* 5.4.1. Authorization Header
101104
@@ -267,30 +270,19 @@ No additional Service Provider specific parameters are allowed when requesting a
267270

268271
if (!string.IsNullOrEmpty(responseText))
269272
{
270-
string oauthToken = null;
271-
string oauthTokenSecret = null;
272-
var keyValPairs = responseText.Split('&');
273+
var keyValPairs = HttpUtility.ParseQueryString(responseText);
273274

274-
for (var i = 0; i < keyValPairs.Length; i++)
275-
{
276-
var splits = keyValPairs[i].Split('=');
277-
switch (splits[0])
278-
{
279-
case "oauth_token":
280-
oauthToken = splits[1];
281-
break;
282-
case "oauth_token_secret":
283-
oauthTokenSecret = splits[1];
284-
break;
285-
}
286-
}
275+
var oauthToken = keyValPairs.GetValues("oauth_token")?.SingleOrDefault();
276+
var oauthTokenSecret = keyValPairs.GetValues("oauth_token_secret")?.SingleOrDefault();
287277

288278
return new AccessTokenInfo
289279
{
290280
AccessToken = oauthToken,
291-
AccessTokenSecret = oauthTokenSecret
281+
AccessTokenSecret = oauthTokenSecret,
282+
AdditionalParams = keyValPairs.ToDictionary()
292283
};
293284
}
285+
294286
throw new Exception("Empty response text when getting the access token");
295287
}
296288

@@ -342,36 +334,21 @@ public async Task<RequestTokenInfo> GetRequestTokenAsync()
342334
// oauth_token_secret:
343335
//The Token Secret.
344336

345-
string oauthToken = null;
346-
string oauthTokenSecret = null;
347337
//string oauthAuthorizeUrl = null;
348338

349-
var keyValPairs = responseText.Split('&');
339+
var keyValPairs = HttpUtility.ParseQueryString(responseText);
350340

351-
for (var i = 0; i < keyValPairs.Length; i++)
352-
{
353-
var splits = keyValPairs[i].Split('=');
354-
switch (splits[0])
355-
{
356-
case "oauth_token":
357-
oauthToken = splits[1];
358-
break;
359-
case "oauth_token_secret":
360-
oauthTokenSecret = splits[1];
361-
break;
362-
// TODO: Handle this one?
363-
//case "xoauth_request_auth_url":
364-
// oauthAuthorizeUrl = splits[1];
365-
// break;
366-
}
367-
}
341+
var oauthToken = keyValPairs.GetValues("oauth_token")?.SingleOrDefault();
342+
var oauthTokenSecret = keyValPairs.GetValues("oauth_token_secret")?.SingleOrDefault();
368343

369344
return new RequestTokenInfo
370345
{
371346
RequestToken = oauthToken,
372-
RequestTokenSecret = oauthTokenSecret
347+
RequestTokenSecret = oauthTokenSecret,
348+
AdditionalParams = keyValPairs.ToDictionary()
373349
};
374350
}
351+
375352
throw new Exception("Empty response text when getting the request token");
376353
}
377354

TinyOAuth1/TinyOAuth1.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
@@ -12,13 +12,13 @@
1212
<Copyright>Copyright 2017 © Johan Otterud. All rights reserved.</Copyright>
1313
<PackageTags>oauth 1.0a oauth1 authorization 1.0 authentication</PackageTags>
1414
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
15-
<Version>1.1.0</Version>
15+
<Version>1.2.0</Version>
1616
<Company />
1717
<PackageLicenseUrl>https://raw.githubusercontent.com/johot/TinyOAuth1/master/LICENSE.md</PackageLicenseUrl>
1818
<PackageProjectUrl>https://github.com/johot/TinyOAuth1</PackageProjectUrl>
1919
<RepositoryUrl></RepositoryUrl>
20-
<AssemblyVersion>1.1.0.0</AssemblyVersion>
21-
<FileVersion>1.1.0.0</FileVersion>
20+
<AssemblyVersion>1.2.0.0</AssemblyVersion>
21+
<FileVersion>1.2.0.0</FileVersion>
2222

2323
</PropertyGroup>
2424

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
using System.Collections.Specialized;
3+
4+
namespace TinyOAuth1.Utility
5+
{
6+
internal static class NameValueCollectionExtensions
7+
{
8+
internal static IDictionary<string, string> ToDictionary(this NameValueCollection col)
9+
{
10+
var dict = new Dictionary<string, string>();
11+
foreach (var k in col.AllKeys)
12+
{
13+
dict.Add(k, col[k]);
14+
}
15+
return dict;
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)