streams/Zotlabs/Module/Owa.php

63 lines
1.9 KiB
PHP
Raw Normal View History

2017-09-08 00:56:02 +00:00
<?php
namespace Zotlabs\Module;
2017-09-08 23:00:27 +00:00
/**
* OpenWebAuth verifier and token generator
* See https://macgirvin.com/wiki/mike/OpenWebAuth/Home
* Requests to this endpoint should be signed using HTTP Signatures
* using the 'Authorization: Signature' authentication method
* If the signature verifies a token is returned.
*
* This token may be exchanged for an authenticated cookie.
*/
2017-09-08 00:56:02 +00:00
class Owa extends \Zotlabs\Web\Controller {
function init() {
2017-09-09 20:34:57 +00:00
$ret = [ 'success' => false ];
foreach([ 'REDIRECT_REMOTE_USER', 'HTTP_AUTHORIZATION' ] as $head) {
2017-09-08 00:56:02 +00:00
if(array_key_exists($head,$_SERVER) && substr(trim($_SERVER[$head]),0,9) === 'Signature') {
if($head !== 'HTTP_AUTHORIZATION') {
$_SERVER['HTTP_AUTHORIZATION'] = $_SERVER[$head];
continue;
}
$sigblock = \Zotlabs\Web\HTTPSig::parse_sigheader($_SERVER[$head]);
if($sigblock) {
$keyId = $sigblock['keyId'];
if($keyId) {
$r = q("select * from hubloc left join xchan on hubloc_hash = xchan_hash
where hubloc_addr = '%s' ",
2017-09-08 00:56:02 +00:00
dbesc(str_replace('acct:','',$keyId))
);
if($r) {
foreach($r as $hubloc) {
$verified = \Zotlabs\Web\HTTPSig::verify('',$hubloc['xchan_pubkey']);
if($verified && $verified['header_signed'] && $verified['header_valid']) {
2018-01-13 22:08:15 +00:00
logger('OWA header: ' . print_r($verified,true),LOGGER_DATA);
2018-01-13 20:24:55 +00:00
logger('OWA success: ' . $hubloc['hubloc_addr'],LOGGER_DATA);
$ret['success'] = true;
$token = random_string(32);
\Zotlabs\Lib\Verify::create('owt',0,$token,$hubloc['hubloc_addr']);
$result = '';
openssl_public_encrypt($token,$result,$hubloc['xchan_pubkey']);
$ret['encrypted_token'] = base64url_encode($result);
break;
}
2018-01-13 20:24:55 +00:00
else {
logger('OWA fail: ' . $hubloc['hubloc_id'] . ' ' . $hubloc['hubloc_addr']);
}
2017-09-08 00:56:02 +00:00
}
}
}
}
}
}
2017-09-09 20:34:57 +00:00
json_return_and_die($ret,'application/x-zot+json');
2017-09-08 00:56:02 +00:00
}
}