|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.Net;
|
| 6 | +using System.Net.Http; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Text; |
| 9 | +using System.Linq; |
6 | 10 | using System.Threading.Tasks;
|
7 | 11 | using Microsoft.AspNet.Authentication.OAuth;
|
8 | 12 | using Microsoft.AspNet.Builder;
|
| 13 | +using Microsoft.AspNet.DataProtection; |
9 | 14 | using Microsoft.AspNet.Http;
|
10 | 15 | using Microsoft.AspNet.Http.Authentication;
|
11 | 16 | using Microsoft.AspNet.TestHost;
|
12 | 17 | using Microsoft.Framework.DependencyInjection;
|
13 | 18 | using Microsoft.Framework.WebEncoders;
|
14 | 19 | using Shouldly;
|
| 20 | +using Newtonsoft.Json; |
15 | 21 | using Xunit;
|
16 | 22 |
|
17 | 23 | namespace Microsoft.AspNet.Authentication.Facebook
|
@@ -168,6 +174,76 @@ public async Task ChallengeWillTriggerRedirection()
|
168 | 174 | location.ShouldContain("state=");
|
169 | 175 | }
|
170 | 176 |
|
| 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 | + |
171 | 247 | private static TestServer CreateServer(Action<IApplicationBuilder> configure, Action<IServiceCollection> configureServices, Func<HttpContext, bool> handler)
|
172 | 248 | {
|
173 | 249 | return TestServer.Create(app =>
|
|
0 commit comments