From f1c917eb789bf625fadad53fb74150438090420c Mon Sep 17 00:00:00 2001
From: Julian <julian@cccv.de>
Date: Sat, 19 Feb 2022 05:27:49 +0100
Subject: [PATCH] In-request caching of getUserData requests

---
 auth.php | 39 +++++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 14 deletions(-)

diff --git a/auth.php b/auth.php
index 6fdd8cd..6aa5282 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;
 	}
 }
-- 
GitLab