@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 Github OAuth2.
5 */
6final class PhutilGitHubAuthAdapter extends PhutilOAuthAuthAdapter {
7
8 public function getAdapterType() {
9 return 'github';
10 }
11
12 public function getAdapterDomain() {
13 return 'github.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 return $this->getOAuthAccountData('login');
26 }
27
28 public function getAccountImageURI() {
29 return $this->getOAuthAccountData('avatar_url');
30 }
31
32 public function getAccountURI() {
33 $name = $this->getAccountName();
34 if (phutil_nonempty_string($name)) {
35 return 'https://github.com/'.$name;
36 }
37 return null;
38 }
39
40 public function getAccountRealName() {
41 return $this->getOAuthAccountData('name');
42 }
43
44 protected function getAuthenticateBaseURI() {
45 return 'https://github.com/login/oauth/authorize';
46 }
47
48 protected function getTokenBaseURI() {
49 return 'https://github.com/login/oauth/access_token';
50 }
51
52 protected function loadOAuthAccountData() {
53 $uri = new PhutilURI('https://api.github.com/user');
54
55 $future = new HTTPSFuture($uri);
56
57 // NOTE: GitHub requires a User-Agent string.
58 $future->addHeader('User-Agent',
59 PhabricatorEnv::getEnvConfig('phabricator.base-uri'));
60
61 // See T13485. Circa early 2020, GitHub has deprecated use of the
62 // "access_token" URI parameter.
63 $token_header = sprintf('token %s', $this->getAccessToken());
64 $future->addHeader('Authorization', $token_header);
65
66 list($body) = $future->resolvex();
67
68 try {
69 return phutil_json_decode($body);
70 } catch (PhutilJSONParserException $ex) {
71 throw new Exception(
72 pht('Expected valid JSON response from GitHub account data request.'),
73 0,
74 $ex);
75 }
76 }
77
78}