Browse Source

Petio SSO

HalianElf 5 years ago
parent
commit
64e4c8d0ba
4 changed files with 92 additions and 1 deletions
  1. 36 0
      api/classes/organizr.class.php
  2. 5 0
      api/config/default.php
  3. 50 1
      api/functions/sso-functions.php
  4. 1 0
      organizr

+ 36 - 0
api/classes/organizr.class.php

@@ -2390,6 +2390,41 @@ class Organizr
 					'value' => $this->config['ssoOverseerr']
 				)
 			),
+			'Petio' => array(
+				array(
+					'type' => 'input',
+					'name' => 'petioURL',
+					'label' => 'Petio URL',
+					'value' => $this->config['petioURL'],
+					'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
+					'placeholder' => 'http(s)://hostname:port'
+				),
+				array(
+					'type' => 'password-alt',
+					'name' => 'petioToken',
+					'label' => 'Token',
+					'value' => $this->config['petioToken']
+				),
+				array(
+					'type' => 'input',
+					'name' => 'petioFallbackUser',
+					'label' => 'Petio Fallback User',
+					'value' => $this->config['petioFallbackUser'],
+					'help' => 'Organizr will request an Petio User Token based off of this user credentials',
+				),
+				array(
+					'type' => 'password-alt',
+					'name' => 'petioFallbackPassword',
+					'label' => 'Petio Fallback Password',
+					'value' => $this->config['petioFallbackPassword'],
+				),
+				array(
+					'type' => 'switch',
+					'name' => 'ssoPetio',
+					'label' => 'Enable',
+					'value' => $this->config['ssoPetio']
+				)
+			),
 			'Ombi' => array(
 				array(
 					'type' => 'input',
@@ -3148,6 +3183,7 @@ class Organizr
 		$this->coookie('delete', 'oAuth');
 		$this->coookie('delete', 'jellyfin_credentials');
 		$this->coookie('delete', 'connect.sid');
+		$this->coookie('delete', 'petio_jwt');
 		$this->clearTautulliTokens();
 		$this->revokeTokenCurrentUser($this->user['token']);
 		$this->user = null;

+ 5 - 0
api/config/default.php

@@ -58,11 +58,16 @@ return array(
 	'overseerrToken' => '',
 	'overseerrFallbackUser' => '',
 	'overseerrFallbackPassword' => '',
+	'petioURL' => '',
+	'petioToken' => '',
+	'petioFallbackUser' => '',
+	'petioFallbackPassword' => '',
 	'ssoPlex' => false,
 	'ssoOmbi' => false,
 	'ssoTautulli' => false,
 	'ssoJellyfin' => false,
 	'ssoOverseerr' => false,
+	'ssoPetio' => false,
 	'sonarrURL' => '',
 	'sonarrUnmonitored' => false,
 	'sonarrToken' => '',

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

@@ -8,7 +8,8 @@ trait SSOFunctions
 			'jellyfin' => 'username',
 			'ombi' => 'username',
 			'overseerr' => 'username',
-			'tautulli' => 'username'
+			'tautulli' => 'username',
+			'petio' => 'username'
 		);
 		return (gettype($userobj) == 'string') ? $userobj : $userobj[$map[$app]];
 	}
@@ -45,6 +46,12 @@ trait SSOFunctions
 				$this->coookie('set', 'connect.sid', $overseerrToken, $this->config['rememberMeDays'], false);
 			}
 		}
+		if ($this->config['ssoPetio']) {
+			$petioToken = $this->getPetioToken($this->getSSOUserFor('petio', $userobj), $password, $token);
+			if ($petioToken) {
+				$this->coookie('set', 'petio_jwt', $petioToken, $this->config['rememberMeDays'], false);
+			}
+		}
 		return true;
 	}
 	
@@ -197,4 +204,46 @@ trait SSOFunctions
 		}
 	}
 	
+	public function getPetioToken($username, $password, $oAuthToken = null, $fallback = false)
+	{
+		$token = null;
+		try {
+			$url = $this->qualifyURL($this->config['petioURL']);
+			$headers = array(
+				"Content-Type" => "application/json"
+			);
+			$data = array(
+				'user' => [
+					'username' => ($oAuthToken ? '' : $username),
+					'password' => ($oAuthToken ? '' : $password),
+					'type' => 1,
+				],
+				'authToken' => false,
+				'token' => $oAuthToken
+			);
+			$endpoint = ($oAuthToken) ? '/api/login/plex_login' : '/api/login';
+			$options = $this->requestOptions($url, false, 60);
+			$response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
+			if ($response->success) {
+				$user = json_decode($response->body, true)['user'];
+				$token = json_decode($response->body, true)['token'];
+				$this->writeLog('success', 'Petio Token Function - Grabbed token', $user['username']);
+			} else {
+				if ($fallback) {
+					$this->writeLog('error', 'Petio Token Function - Petio did not return Token - Will retry using fallback credentials', $username);
+				} else {
+					$this->writeLog('error', 'Petio Token Function - Petio did not return Token', $username);
+				}
+			}
+		} catch (Requests_Exception $e) {
+			$this->writeLog('error', 'Petio Token Function - Error: ' . $e->getMessage(), $username);
+		}
+		if ($token) {
+			return $token;
+		} elseif ($fallback) {
+			return $this->getPetioToken($this->config['petioFallbackUser'], $this->decrypt($this->config['petioFallbackPassword']), null, false);
+		} else {
+			return false;
+		}
+	}
 }

+ 1 - 0
organizr

@@ -0,0 +1 @@
+Subproject commit 0d0dea8edaa7bc3a943988316d6f367cb1187274