123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- declare(strict_types=1);
- namespace Plugin\ActivityPub\Entity;
- use App\Core\DB\DB;
- use App\Core\Entity;
- use App\Core\Log;
- use App\Entity\Actor;
- use App\Util\Exception\ServerException;
- use DateTimeInterface;
- class ActivitypubRsa extends Entity
- {
-
-
- private int $actor_id;
- private ?string $private_key = null;
- private string $public_key;
- private DateTimeInterface $created;
- private DateTimeInterface $modified;
- public function getActorId(): int
- {
- return $this->actor_id;
- }
- public function setActorId(int $actor_id): self
- {
- $this->actor_id = $actor_id;
- return $this;
- }
- public function getPrivateKey(): string
- {
- return $this->private_key;
- }
- public function setPrivateKey(string $private_key): self
- {
- $this->private_key = $private_key;
- return $this;
- }
- public function getPublicKey(): string
- {
- return $this->public_key;
- }
- public function setPublicKey(string $public_key): self
- {
- $this->public_key = $public_key;
- return $this;
- }
- public function getCreated(): DateTimeInterface
- {
- return $this->created;
- }
- public function setCreated(DateTimeInterface $created): self
- {
- $this->created = $created;
- return $this;
- }
- public function getModified(): DateTimeInterface
- {
- return $this->modified;
- }
- public function setModified(DateTimeInterface $modified): self
- {
- $this->modified = $modified;
- return $this;
- }
-
-
-
- public static function schemaDef(): array
- {
- return [
- 'name' => 'activitypub_rsa',
- 'fields' => [
- 'actor_id' => ['type' => 'int', 'not null' => true],
- 'private_key' => ['type' => 'text'],
- 'public_key' => ['type' => 'text', 'not null' => true],
- 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
- 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
- ],
- 'primary key' => ['actor_id'],
- 'foreign keys' => [
- 'activitypub_rsa_actor_id_fkey' => ['actor', ['actor_id' => 'id']],
- ],
- ];
- }
-
- public static function getByActor(Actor $gsactor, bool $fetch = true): self
- {
- $apRSA = self::getByPK(['actor_id' => ($actor_id = $gsactor->getId())]);
- if (is_null($apRSA)) {
-
- if ($gsactor->getIsLocal()) {
- self::generateKeys($private_key, $public_key);
- $apRSA = self::create([
- 'actor_id' => $actor_id,
- 'private_key' => $private_key,
- 'public_key' => $public_key,
- ]);
- DB::wrapInTransaction(fn() => DB::persist($apRSA));
- } else {
-
- Log::error("Activitypub_rsa: An impossible thing has happened... Please let the devs know.");
- if ($fetch) {
-
-
-
- } else {
- throw new ServerException('Activitypub_rsa: Failed to find keys for given profile. That should have not happened!');
- }
- }
- }
- return $apRSA;
- }
-
- private static function generateKeys(?string &$private_key, ?string &$public_key): void
- {
- $config = [
- 'digest_alg' => 'sha512',
- 'private_key_bits' => 2048,
- 'private_key_type' => OPENSSL_KEYTYPE_RSA,
- ];
-
- $res = openssl_pkey_new($config);
-
- openssl_pkey_export($res, $private_key);
-
- $pubKey = openssl_pkey_get_details($res);
- $public_key = $pubKey["key"];
- unset($pubKey);
- }
- }
|