Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Added the possibility to get the email in the Twitter implementation #118

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bin
obj
.idea
*ReSharper*
packages
*.pyc
Expand Down
2 changes: 1 addition & 1 deletion OAuth2/Client/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface IClient
/// Callback request payload (parameters).
/// <example>Request.QueryString</example>
/// </param>
UserInfo GetUserInfo(NameValueCollection parameters);
UserInfo GetUserInfo(NameValueCollection parameters, NameValueCollection queryParameters = null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use parameters for this? I'm not sure we need to introduce a new field as all the other implementations are passing state when needed already.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is the only form to pass parameters to the QueryUserInfo() method...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please confirm this as configuration is passed through to each oauth implementation. I know extra state parameters can be set on the initial request.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's only for OAuth1 clients, Twitter specific, but maybe another OAuth1 client needs this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@titarenko do you have any more info on this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you covert this to be a IDictionary<string,string>. It would be nice to not use any super specialized collections.


/// <summary>
/// Client configuration object.
Expand Down
10 changes: 5 additions & 5 deletions OAuth2/Client/Impl/FacebookClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected override Endpoint UserInfoServiceEndpoint
/// </summary>
protected override void BeforeGetUserInfo(BeforeAfterRequestArgs args)
{
args.Request.AddParameter("fields", "id,first_name,last_name,email,picture");
args.Request.AddParameter("fields", "id,first_name,last_name,email,picture.width(256).height(256)");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason the width and height is specified here. It seems we want three different image sizes. If this is due to an api change can you please attach a link to the info.

}

/// <summary>
Expand All @@ -81,7 +81,7 @@ protected override void BeforeGetUserInfo(BeforeAfterRequestArgs args)
protected override UserInfo ParseUserInfo(string content)
{
var response = JObject.Parse(content);
const string avatarUriTemplate = "{0}?type={1}";
const string avatarUriTemplate = "{0}";
var avatarUri = response["picture"]["data"]["url"].Value<string>();
return new UserInfo
{
Expand All @@ -91,9 +91,9 @@ protected override UserInfo ParseUserInfo(string content)
Email = response["email"].SafeGet(x => x.Value<string>()),
AvatarUri =
{
Small = !string.IsNullOrWhiteSpace(avatarUri) ? string.Format(avatarUriTemplate, avatarUri, "small") : string.Empty,
Normal = !string.IsNullOrWhiteSpace(avatarUri) ? string.Format(avatarUriTemplate, avatarUri, "normal") : string.Empty,
Large = !string.IsNullOrWhiteSpace(avatarUri) ? string.Format(avatarUriTemplate, avatarUri, "large") : string.Empty
Small = !string.IsNullOrWhiteSpace(avatarUri) ? string.Format(avatarUriTemplate, avatarUri) : string.Empty,
Normal = !string.IsNullOrWhiteSpace(avatarUri) ? string.Format(avatarUriTemplate, avatarUri) : string.Empty,
Large = !string.IsNullOrWhiteSpace(avatarUri) ? string.Format(avatarUriTemplate, avatarUri) : string.Empty
}
};
}
Expand Down
9 changes: 5 additions & 4 deletions OAuth2/Client/Impl/TwitterClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Newtonsoft.Json.Linq;
using OAuth2.Configuration;
using OAuth2.Infrastructure;
Expand Down Expand Up @@ -109,14 +110,14 @@ protected override UserInfo ParseUserInfo(string content)
return new UserInfo
{
Id = response["id"].Value<string>(),
Email = null,
Email = response["email"] == null ? null : response["email"].Value<string>(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to call SafeGet to get the string value here so we don't blow up.

FirstName = firstName,
LastName = lastName,
AvatarUri =
{
Small = avatarUri.Replace("normal", "mini"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this due to an api change? If so can you please attach a link to the info?

Normal = avatarUri,
Large = avatarUri.Replace("normal", "bigger")
Small = avatarUri,
Normal = avatarUri.Replace("normal", "bigger"),
Large = avatarUri.Replace("_normal", "")
}
};
}
Expand Down
23 changes: 17 additions & 6 deletions OAuth2/Client/OAuth2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class OAuth2Client : IClient
/// <summary>
/// Access token returned by provider. Can be used for further calls of provider API.
/// </summary>
public string AccessToken { get; private set; }
public string AccessToken { get; set; }

/// <summary>
/// Refresh token returned by provider. Can be used for further calls of provider API.
Expand Down Expand Up @@ -110,11 +110,19 @@ public virtual string GetLoginLinkUri(string state = null)
/// Obtains user information using OAuth2 service and data provided via callback request.
/// </summary>
/// <param name="parameters">Callback request payload (parameters).</param>
public UserInfo GetUserInfo(NameValueCollection parameters)
/// <param name="queryParameters">Callback request payload (query parameters).</param>
public UserInfo GetUserInfo(NameValueCollection parameters, NameValueCollection queryParameters = null)
{
GrantType = "authorization_code";
CheckErrorAndSetState(parameters);
QueryAccessToken(parameters);
queryParameters = queryParameters ?? new NameValueCollection();

if (this.AccessToken == null)
{
GrantType = "authorization_code";
CheckErrorAndSetState(parameters);

QueryAccessToken(parameters);
}

return GetUserInfo();
}

Expand All @@ -126,7 +134,10 @@ public string GetToken(NameValueCollection parameters)
{
GrantType = "authorization_code";
CheckErrorAndSetState(parameters);
QueryAccessToken(parameters);

if (this.AccessToken == null)
QueryAccessToken(parameters);

return AccessToken;
}

Expand Down
27 changes: 20 additions & 7 deletions OAuth2/Client/OAuthClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Specialized;
using System.Runtime.InteropServices.ComTypes;
using System.Web;
using OAuth2.Configuration;
using OAuth2.Infrastructure;
Expand Down Expand Up @@ -38,12 +39,12 @@ public abstract class OAuthClient : IClient
/// <summary>
/// Access token received from service. Can be used for further service API calls.
/// </summary>
public string AccessToken { get; private set; }
public string AccessToken { get; set; }

/// <summary>
/// Access token secret received from service. Can be used for further service API calls.
/// </summary>
public string AccessTokenSecret { get; private set; }
public string AccessTokenSecret { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="OAuthClient" /> class.
Expand Down Expand Up @@ -78,13 +79,20 @@ public string GetLoginLinkUri(string state = null)
/// </summary>
/// <param name="parameters">Callback request payload (parameters).
/// <example>Request.QueryString</example></param>
/// <param name="queryParameters">Callback request payload for query user info (parameters).
/// <example>Request.QueryString</example></param>
/// <returns></returns>
public UserInfo GetUserInfo(NameValueCollection parameters)
public UserInfo GetUserInfo(NameValueCollection parameters, NameValueCollection queryParameters = null)
{
AccessToken = parameters.GetOrThrowUnexpectedResponse(OAuthTokenKey);
QueryAccessToken(parameters.GetOrThrowUnexpectedResponse("oauth_verifier"));
queryParameters = queryParameters ?? new NameValueCollection();

if (this.AccessToken == null || this.AccessTokenSecret == null)
{
AccessToken = parameters.GetOrThrowUnexpectedResponse(OAuthTokenKey);
QueryAccessToken(parameters.GetOrThrowUnexpectedResponse("oauth_verifier"));
}

var result = ParseUserInfo(QueryUserInfo());
var result = ParseUserInfo(QueryUserInfo(queryParameters));
result.ProviderName = Name;

return result;
Expand Down Expand Up @@ -207,14 +215,19 @@ protected virtual void BeforeGetUserInfo(BeforeAfterRequestArgs args)
/// <summary>
/// Queries user info using corresponding service and data received by access token request.
/// </summary>
private string QueryUserInfo()
private string QueryUserInfo(NameValueCollection queryParameters)
{
var client = _factory.CreateClient(UserInfoServiceEndpoint);
client.Authenticator = OAuth1Authenticator.ForProtectedResource(
Configuration.ClientId, Configuration.ClientSecret, AccessToken, AccessTokenSecret);

var request = _factory.CreateRequest(UserInfoServiceEndpoint);

foreach(var parameter in queryParameters.AllKeys)
{
request.AddParameter(parameter, queryParameters[parameter]);
}

BeforeGetUserInfo(new BeforeAfterRequestArgs
{
Client = client,
Expand Down