Parcourir la source

added komga sso (FR#24)

CauseFX il y a 4 ans
Parent
commit
80c9fa8c57

+ 7 - 1
api/classes/organizr.class.php

@@ -1502,6 +1502,7 @@ class Organizr
 			'{email}' => $this->user['email'],
 			'{group}' => $this->user['group'],
 			'{group_id}' => $this->user['groupID'],
+			'{komga}' => $_COOKIE['komga_token'] ?? ''
 		];
 		if (empty($tabs)) {
 			return $tabs;
@@ -3082,7 +3083,11 @@ class Organizr
 					'label' => 'Enable',
 					'value' => $this->config['ssoJellyfin']
 				]
-			]
+			],
+			'Komga' => [
+				$this->settingsOption('url', 'komgaURL'),
+				$this->settingsOption('enable', 'ssoKomga'),
+			],
 		];
 	}
 	
@@ -3914,6 +3919,7 @@ class Organizr
 		$this->clearTautulliTokens();
 		$this->clearJellyfinTokens();
 		$this->revokeTokenCurrentUser($this->user['token']);
+		$this->clearKomgaToken();
 		$this->user = null;
 		return true;
 	}

+ 2 - 0
api/config/default.php

@@ -77,6 +77,8 @@ return [
 	'ssoJellyfin' => false,
 	'ssoOverseerr' => false,
 	'ssoPetio' => false,
+	'ssoKomga' => false,
+	'komgaURL' => '',
 	'sonarrURL' => '',
 	'sonarrUnmonitored' => false,
 	'sonarrToken' => '',

+ 19 - 0
api/functions/organizr-functions.php

@@ -709,6 +709,25 @@ trait OrganizrFunctions
 		$this->coookie('delete', 'jellyfin_credentials');
 	}
 	
+	public function clearKomgaToken()
+	{
+		if (isset($_COOKIE['komga_token'])) {
+			try {
+				$url = $this->qualifyURL($this->config['komgaURL']);
+				$options = $this->requestOptions($url, 60000, true, false);
+				$response = Requests::post($url . '/api/v1/users/logout', ['X-Auth-Token' => $_COOKIE['komga_token']], $options);
+				if ($response->success) {
+					$this->writeLog('success', 'Komga Token Function - Logged User out', 'SYSTEM');
+				} else {
+					$this->writeLog('error', 'Komga Token Function - Unable to Logged User out', 'SYSTEM');
+				}
+			} catch (Requests_Exception $e) {
+				$this->writeLog('error', 'Komga Token Function - Error: ' . $e->getMessage(), 'SYSTEM');
+			}
+			$this->coookie('delete', 'komga_token');
+		}
+	}
+	
 	public function analyzeIP($ip)
 	{
 		if (strpos($ip, '/') !== false) {

+ 32 - 1
api/functions/sso-functions.php

@@ -8,6 +8,7 @@ trait SSOFunctions
 			'myPlexAccessToken' => $_COOKIE['mpt'] ?? false,
 			'id_token' => $_COOKIE['Auth'] ?? false,
 			'jellyfin_credentials' => $_COOKIE['jellyfin_credentials'] ?? false,
+			'komga_token' => $_COOKIE['komga_token'] ?? false
 		);
 		// Jellyfin cookie
 		foreach (array_keys($_COOKIE) as $k => $v) {
@@ -28,7 +29,8 @@ trait SSOFunctions
 			'ombi' => 'username',
 			'overseerr' => 'email',
 			'tautulli' => 'username',
-			'petio' => 'username'
+			'petio' => 'username',
+			'komga' => 'email'
 		);
 		return (gettype($userobj) == 'string') ? $userobj : $userobj[$map[$app]];
 	}
@@ -75,9 +77,38 @@ trait SSOFunctions
 				$this->coookie('set', 'petio_jwt', $petioToken, $this->config['rememberMeDays'], false);
 			}
 		}
+		if ($this->config['ssoKomga']) {
+			$komga = $this->getKomgaToken($this->getSSOUserFor('komga', $userobj), $password);
+			if ($komga) {
+				$this->coookie('set', 'komga_token', $komga, $this->config['rememberMeDays'], false);
+			}
+		}
 		return true;
 	}
 	
+	public function getKomgaToken($email, $password)
+	{
+		try {
+			$credentials = array('auth' => new Requests_Auth_Digest(array($email, $password)));
+			$url = $this->qualifyURL($this->config['komgaURL']);
+			$options = $this->requestOptions($url, 60000, true, false, $credentials);
+			$response = Requests::get($url . '/api/v1/users/me', ['X-Auth-Token' => 'organizrSSO'], $options);
+			if ($response->success) {
+				if ($response->headers['x-auth-token']) {
+					$this->writeLog('success', 'Komga Token Function - Grabbed token.', $email);
+					return $response->headers['x-auth-token'];
+				} else {
+					$this->writeLog('error', 'Komga Token Function - Komga did not return Token', $email);
+				}
+			} else {
+				$this->writeLog('error', 'Komga Token Function - Komga did not return Token', $email);
+			}
+		} catch (Requests_Exception $e) {
+			$this->writeLog('error', 'Komga Token Function - Error: ' . $e->getMessage(), $email);
+		}
+		return false;
+	}
+	
 	public function getJellyfinToken($username, $password)
 	{
 		$token = null;