server to server magic auth
This commit is contained in:
		| @@ -7,6 +7,7 @@ | |||||||
| function api_login(&$a){ | function api_login(&$a){ | ||||||
|  |  | ||||||
| 	$record = null; | 	$record = null; | ||||||
|  | 	$remote_auth = false; | ||||||
|  |  | ||||||
| 	require_once('include/oauth.php'); | 	require_once('include/oauth.php'); | ||||||
|  |  | ||||||
| @@ -34,6 +35,9 @@ function api_login(&$a){ | |||||||
| 	// workarounds for HTTP-auth in CGI mode | 	// workarounds for HTTP-auth in CGI mode | ||||||
|  |  | ||||||
| 	foreach([ 'REDIRECT_REMOTE_USER', 'HTTP_AUTHORIZATION' ] as $head) { | 	foreach([ 'REDIRECT_REMOTE_USER', 'HTTP_AUTHORIZATION' ] as $head) { | ||||||
|  |  | ||||||
|  | 		/* Basic authentication */ | ||||||
|  |  | ||||||
| 		if(array_key_exists($head,$_SERVER) && substr(trim($_SERVER[$head]),0,5) === 'Basic') { | 		if(array_key_exists($head,$_SERVER) && substr(trim($_SERVER[$head]),0,5) === 'Basic') { | ||||||
| 			$userpass = @base64_decode(substr(trim($_SERVER[$head]),6)) ; | 			$userpass = @base64_decode(substr(trim($_SERVER[$head]),6)) ; | ||||||
| 			if(strlen($userpass)) { | 			if(strlen($userpass)) { | ||||||
| @@ -43,6 +47,52 @@ function api_login(&$a){ | |||||||
| 			} | 			} | ||||||
| 			break; | 			break; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		/* Signature authentication */ | ||||||
|  |  | ||||||
|  | 		if(array_key_exists($head,$_SERVER) && substr(trim($_SERVER[$head]),0,5) === 'Signature') { | ||||||
|  | 			$sigblock = \Zotlabs\Web\HTTPSig::parse_sigheader($_SERVER[$head]); | ||||||
|  | 			if($sigblock) { | ||||||
|  | 				$keyId = $sigblock['keyId']; | ||||||
|  | 				if($keyId) { | ||||||
|  | 					$r = q("select * from hubloc where hubloc_addr = '%s' limit 1", | ||||||
|  | 						dbesc($keyId) | ||||||
|  | 					); | ||||||
|  | 					if($r) { | ||||||
|  | 						$c = channelx_by_hash($r[0]['hubloc_hash']); | ||||||
|  | 						if($c) { | ||||||
|  | 							$a = q("select * from account where account_id = %d limit 1", | ||||||
|  | 								intval($c[0]['channel_account_id']) | ||||||
|  | 							); | ||||||
|  | 							if($a) { | ||||||
|  | 								$record = [ 'channel' => $c[0], 'account' => $a[0] ]; | ||||||
|  | 								$channel_login = $c[0]['channel_id']; | ||||||
|  | 							} | ||||||
|  | 							else { | ||||||
|  | 								continue; | ||||||
|  | 							} | ||||||
|  | 						} | ||||||
|  | 						else { | ||||||
|  | 							continue; | ||||||
|  | 						} | ||||||
|  | 					} | ||||||
|  | 					else { | ||||||
|  | 						continue; | ||||||
|  | 					} | ||||||
|  |  | ||||||
|  | 					if($head !== 'HTTP_AUTHORIZATION') { | ||||||
|  | 						$_SERVER['HTTP_AUTHORIZATION'] = $_SERVER[$head]; | ||||||
|  | 					} | ||||||
|  | 					if($record) {					 | ||||||
|  | 						$verified = \Zotlabs\Web\HTTPSig::verify('',$record['channel']['channel_pubkey']); | ||||||
|  | 						if(! ($verified && $verified['header_signed'] && $verified['header_valid'])) { | ||||||
|  | 							$record = null; | ||||||
|  | 						} | ||||||
|  | 						break; | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	require_once('include/auth.php'); | 	require_once('include/auth.php'); | ||||||
| @@ -58,8 +108,6 @@ function api_login(&$a){ | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 	if($record['account']) { | 	if($record['account']) { | ||||||
| 		authenticate_success($record['account']); | 		authenticate_success($record['account']); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user