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

Commit a18181d

Browse files
committed
#565 Update facebook APIs to v2.5.
1 parent 17e9a33 commit a18181d

File tree

5 files changed

+48
-42
lines changed

5 files changed

+48
-42
lines changed

samples/SocialSample/Startup.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
using System;
12
using System.Linq;
23
using System.Net.Http;
34
using System.Net.Http.Headers;
45
using System.Security.Claims;
56
using System.Text.Encodings.Web;
67
using System.Threading.Tasks;
78
using Microsoft.AspNet.Authentication.Cookies;
9+
using Microsoft.AspNet.Authentication.Facebook;
810
using Microsoft.AspNet.Authentication.Google;
911
using Microsoft.AspNet.Authentication.MicrosoftAccount;
1012
using Microsoft.AspNet.Authentication.OAuth;
@@ -30,6 +32,24 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
3032
{
3133
loggerfactory.AddConsole(LogLevel.Information);
3234

35+
// Simple error page to avoid a repo dependency.
36+
app.Use(async (context, next) =>
37+
{
38+
try
39+
{
40+
await next();
41+
}
42+
catch (Exception ex)
43+
{
44+
if (context.Response.HasStarted)
45+
{
46+
throw;
47+
}
48+
context.Response.StatusCode = 500;
49+
await context.Response.WriteAsync(ex.ToString());
50+
}
51+
});
52+
3353
app.UseCookieAuthentication(options =>
3454
{
3555
options.AutomaticAuthenticate = true;
@@ -38,10 +58,12 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
3858
});
3959

4060
// https://developers.facebook.com/apps/
41-
app.UseFacebookAuthentication(options =>
61+
app.UseFacebookAuthentication(new FacebookOptions()
4262
{
43-
options.AppId = "569522623154478";
44-
options.AppSecret = "a124463c4719c94b4228d9a240e5dc1a";
63+
AppId = "569522623154478",
64+
AppSecret = "a124463c4719c94b4228d9a240e5dc1a",
65+
Scope = { "email" },
66+
Fields = { "name", "email" },
4567
});
4668

4769
app.UseOAuthAuthentication(new OAuthOptions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public static class FacebookDefaults
77
{
88
public const string AuthenticationScheme = "Facebook";
99

10-
public static readonly string AuthorizationEndpoint = "https://www.facebook.com/v2.2/dialog/oauth";
10+
public static readonly string AuthorizationEndpoint = "https://www.facebook.com/v2.5/dialog/oauth";
1111

12-
public static readonly string TokenEndpoint = "https://graph.facebook.com/v2.2/oauth/access_token";
12+
public static readonly string TokenEndpoint = "https://graph.facebook.com/v2.5/oauth/access_token";
1313

14-
public static readonly string UserInformationEndpoint = "https://graph.facebook.com/v2.2/me";
14+
public static readonly string UserInformationEndpoint = "https://graph.facebook.com/v2.5/me";
1515
}
1616
}

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

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Globalization;
65
using System.Net.Http;
76
using System.Security.Claims;
@@ -10,8 +9,6 @@
109
using System.Threading.Tasks;
1110
using Microsoft.AspNet.Authentication.OAuth;
1211
using Microsoft.AspNet.Http.Authentication;
13-
using Microsoft.AspNet.Http.Extensions;
14-
using Microsoft.AspNet.Http.Internal;
1512
using Microsoft.AspNet.WebUtilities;
1613
using Newtonsoft.Json.Linq;
1714

@@ -24,38 +21,17 @@ public FacebookHandler(HttpClient httpClient)
2421
{
2522
}
2623

27-
protected override async Task<OAuthTokenResponse> ExchangeCodeAsync(string code, string redirectUri)
28-
{
29-
var queryBuilder = new QueryBuilder()
30-
{
31-
{ "grant_type", "authorization_code" },
32-
{ "code", code },
33-
{ "redirect_uri", redirectUri },
34-
{ "client_id", Options.AppId },
35-
{ "client_secret", Options.AppSecret },
36-
};
37-
38-
var response = await Backchannel.GetAsync(Options.TokenEndpoint + queryBuilder.ToString(), Context.RequestAborted);
39-
response.EnsureSuccessStatusCode();
40-
41-
var form = new FormCollection(FormReader.ReadForm(await response.Content.ReadAsStringAsync()));
42-
var payload = new JObject();
43-
foreach (string key in form.Keys)
44-
{
45-
payload.Add(string.Equals(key, "expires", StringComparison.OrdinalIgnoreCase) ? "expires_in" : key, (string)form[key]);
46-
}
47-
48-
// The refresh token is not available.
49-
return OAuthTokenResponse.Success(payload);
50-
}
51-
5224
protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
5325
{
5426
var endpoint = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "access_token", tokens.AccessToken);
5527
if (Options.SendAppSecretProof)
5628
{
5729
endpoint = QueryHelpers.AddQueryString(endpoint, "appsecret_proof", GenerateAppSecretProof(tokens.AccessToken));
5830
}
31+
if (Options.Fields.Count > 0)
32+
{
33+
endpoint = QueryHelpers.AddQueryString(endpoint, "fields", string.Join(",", Options.Fields));
34+
}
5935

6036
var response = await Backchannel.GetAsync(endpoint, Context.RequestAborted);
6137
response.EnsureSuccessStatusCode();

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using Microsoft.AspNet.Http;
4+
using System.Collections.Generic;
55
using Microsoft.AspNet.Authentication.OAuth;
6+
using Microsoft.AspNet.Http;
67

78
namespace Microsoft.AspNet.Authentication.Facebook
89
{
@@ -24,6 +25,7 @@ public FacebookOptions()
2425
TokenEndpoint = FacebookDefaults.TokenEndpoint;
2526
UserInformationEndpoint = FacebookDefaults.UserInformationEndpoint;
2627
SaveTokensAsClaims = false;
28+
Fields = new List<string>();
2729
}
2830

2931
// Facebook uses a non-standard term for this field.
@@ -51,5 +53,11 @@ public string AppSecret
5153
/// This is enabled by default.
5254
/// </summary>
5355
public bool SendAppSecretProof { get; set; }
56+
57+
/// <summary>
58+
/// The list of fields to retrieve from the UserInformationEndpoint.
59+
/// https://developers.facebook.com/docs/graph-api/reference/user
60+
/// </summary>
61+
public IList<string> Fields { get; }
5462
}
5563
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public async Task NestedMapWillNotAffectRedirect()
8686
var transaction = await server.SendAsync("http://example.com/base/login");
8787
Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
8888
var location = transaction.Response.Headers.Location.AbsoluteUri;
89-
Assert.Contains("https://www.facebook.com/v2.2/dialog/oauth", location);
89+
Assert.Contains("https://www.facebook.com/v2.5/dialog/oauth", location);
9090
Assert.Contains("response_type=code", location);
9191
Assert.Contains("client_id=", location);
9292
Assert.Contains("redirect_uri=" + UrlEncoder.Default.Encode("http://example.com/base/signin-facebook"), location);
@@ -113,7 +113,7 @@ public async Task MapWillNotAffectRedirect()
113113
var transaction = await server.SendAsync("http://example.com/login");
114114
Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
115115
var location = transaction.Response.Headers.Location.AbsoluteUri;
116-
Assert.Contains("https://www.facebook.com/v2.2/dialog/oauth", location);
116+
Assert.Contains("https://www.facebook.com/v2.5/dialog/oauth", location);
117117
Assert.Contains("response_type=code", location);
118118
Assert.Contains("client_id=", location);
119119
Assert.Contains("redirect_uri="+ UrlEncoder.Default.Encode("http://example.com/signin-facebook"), location);
@@ -147,7 +147,7 @@ public async Task ChallengeWillTriggerRedirection()
147147
var transaction = await server.SendAsync("http://example.com/challenge");
148148
Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
149149
var location = transaction.Response.Headers.Location.AbsoluteUri;
150-
Assert.Contains("https://www.facebook.com/v2.2/dialog/oauth", location);
150+
Assert.Contains("https://www.facebook.com/v2.5/dialog/oauth", location);
151151
Assert.Contains("response_type=code", location);
152152
Assert.Contains("client_id=", location);
153153
Assert.Contains("redirect_uri=", location);
@@ -178,11 +178,11 @@ public async Task CustomUserInfoEndpointHasValidGraphQuery()
178178
if (req.RequestUri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped) == FacebookDefaults.TokenEndpoint)
179179
{
180180
var res = new HttpResponseMessage(HttpStatusCode.OK);
181-
var tokenResponse = new Dictionary<string, string>
181+
var graphResponse = JsonConvert.SerializeObject(new
182182
{
183-
{ "access_token", "TestAuthToken" },
184-
};
185-
res.Content = new FormUrlEncodedContent(tokenResponse);
183+
access_token = "TestAuthToken"
184+
});
185+
res.Content = new StringContent(graphResponse, Encoding.UTF8);
186186
return res;
187187
}
188188
if (req.RequestUri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped) ==

0 commit comments

Comments
 (0)