diff --git a/auth.php b/auth.php index 6fdd8cd8adcebe9d9abaeed82ea44335c1cd3ecb..6aa528247aba3c73d1c38aff571c7a0300eef34b 100644 --- a/auth.php +++ b/auth.php @@ -10,6 +10,8 @@ class auth_plugin_authuffd extends DokuWiki_Auth_Plugin { parent::__construct(); $this->cando['external'] = true; + $this->api_client = null; + $this->api_user_cache = array(); } private function getOAuth2RedirectURI() @@ -136,23 +138,32 @@ class auth_plugin_authuffd extends DokuWiki_Auth_Plugin { if ($_SESSION[DOKU_COOKIE]['auth']['user'] == $user) return $_SESSION[DOKU_COOKIE]['auth']['info']; - $http = new DokuHTTPClient; - $http->keep_alive = false; - $http->user = $this->getConf('api_username'); - $http->pass = $this->getConf('api_password'); + if (!$this->api_client) + { + $this->api_client = new DokuHTTPClient; + $this->api_client->user = $this->getConf('api_username'); + $this->api_client->pass = $this->getConf('api_password'); + } + if (isset($this->api_user_cache[$user])) + return $this->api_user_cache[$user]; $params = array( 'loginname' => $user ); - $ok = $http->get($this->getConf('baseurl') . '/api/v1/getusers?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986)); - if (!$ok || $http->status != 200) - return false; - $results = json_decode($http->resp_body); - if (!$results) + $ok = $this->api_client->get($this->getConf('baseurl') . '/api/v1/getusers?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986)); + if (!$ok || $this->api_client->status != 200) return false; - return array( - 'name' => $results[0]->displayname, - 'mail' => $results[0]->email, - 'grps' => $results[0]->groups, - ); + $results = json_decode($this->api_client->resp_body); + $result = false; + if ($results) + { + $result = array( + 'name' => $results[0]->displayname, + 'mail' => $results[0]->email, + 'grps' => $results[0]->groups, + ); + + } + $this->api_user_cache[$user] = $result; + return $result; } }