streams/Zotlabs/Zot/Finger.php

147 lines
4 KiB
PHP
Raw Normal View History

2016-05-22 01:21:04 +00:00
<?php
namespace Zotlabs\Zot;
/**
* @brief Finger
*
*/
2016-05-22 01:21:04 +00:00
class Finger {
static private $token;
/**
* @brief Look up information about channel.
*
* @param string $webbie
* does not have to be host qualified e.g. 'foo' is treated as 'foo\@thishub'
* @param array $channel
* (optional), if supplied permissions will be enumerated specifically for $channel
* @param boolean $autofallback
* fallback/failover to http if https connection cannot be established. Default is true.
*
* @return zotinfo array (with 'success' => true) or array('success' => false);
2016-05-22 01:21:04 +00:00
*/
2017-09-05 04:49:44 +00:00
2016-05-22 01:21:04 +00:00
static public function run($webbie, $channel = null, $autofallback = true) {
$ret = array('success' => false);
self::$token = random_string();
if (strpos($webbie, '@') === false) {
2016-05-22 01:21:04 +00:00
$address = $webbie;
$host = \App::get_hostname();
2016-05-22 01:21:04 +00:00
} else {
$address = substr($webbie,0,strpos($webbie,'@'));
$host = substr($webbie,strpos($webbie,'@')+1);
if(strpos($host,'/'))
$host = substr($host,0,strpos($host,'/'));
2016-05-22 01:21:04 +00:00
}
$xchan_addr = $address . '@' . $host;
if ((! $address) || (! $xchan_addr)) {
logger('zot_finger: no address :' . $webbie);
2016-05-22 01:21:04 +00:00
return $ret;
}
logger('using xchan_addr: ' . $xchan_addr, LOGGER_DATA, LOG_DEBUG);
// potential issue here; the xchan_addr points to the primary hub.
// The webbie we were called with may not, so it might not be found
// unless we query for hubloc_addr instead of xchan_addr
$r = q("select xchan.*, hubloc.* from xchan
left join hubloc on xchan_hash = hubloc_hash
where xchan_addr = '%s' and hubloc_primary = 1 limit 1",
dbesc($xchan_addr)
);
if($r) {
2016-05-22 01:21:04 +00:00
$url = $r[0]['hubloc_url'];
if($r[0]['hubloc_network'] && $r[0]['hubloc_network'] !== 'zot') {
2016-05-22 01:21:04 +00:00
logger('zot_finger: alternate network: ' . $webbie);
logger('url: ' . $url . ', net: ' . var_export($r[0]['hubloc_network'],true), LOGGER_DATA, LOG_DEBUG);
2016-05-22 01:21:04 +00:00
return $ret;
}
} else {
2016-05-22 01:21:04 +00:00
$url = 'https://' . $host;
}
$rhs = '/.well-known/zot-info';
$https = ((strpos($url,'https://') === 0) ? true : false);
logger('zot_finger: ' . $address . ' at ' . $url, LOGGER_DEBUG);
if ($channel) {
$postvars = array(
'address' => $address,
'target' => $channel['channel_guid'],
'target_sig' => $channel['channel_guid_sig'],
'key' => $channel['channel_pubkey'],
'token' => self::$token
);
2017-09-05 04:49:44 +00:00
$headers = [];
$headers['X-Zot-Channel'] = $channel['channel_address'] . '@' . \App::get_hostname();
$headers['X-Zot-Nonce'] = random_string();
$xhead = \Zotlabs\Web\HTTPSig::create_sig('',$headers,$channel['channel_prvkey'],
'acct:' . $channel['channel_address'] . '@' . \App::get_hostname(),false);
$retries = 0;
$result = z_post_url($url . $rhs,$postvars,$retries, [ 'headers' => $xhead ]);
2016-05-22 01:21:04 +00:00
if ((! $result['success']) && ($autofallback)) {
if ($https) {
logger('zot_finger: https failed. falling back to http');
2017-09-05 04:49:44 +00:00
$result = z_post_url('http://' . $host . $rhs,$postvars, $retries, [ 'headers' => $xhead ]);
2016-05-22 01:21:04 +00:00
}
}
2017-09-05 04:49:44 +00:00
}
else {
2016-05-22 01:21:04 +00:00
$rhs .= '?f=&address=' . urlencode($address) . '&token=' . self::$token;
2017-09-05 04:49:44 +00:00
$result = z_fetch_url($url . $rhs);
if((! $result['success']) && ($autofallback)) {
if($https) {
2016-05-22 01:21:04 +00:00
logger('zot_finger: https failed. falling back to http');
$result = z_fetch_url('http://' . $host . $rhs);
}
}
}
if(! $result['success']) {
logger('zot_finger: no results');
2016-05-22 01:21:04 +00:00
return $ret;
}
$x = json_decode($result['body'], true);
2017-09-06 01:56:25 +00:00
$verify = \Zotlabs\Web\HTTPSig::verify($result,(($x) ? $x['key'] : ''));
2017-09-08 01:52:18 +00:00
2017-09-05 04:49:44 +00:00
if($x && (! $verify['header_valid'])) {
$signed_token = ((is_array($x) && array_key_exists('signed_token', $x)) ? $x['signed_token'] : null);
2016-05-22 01:21:04 +00:00
if($signed_token) {
$valid = rsa_verify('token.' . self::$token, base64url_decode($signed_token), $x['key']);
2016-05-22 01:21:04 +00:00
if(! $valid) {
logger('invalid signed token: ' . $url . $rhs, LOGGER_NORMAL, LOG_ERR);
2016-05-22 01:21:04 +00:00
return $ret;
}
}
else {
logger('No signed token from ' . $url . $rhs, LOGGER_NORMAL, LOG_WARNING);
return $ret;
2016-05-22 01:21:04 +00:00
}
}
return $x;
}
}