Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit bdab4d9

Browse files
bchavezTratcher
authored andcommitted
Using QueryHelpers helps avoid issue #365.
1 parent 5bb5662 commit bdab4d9

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

src/Microsoft.AspNet.Authentication.Facebook/FacebookAuthenticationHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync(string code,
5151

5252
protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
5353
{
54-
var endpoint = Options.UserInformationEndpoint + "?access_token=" + UrlEncoder.UrlEncode(tokens.AccessToken);
54+
var endpoint = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "access_token", tokens.AccessToken);
5555
if (Options.SendAppSecretProof)
5656
{
57-
endpoint += "&appsecret_proof=" + GenerateAppSecretProof(tokens.AccessToken);
57+
endpoint = QueryHelpers.AddQueryString(endpoint, "appsecret_proof", GenerateAppSecretProof(tokens.AccessToken));
5858
}
5959

6060
var response = await Backchannel.GetAsync(endpoint, Context.RequestAborted);

test/Microsoft.AspNet.Authentication.Test/Facebook/FacebookMiddlewareTests.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33

44
using System;
55
using System.Net;
6+
using System.Net.Http;
7+
using System.Collections.Generic;
8+
using System.Text;
9+
using System.Linq;
610
using System.Threading.Tasks;
711
using Microsoft.AspNet.Authentication.OAuth;
812
using Microsoft.AspNet.Builder;
13+
using Microsoft.AspNet.DataProtection;
914
using Microsoft.AspNet.Http;
1015
using Microsoft.AspNet.Http.Authentication;
1116
using Microsoft.AspNet.TestHost;
1217
using Microsoft.Framework.DependencyInjection;
1318
using Microsoft.Framework.WebEncoders;
1419
using Shouldly;
20+
using Newtonsoft.Json;
1521
using Xunit;
1622

1723
namespace Microsoft.AspNet.Authentication.Facebook
@@ -168,6 +174,76 @@ public async Task ChallengeWillTriggerRedirection()
168174
location.ShouldContain("state=");
169175
}
170176

177+
[Fact]
178+
public async Task CustomUserInfoEndpointHasValidGraphQuery()
179+
{
180+
var customUserInfoEndpoint = "https://graph.facebook.com/me?fields=email,timezone,picture";
181+
string finalUserInfoEndpoint = string.Empty;
182+
var stateFormat = new PropertiesDataFormat(new EphemeralDataProtectionProvider().CreateProtector("FacebookTest"));
183+
var server = CreateServer(
184+
app =>
185+
{
186+
app.UseFacebookAuthentication();
187+
app.UseCookieAuthentication();
188+
},
189+
services =>
190+
{
191+
services.AddAuthentication();
192+
services.ConfigureFacebookAuthentication(options =>
193+
{
194+
options.AppId = "Test App Id";
195+
options.AppSecret = "Test App Secret";
196+
options.StateDataFormat = stateFormat;
197+
options.UserInformationEndpoint = customUserInfoEndpoint;
198+
options.BackchannelHttpHandler = new TestHttpMessageHandler
199+
{
200+
Sender = req =>
201+
{
202+
if (req.RequestUri.GetLeftPart(UriPartial.Path) == FacebookAuthenticationDefaults.TokenEndpoint)
203+
{
204+
var res = new HttpResponseMessage(HttpStatusCode.OK);
205+
var tokenResponse = new Dictionary<string, string>
206+
{
207+
{ "access_token", "TestAuthToken" },
208+
};
209+
res.Content = new FormUrlEncodedContent(tokenResponse);
210+
return res;
211+
}
212+
if (req.RequestUri.GetLeftPart(UriPartial.Path) ==
213+
new Uri(customUserInfoEndpoint).GetLeftPart(UriPartial.Path))
214+
{
215+
finalUserInfoEndpoint = req.RequestUri.ToString();
216+
var res = new HttpResponseMessage(HttpStatusCode.OK);
217+
var graphResponse = JsonConvert.SerializeObject(new
218+
{
219+
id = "TestProfileId",
220+
name = "TestName"
221+
});
222+
res.Content = new StringContent(graphResponse, Encoding.UTF8);
223+
return res;
224+
}
225+
return null;
226+
}
227+
};
228+
});
229+
}, handler: null);
230+
231+
var properties = new AuthenticationProperties();
232+
var correlationKey = ".AspNet.Correlation.Facebook";
233+
var correlationValue = "TestCorrelationId";
234+
properties.Items.Add(correlationKey, correlationValue);
235+
properties.RedirectUri = "/me";
236+
var state = stateFormat.Protect(properties);
237+
var transaction = await server.SendAsync(
238+
"https://example.com/signin-facebook?code=TestCode&state=" + UrlEncoder.Default.UrlEncode(state),
239+
correlationKey + "=" + correlationValue);
240+
transaction.Response.StatusCode.ShouldBe(HttpStatusCode.Redirect);
241+
transaction.Response.Headers.Location.ToString().ShouldBe("/me");
242+
finalUserInfoEndpoint.Count(c => c == '?').ShouldBe(1);
243+
finalUserInfoEndpoint.ShouldContain("fields=email,timezone,picture");
244+
finalUserInfoEndpoint.ShouldContain("&access_token=");
245+
}
246+
171247
private static TestServer CreateServer(Action<IApplicationBuilder> configure, Action<IServiceCollection> configureServices, Func<HttpContext, bool> handler)
172248
{
173249
return TestServer.Create(app =>

0 commit comments

Comments
 (0)