소스 검색

add fail safe for SSO options and login

causefx 8 년 전
부모
커밋
1267b58613
2개의 변경된 파일95개의 추가작업 그리고 79개의 파일을 삭제
  1. 52 44
      api/functions/auth-functions.php
  2. 43 35
      api/functions/sso-functions.php

+ 52 - 44
api/functions/auth-functions.php

@@ -14,58 +14,66 @@ function authRegister($username,$password,$defaults,$email){
 	}
 }
 function checkPlexUser($username){
-	if(!empty($GLOBALS['plexToken'])){
-		$url = 'https://plex.tv/pms/friends/all';
-		$headers = array(
-			'X-Plex-Token' => $GLOBALS['plexToken'],
-		);
-		$response = Requests::get($url, $headers);
-		if($response->success){
-			libxml_use_internal_errors(true);
-			$userXML = simplexml_load_string($response->body);
-			if (is_array($userXML) || is_object($userXML)) {
-				$usernameLower = strtolower($username);
-				foreach($userXML AS $child) {
-					if(isset($child['username']) && strtolower($child['username']) == $usernameLower || isset($child['email']) && strtolower($child['email']) == $usernameLower) {
-						return true;
+	try{
+		if(!empty($GLOBALS['plexToken'])){
+			$url = 'https://plex.tv/pms/friends/all';
+			$headers = array(
+				'X-Plex-Token' => $GLOBALS['plexToken'],
+			);
+			$response = Requests::get($url, $headers);
+			if($response->success){
+				libxml_use_internal_errors(true);
+				$userXML = simplexml_load_string($response->body);
+				if (is_array($userXML) || is_object($userXML)) {
+					$usernameLower = strtolower($username);
+					foreach($userXML AS $child) {
+						if(isset($child['username']) && strtolower($child['username']) == $usernameLower || isset($child['email']) && strtolower($child['email']) == $usernameLower) {
+							return true;
+						}
 					}
 				}
 			}
 		}
-	}
-	return false;
+		return false;
+	}catch( Requests_Exception $e ) {
+		writeLog('success', 'Plex User Check Function - Error: '.$e->getMessage(), $username);
+	};
 }
 function plugin_auth_plex($username, $password) {
-	$usernameLower = strtolower($username);
-	if(checkPlexUser($username)){
-		//Login User
-		$url = 'https://plex.tv/users/sign_in.json';
-		$headers = array(
-			'Accept'=> 'application/json',
-			'Content-Type' => 'application/x-www-form-urlencoded',
-			'X-Plex-Product' => 'Organizr',
-			'X-Plex-Version' => '2.0',
-			'X-Plex-Client-Identifier' => '01010101-10101010',
-		);
-		$data = array(
-			'user[login]' => $username,
-			'user[password]' => $password,
-		);
-		$response = Requests::post($url, $headers, $data);
-		if($response->success){
-			$json = json_decode($response->body, true);
-			if ((is_array($json) && isset($json['user']) && isset($json['user']['username'])) && strtolower($json['user']['username']) == $usernameLower || strtolower($json['user']['email']) == $usernameLower) {
-				//writeLog("success", $json['user']['username']." was logged into organizr using plex credentials");
-                return array(
-					'username' => $json['user']['username'],
-					'email' => $json['user']['email'],
-					'image' => $json['user']['thumb'],
-					'token' => $json['user']['authToken']
-				);
+	try{
+		$usernameLower = strtolower($username);
+		if(checkPlexUser($username)){
+			//Login User
+			$url = 'https://plex.tv/users/sign_in.json';
+			$headers = array(
+				'Accept'=> 'application/json',
+				'Content-Type' => 'application/x-www-form-urlencoded',
+				'X-Plex-Product' => 'Organizr',
+				'X-Plex-Version' => '2.0',
+				'X-Plex-Client-Identifier' => '01010101-10101010',
+			);
+			$data = array(
+				'user[login]' => $username,
+				'user[password]' => $password,
+			);
+			$response = Requests::post($url, $headers, $data);
+			if($response->success){
+				$json = json_decode($response->body, true);
+				if ((is_array($json) && isset($json['user']) && isset($json['user']['username'])) && strtolower($json['user']['username']) == $usernameLower || strtolower($json['user']['email']) == $usernameLower) {
+					//writeLog("success", $json['user']['username']." was logged into organizr using plex credentials");
+	                return array(
+						'username' => $json['user']['username'],
+						'email' => $json['user']['email'],
+						'image' => $json['user']['thumb'],
+						'token' => $json['user']['authToken']
+					);
+				}
 			}
 		}
-	}
-	return false;
+		return false;
+	}catch( Requests_Exception $e ) {
+		writeLog('success', 'Plex Auth Function - Error: '.$e->getMessage(), $username);
+	};
 }
 if (function_exists('ldap_connect')){
 	// Pass credentials to LDAP backend

+ 43 - 35
api/functions/sso-functions.php

@@ -20,41 +20,49 @@ function ssoCheck($username, $password, $token=null){
 	return true;
 }
 function getOmbiToken($username, $password){
-	$url = $GLOBALS['ombiURL'].'/api/v1/Token';
-	$token = null;
-	$headers = array(
-		"Accept" => "application/json",
-		"Content-Type" => "application/json"
-	);
-	$data = array(
-		"username" => $username,
-		"password" => $password,
-		"rememberMe" => "true",
-	);
-	$options = (localURL($url)) ? array('verify' => false ) : array();
-	$response = Requests::post($url, $headers, json_encode($data), $options);
-	if($response->success){
-		$token = json_decode($response->body, true)['access_token'];
-	}
-	return ($token) ? $token : false;
+	try{
+		$url = $GLOBALS['ombiURL'].'/api/v1/Token';
+		$token = null;
+		$headers = array(
+			"Accept" => "application/json",
+			"Content-Type" => "application/json"
+		);
+		$data = array(
+			"username" => $username,
+			"password" => $password,
+			"rememberMe" => "true",
+		);
+		$options = (localURL($url)) ? array('verify' => false ) : array();
+		$response = Requests::post($url, $headers, json_encode($data), $options);
+		if($response->success){
+			$token = json_decode($response->body, true)['access_token'];
+		}
+		return ($token) ? $token : false;
+	}catch( Requests_Exception $e ) {
+		writeLog('success', 'Ombi Token Function - Error: '.$e->getMessage(), $username);
+	};
 }
 function getTautulliToken($username, $password){
-	$url = $GLOBALS['tautulliURL'].'/auth/signin';
-	$token = null;
-	$headers = array(
-		"Accept" => "application/json",
-		"Content-Type" => "application/x-www-form-urlencoded"
-	);
-	$data = array(
-		"username" => $username,
-		"password" => $password,
-		"remember_me" => 1,
-	);
-	$options = (localURL($url)) ? array('verify' => false ) : array();
-	$response = Requests::post($url, $headers, $data, $options);
-	if($response->success){
-		$token['token'] = json_decode($response->body, true)['token'];
-		$token['uuid'] = json_decode($response->body, true)['uuid'];
-	}
-	return ($token) ? $token : false;
+	try{
+		$url = $GLOBALS['tautulliURL'].'/auth/signin';
+		$token = null;
+		$headers = array(
+			"Accept" => "application/json",
+			"Content-Type" => "application/x-www-form-urlencoded"
+		);
+		$data = array(
+			"username" => $username,
+			"password" => $password,
+			"remember_me" => 1,
+		);
+		$options = (localURL($url)) ? array('verify' => false ) : array();
+		$response = Requests::post($url, $headers, $data, $options);
+		if($response->success){
+			$token['token'] = json_decode($response->body, true)['token'];
+			$token['uuid'] = json_decode($response->body, true)['uuid'];
+		}
+		return ($token) ? $token : false;
+	}catch( Requests_Exception $e ) {
+		writeLog('success', 'Tautulli Token Function - Error: '.$e->getMessage(), $username);
+	};
 }