Page MenuHomeIssueTracker

PhutilDexAuthAdapter.php
ActivePublic

Authored by revi on Feb 15 2024, 15:32.
Tags
None
Referenced Files
F616: PhutilDexAuthAdapter.php
Feb 15 2024, 15:32
Subscribers
None
<?php
final class PhutilDexAuthAdapter
extends PhutilOAuthAuthAdapter {
private $wellKnownConfiguration;
public function getAdapterType() {
return 'dex';
}
public function getDexBaseURI() {
return new PhutilURI('http://127.0.0.1:5556/dex');
}
public function getAdapterDomain() {
return $this->getDexBaseURI()->getDomain();
}
public function getAccountID() {
return $this->getOAuthAccountData('sub');
}
public function getAccountEmail() {
return $this->getOAuthAccountData('email');
}
public function getAccountName() {
return null;
}
public function getAccountImageURI() {
return null;
}
public function getAccountURI() {
return null;
}
public function getAccountRealName() {
return $this->getOAuthAccountData('name');
}
protected function getAuthenticateBaseURI() {
return $this->getWellKnownConfiguration('authorization_endpoint');
}
protected function getTokenBaseURI() {
return $this->getWellKnownConfiguration('token_endpoint');
}
public function getScope() {
return 'openid profile email';
}
public function getExtraAuthenticateParameters() {
return array(
'response_type' => 'code',
);
}
public function getExtraTokenParameters() {
return array(
'grant_type' => 'authorization_code',
);
}
public function getAccessToken() {
return $this->getAccessTokenData('access_token');
}
protected function loadOAuthAccountData() {
$uri = $this->getWellKnownConfiguration('userinfo_endpoint');
$future = new HTTPSFuture($uri);
$token = $this->getAccessToken();
$future->addHeader('Authorization', "Bearer {$token}");
list($body) = $future->resolvex();
try {
$result = phutil_json_decode($body);
return $result;
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Expected valid JSON response from OIDC account data request.'),
$ex);
}
}
private function getWellKnownConfiguration($key) {
if ($this->wellKnownConfiguration === null) {
$uri = $this->getDexBaseURI();
$path = $uri->getPath();
$path = rtrim($path, '/').'/.well-known/openid-configuration';
$uri->setPath($path);
$uri = phutil_string_cast($uri);
$future = new HTTPSFuture($uri);
list($body) = $future->resolvex();
$data = phutil_json_decode($body);
$this->wellKnownConfiguration = $data;
}
if (!isset($this->wellKnownConfiguration[$key])) {
throw new Exception(
pht(
'Expected key "%s" in well-known configuration!',
$key));
}
return $this->wellKnownConfiguration[$key];
}
}