@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * Authentication adapter for Facebook OAuth2.
5 */
6final class PhutilFacebookAuthAdapter extends PhutilOAuthAuthAdapter {
7
8 public function getAdapterType() {
9 return 'facebook';
10 }
11
12 public function getAdapterDomain() {
13 return 'facebook.com';
14 }
15
16 public function getAccountID() {
17 return $this->getOAuthAccountData('id');
18 }
19
20 public function getAccountEmail() {
21 return $this->getOAuthAccountData('email');
22 }
23
24 public function getAccountName() {
25 $link = $this->getOAuthAccountData('link');
26 if (!$link) {
27 return null;
28 }
29
30 $matches = null;
31 if (!preg_match('@/([^/]+)$@', $link, $matches)) {
32 return null;
33 }
34
35 return $matches[1];
36 }
37
38 public function getAccountImageURI() {
39 $picture = $this->getOAuthAccountData('picture');
40 if ($picture) {
41 $picture_data = idx($picture, 'data');
42 if ($picture_data) {
43 return idx($picture_data, 'url');
44 }
45 }
46 return null;
47 }
48
49 public function getAccountURI() {
50 return $this->getOAuthAccountData('link');
51 }
52
53 public function getAccountRealName() {
54 return $this->getOAuthAccountData('name');
55 }
56
57 protected function getAuthenticateBaseURI() {
58 return 'https://www.facebook.com/dialog/oauth';
59 }
60
61 protected function getTokenBaseURI() {
62 return 'https://graph.facebook.com/oauth/access_token';
63 }
64
65 protected function loadOAuthAccountData() {
66 $fields = array(
67 'id',
68 'name',
69 'email',
70 'link',
71 'picture',
72 );
73
74 $uri = new PhutilURI('https://graph.facebook.com/me');
75 $uri->replaceQueryParam('access_token', $this->getAccessToken());
76 $uri->replaceQueryParam('fields', implode(',', $fields));
77 list($body) = id(new HTTPSFuture($uri))->resolvex();
78
79 $data = null;
80 try {
81 $data = phutil_json_decode($body);
82 } catch (PhutilJSONParserException $ex) {
83 throw new Exception(
84 pht('Expected valid JSON response from Facebook account data request.'),
85 0,
86 $ex);
87 }
88
89 return $data;
90 }
91
92}