Merge pull request #11681 from Quix0r/fixes/item-guidfromuri-allow-null

Allowing Item::getFromUri()'s 2nd parameter a null value
This commit is contained in:
Hypolite Petovan 2022-06-24 10:15:26 -04:00 committed by GitHub
commit c5b46fe748
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 12 deletions

View file

@ -1830,11 +1830,11 @@ class Item
* Posts that are created on this system are using System::createUUID. * Posts that are created on this system are using System::createUUID.
* Received ActivityPub posts are using Processor::getGUIDByURL. * Received ActivityPub posts are using Processor::getGUIDByURL.
* *
* @param string $uri uri of an item entry * @param string $uri uri of an item entry
* @param string $host hostname for the GUID prefix * @param string|null $host hostname for the GUID prefix
* @return string unique guid * @return string Unique guid
*/ */
public static function guidFromUri(string $uri, string $host): string public static function guidFromUri(string $uri, string $host = null): string
{ {
// Our regular guid routine is using this kind of prefix as well // Our regular guid routine is using this kind of prefix as well
// We have to avoid that different routines could accidentally create the same value // We have to avoid that different routines could accidentally create the same value

View file

@ -313,7 +313,7 @@ class Feed
$item['uri'] = $guid; $item['uri'] = $guid;
// Don't use the GUID value directly but instead use it as a basis for the GUID // Don't use the GUID value directly but instead use it as a basis for the GUID
$item['guid'] = Item::guidFromUri($guid, parse_url($guid, PHP_URL_HOST) ?? parse_url($item['plink'], PHP_URL_HOST) ?? ''); $item['guid'] = Item::guidFromUri($guid, parse_url($guid, PHP_URL_HOST) ?? parse_url($item['plink'], PHP_URL_HOST));
} }
if (empty($item['uri'])) { if (empty($item['uri'])) {

View file

@ -31,12 +31,24 @@ use Friendica\Model\APContact;
*/ */
class LDSignature class LDSignature
{ {
public static function isSigned($data) /**
* Checks if element 'signature' is found and not empty
*
* @param array $data
* @return bool
*/
public static function isSigned(array $data): bool
{ {
return !empty($data['signature']); return !empty($data['signature']);
} }
public static function getSigner($data) /**
* Returns actor (signer) from given data
*
* @param array $data
* @return mixed Returns actor or false on error
*/
public static function getSigner(array $data)
{ {
if (!self::isSigned($data)) { if (!self::isSigned($data)) {
return false; return false;
@ -66,13 +78,20 @@ class LDSignature
} }
} }
public static function sign($data, $owner) /**
* Signs given data by owner's signature
*
* @param array $data Data to sign
* @param array $owner Owner information, like URL
* @return array Merged array of $data and signature
*/
public static function sign(array $data, array $owner): array
{ {
$options = [ $options = [
'type' => 'RsaSignature2017', 'type' => 'RsaSignature2017',
'nonce' => Strings::getRandomHex(64), 'nonce' => Strings::getRandomHex(64),
'creator' => $owner['url'] . '#main-key', 'creator' => $owner['url'] . '#main-key',
'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM) 'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
]; ];
$ohash = self::hash(self::signableOptions($options)); $ohash = self::hash(self::signableOptions($options));
@ -82,13 +101,25 @@ class LDSignature
return array_merge($data, ['signature' => $options]); return array_merge($data, ['signature' => $options]);
} }
private static function signableData($data) /**
* Removes element 'signature' from array
*
* @param array $data
* @return array With no element 'signature'
*/
private static function signableData(array $data): array
{ {
unset($data['signature']); unset($data['signature']);
return $data; return $data;
} }
private static function signableOptions($options) /**
* Removes some elements and adds '@context' to it
*
* @param array $options
* @return array With some removed elements and added '@context' element
*/
private static function signableOptions(array $options): array
{ {
$newopts = ['@context' => 'https://w3id.org/identity/v1']; $newopts = ['@context' => 'https://w3id.org/identity/v1'];
@ -99,7 +130,13 @@ class LDSignature
return array_merge($newopts, $options); return array_merge($newopts, $options);
} }
private static function hash($obj) /**
* Hashes normalized object
*
* @param ??? $obj
* @return string SHA256 hash
*/
private static function hash($obj): string
{ {
return hash('sha256', JsonLD::normalize($obj)); return hash('sha256', JsonLD::normalize($obj));
} }