diff --git a/config-templates/authsources.php b/config-templates/authsources.php index a936f64bae3c83b0e486734c70304f86227a803e..9f09ffdcbb717cce1da97a3fb3a6d6c44ed972e1 100644 --- a/config-templates/authsources.php +++ b/config-templates/authsources.php @@ -201,6 +201,10 @@ $config = array( // which additional data permissions to request from user // see http://developers.facebook.com/docs/authentication/permissions/ for the full list // 'req_perms' => 'email,user_birthday', + // Which additional user profile fields to request. + // When empty, only the app-specific user id and name will be returned + // See https://developers.facebook.com/docs/graph-api/reference/v2.6/user for the full list + // 'user_fields' => 'email,birthday,third_party_id,name,first_name,last_name', ), */ diff --git a/modules/authfacebook/lib/Auth/Source/Facebook.php b/modules/authfacebook/lib/Auth/Source/Facebook.php index 5271f313d43f10f5eba05fdcf6537a75dc9e1a65..49616522f46f3013b3a3e3c9700c1674a3e298f6 100644 --- a/modules/authfacebook/lib/Auth/Source/Facebook.php +++ b/modules/authfacebook/lib/Auth/Source/Facebook.php @@ -39,6 +39,21 @@ class sspmod_authfacebook_Auth_Source_Facebook extends SimpleSAML_Auth_Source { private $req_perms; + /** + * A comma-separated list of user profile fields to request. + * + * Note that some user fields require appropriate permissions. For + * example, to retrieve the user's primary email address, "email" must + * be specified in both the req_perms and the user_fields parameter. + * + * When empty, only the app-specific user id and name will be returned. + * + * See the Graph API specification for all available user fields: + * https://developers.facebook.com/docs/graph-api/reference/v2.6/user + */ + private $user_fields; + + /** * Constructor for this authentication source. * @@ -57,6 +72,7 @@ class sspmod_authfacebook_Auth_Source_Facebook extends SimpleSAML_Auth_Source { $this->api_key = $cfgParse->getString('api_key'); $this->secret = $cfgParse->getString('secret'); $this->req_perms = $cfgParse->getString('req_perms', NULL); + $this->user_fields = $cfgParse->getString('user_fields', NULL); } @@ -91,7 +107,7 @@ class sspmod_authfacebook_Auth_Source_Facebook extends SimpleSAML_Auth_Source { if (isset($uid) && $uid) { try { - $info = $facebook->api("/" . $uid); + $info = $facebook->api("/" . $uid . ($this->user_fields ? "?fields=" . $this->user_fields : "")); } catch (FacebookApiException $e) { throw new SimpleSAML_Error_AuthSource($this->authId, 'Error getting user profile.', $e); } @@ -108,8 +124,8 @@ class sspmod_authfacebook_Auth_Source_Facebook extends SimpleSAML_Auth_Source { } } - if (array_key_exists('username', $info)) { - $attributes['facebook_user'] = array($info['username'] . '@facebook.com'); + if (array_key_exists('third_party_id', $info)) { + $attributes['facebook_user'] = array($info['third_party_id'] . '@facebook.com'); } else { $attributes['facebook_user'] = array($uid . '@facebook.com'); }