Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/auth/adapter/PhutilPhabricatorAuthAdapter.php
12256 views
1
<?php
2
3
/**
4
* Authentication adapter for Phabricator OAuth2.
5
*/
6
final class PhutilPhabricatorAuthAdapter extends PhutilOAuthAuthAdapter {
7
8
private $phabricatorBaseURI;
9
private $adapterDomain;
10
11
public function setPhabricatorBaseURI($uri) {
12
$this->phabricatorBaseURI = $uri;
13
return $this;
14
}
15
16
public function getPhabricatorBaseURI() {
17
return $this->phabricatorBaseURI;
18
}
19
20
public function getAdapterDomain() {
21
return $this->adapterDomain;
22
}
23
24
public function setAdapterDomain($domain) {
25
$this->adapterDomain = $domain;
26
return $this;
27
}
28
29
public function getAdapterType() {
30
return 'phabricator';
31
}
32
33
public function getAccountID() {
34
return $this->getOAuthAccountData('phid');
35
}
36
37
public function getAccountEmail() {
38
return $this->getOAuthAccountData('primaryEmail');
39
}
40
41
public function getAccountName() {
42
return $this->getOAuthAccountData('userName');
43
}
44
45
public function getAccountImageURI() {
46
return $this->getOAuthAccountData('image');
47
}
48
49
public function getAccountURI() {
50
return $this->getOAuthAccountData('uri');
51
}
52
53
public function getAccountRealName() {
54
return $this->getOAuthAccountData('realName');
55
}
56
57
protected function getAuthenticateBaseURI() {
58
return $this->getPhabricatorURI('oauthserver/auth/');
59
}
60
61
protected function getTokenBaseURI() {
62
return $this->getPhabricatorURI('oauthserver/token/');
63
}
64
65
public function getScope() {
66
return '';
67
}
68
69
public function getExtraAuthenticateParameters() {
70
return array(
71
'response_type' => 'code',
72
);
73
}
74
75
public function getExtraTokenParameters() {
76
return array(
77
'grant_type' => 'authorization_code',
78
);
79
}
80
81
protected function loadOAuthAccountData() {
82
$uri = id(new PhutilURI($this->getPhabricatorURI('api/user.whoami')))
83
->replaceQueryParam('access_token', $this->getAccessToken());
84
list($body) = id(new HTTPSFuture($uri))->resolvex();
85
86
try {
87
$data = phutil_json_decode($body);
88
return $data['result'];
89
} catch (PhutilJSONParserException $ex) {
90
throw new Exception(
91
pht(
92
'Expected valid JSON response from "user.whoami" request.'),
93
$ex);
94
}
95
}
96
97
private function getPhabricatorURI($path) {
98
return rtrim($this->phabricatorBaseURI, '/').'/'.ltrim($path, '/');
99
}
100
101
}
102
103