X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FLDSignature.php;h=8cfadb16dada19da23ac65ccf997bb610bd448af;hb=0b1f67f5b33cd9a57e9bebeec76cbb8898f2ab27;hp=9762b837f3bca737269d4ca87f78d95d5f45f855;hpb=cb3f09ae4f344ff83fca9dc435f3cbad1972737f;p=friendica.git diff --git a/src/Util/LDSignature.php b/src/Util/LDSignature.php index 9762b837f3..8cfadb16da 100644 --- a/src/Util/LDSignature.php +++ b/src/Util/LDSignature.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Util; @@ -6,18 +25,30 @@ use Friendica\Core\Logger; use Friendica\Model\APContact; /** - * @brief Implements JSON-LD signatures + * Implements JSON-LD signatures * * Ported from Osada: https://framagit.org/macgirvin/osada */ 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']); } - 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)) { return false; @@ -38,7 +69,7 @@ class LDSignature $dhash = self::hash(self::signableData($data)); $x = Crypto::rsaVerify($ohash . $dhash, base64_decode($data['signature']['signatureValue']), $pubkey); - Logger::log('LD-verify: ' . intval($x)); + Logger::info('LD-verify', ['verified' => (int)$x, 'actor' => $profile['url']]); if (empty($x)) { return false; @@ -47,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 = [ 'type' => 'RsaSignature2017', 'nonce' => Strings::getRandomHex(64), 'creator' => $owner['url'] . '#main-key', - 'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM) + 'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM), ]; $ohash = self::hash(self::signableOptions($options)); @@ -63,13 +101,25 @@ class LDSignature 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']); 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']; @@ -80,7 +130,13 @@ class LDSignature 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)); }