Remove username from Facebook

Add default fields for Facebook

Remove default fields except name and email

Add all the core fields for Facebook

Fix location and field uniqueness
This commit is contained in:
Mike Surcouf 2016-01-26 13:38:50 +00:00 committed by Chris R
parent 80dc5759cc
commit e737f3207e
4 changed files with 202 additions and 24 deletions

View File

@ -48,10 +48,22 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, identifier, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var userName = FacebookHelper.GetUserName(payload);
if (!string.IsNullOrEmpty(userName))
var ageRangeMin = FacebookHelper.GetAgeRangeMin(payload);
if (!string.IsNullOrEmpty(ageRangeMin))
{
identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, userName, ClaimValueTypes.String, Options.ClaimsIssuer));
identity.AddClaim(new Claim("urn:facebook:age_range_min", ageRangeMin, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var ageRangeMax = FacebookHelper.GetAgeRangeMax(payload);
if (!string.IsNullOrEmpty(ageRangeMax))
{
identity.AddClaim(new Claim("urn:facebook:age_range_max", ageRangeMax, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var birthday = FacebookHelper.GetBirthday(payload);
if (!string.IsNullOrEmpty(birthday))
{
identity.AddClaim(new Claim(ClaimTypes.DateOfBirth, birthday, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var email = FacebookHelper.GetEmail(payload);
@ -60,16 +72,22 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
identity.AddClaim(new Claim(ClaimTypes.Email, email, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var name = FacebookHelper.GetName(payload);
if (!string.IsNullOrEmpty(name))
var firstName = FacebookHelper.GetFirstName(payload);
if (!string.IsNullOrEmpty(firstName))
{
identity.AddClaim(new Claim("urn:facebook:name", name, ClaimValueTypes.String, Options.ClaimsIssuer));
identity.AddClaim(new Claim(ClaimTypes.GivenName, firstName, ClaimValueTypes.String, Options.ClaimsIssuer));
}
// Many Facebook accounts do not set the UserName field. Fall back to the Name field instead.
if (string.IsNullOrEmpty(userName))
{
identity.AddClaim(new Claim(identity.NameClaimType, name, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var gender = FacebookHelper.GetGender(payload);
if (!string.IsNullOrEmpty(gender))
{
identity.AddClaim(new Claim(ClaimTypes.Gender, gender, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var lastName = FacebookHelper.GetLastName(payload);
if (!string.IsNullOrEmpty(lastName))
{
identity.AddClaim(new Claim(ClaimTypes.Surname, lastName, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var link = FacebookHelper.GetLink(payload);
@ -78,6 +96,36 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
identity.AddClaim(new Claim("urn:facebook:link", link, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var location = FacebookHelper.GetLocation(payload);
if (!string.IsNullOrEmpty(location))
{
identity.AddClaim(new Claim("urn:facebook:location", location, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var locale = FacebookHelper.GetLocale(payload);
if (!string.IsNullOrEmpty(locale))
{
identity.AddClaim(new Claim(ClaimTypes.Locality, locale, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var middleName = FacebookHelper.GetMiddleName(payload);
if (!string.IsNullOrEmpty(middleName))
{
identity.AddClaim(new Claim("urn:facebook:middle_name", middleName, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var name = FacebookHelper.GetName(payload);
if (!string.IsNullOrEmpty(name))
{
identity.AddClaim(new Claim(ClaimTypes.Name, name, ClaimValueTypes.String, Options.ClaimsIssuer));
}
var timeZone = FacebookHelper.GetTimeZone(payload);
if (!string.IsNullOrEmpty(timeZone))
{
identity.AddClaim(new Claim("urn:facebook:timezone", timeZone, ClaimValueTypes.String, Options.ClaimsIssuer));
}
await Options.Events.CreatingTicket(context);
return context.Ticket;

View File

@ -26,16 +26,91 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
}
/// <summary>
/// Gets the user's name.
/// Gets the user's min age.
/// </summary>
public static string GetName(JObject user)
public static string GetAgeRangeMin(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return TryGetValue(user, "age_range", "min");
}
/// <summary>
/// Gets the user's max age.
/// </summary>
public static string GetAgeRangeMax(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("name");
return TryGetValue(user, "age_range", "max");
}
/// <summary>
/// Gets the user's birthday.
/// </summary>
public static string GetBirthday(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("birthday");
}
/// <summary>
/// Gets the Facebook email.
/// </summary>
public static string GetEmail(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("email");
}
/// <summary>
/// Gets the user's first name.
/// </summary>
public static string GetFirstName(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("first_name");
}
/// <summary>
/// Gets the user's gender.
/// </summary>
public static string GetGender(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("gender");
}
/// <summary>
/// Gets the user's family name.
/// </summary>
public static string GetLastName(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("last_name");
}
/// <summary>
@ -51,30 +126,81 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
}
/// <summary>
/// Gets the Facebook username.
/// Gets the user's location.
/// </summary>
public static string GetUserName(JObject user)
public static string GetLocation(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("username");
return TryGetValue(user, "location", "name");
}
/// <summary>
/// Gets the Facebook email.
/// Gets the user's locale.
/// </summary>
public static string GetEmail(JObject user)
public static string GetLocale(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("locale");
}
/// <summary>
/// Gets the user's middle name.
/// </summary>
public static string GetMiddleName(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("email");
return user.Value<string>("middle_name");
}
/// <summary>
/// Gets the user's name.
/// </summary>
public static string GetName(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("name");
}
/// <summary>
/// Gets the user's timezone.
/// </summary>
public static string GetTimeZone(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value<string>("timezone");
}
// Get the given subProperty from a property.
private static string TryGetValue(JObject user, string propertyName, string subProperty)
{
JToken value;
if (user.TryGetValue(propertyName, out value))
{
var subObject = JObject.Parse(value.ToString());
if (subObject != null && subObject.TryGetValue(subProperty, out value))
{
return value.ToString();
}
}
return null;
}
}
}

View File

@ -71,6 +71,7 @@ namespace Microsoft.AspNetCore.Authentication.Facebook
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Exception_OptionMustBeProvided, nameof(Options.AppId)));
}
if (string.IsNullOrEmpty(Options.AppSecret))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Exception_OptionMustBeProvided, nameof(Options.AppSecret)));

View File

@ -24,7 +24,10 @@ namespace Microsoft.AspNetCore.Builder
AuthorizationEndpoint = FacebookDefaults.AuthorizationEndpoint;
TokenEndpoint = FacebookDefaults.TokenEndpoint;
UserInformationEndpoint = FacebookDefaults.UserInformationEndpoint;
Fields = new List<string>();
Scope.Add("public_profile");
Scope.Add("email");
Fields.Add("name");
Fields.Add("email");
}
// Facebook uses a non-standard term for this field.
@ -57,6 +60,6 @@ namespace Microsoft.AspNetCore.Builder
/// The list of fields to retrieve from the UserInformationEndpoint.
/// https://developers.facebook.com/docs/graph-api/reference/user
/// </summary>
public IList<string> Fields { get; }
public ICollection<string> Fields { get; } = new HashSet<string>();
}
}