User: Move public_key from source_data to own field
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Wed, 1 Apr 2020 04:58:48 +0000 (06:58 +0200)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Fri, 10 Apr 2020 04:16:18 +0000 (06:16 +0200)
lib/pleroma/user.ex
lib/pleroma/web/activity_pub/activity_pub.ex
priv/repo/migrations/20200401030751_users_add_public_key.exs [new file with mode: 0644]
test/signature_test.exs

index d030c73143b73a3bcc5e6164623ce4e707237c42..0adea42ec41e4d9b83fa0799d3450c793f761a09 100644 (file)
@@ -82,6 +82,7 @@ defmodule Pleroma.User do
     field(:password, :string, virtual: true)
     field(:password_confirmation, :string, virtual: true)
     field(:keys, :string)
+    field(:public_key, :string)
     field(:ap_id, :string)
     field(:avatar, :map)
     field(:local, :boolean, default: true)
@@ -366,6 +367,7 @@ defmodule Pleroma.User do
         :name,
         :ap_id,
         :nickname,
+        :public_key,
         :avatar,
         :ap_enabled,
         :source_data,
@@ -407,6 +409,7 @@ defmodule Pleroma.User do
         :bio,
         :name,
         :avatar,
+        :public_key,
         :locked,
         :no_rich_text,
         :default_scope,
@@ -503,6 +506,7 @@ defmodule Pleroma.User do
         :name,
         :follower_address,
         :following_address,
+        :public_key,
         :avatar,
         :last_refreshed_at,
         :ap_enabled,
@@ -1616,8 +1620,7 @@ defmodule Pleroma.User do
     |> set_cache()
   end
 
-  # AP style
-  def public_key(%{source_data: %{"publicKey" => %{"publicKeyPem" => public_key_pem}}}) do
+  def public_key(%{public_key: public_key_pem}) when is_binary(public_key_pem) do
     key =
       public_key_pem
       |> :public_key.pem_decode()
@@ -1627,7 +1630,7 @@ defmodule Pleroma.User do
     {:ok, key}
   end
 
-  def public_key(_), do: {:error, "not found key"}
+  def public_key(_), do: {:error, "key not found"}
 
   def get_public_key_for_ap_id(ap_id) do
     with {:ok, %User{} = user} <- get_or_fetch_by_ap_id(ap_id),
index 19286fd01a29f9f75dfb8ed78e9a2d94694368d3..0e4a9d842ba12aa1a74e2df452f97569a97bd921 100644 (file)
@@ -1432,6 +1432,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     discoverable = data["discoverable"] || false
     invisible = data["invisible"] || false
     actor_type = data["type"] || "Person"
+    public_key = data["publicKey"]["publicKeyPem"]
 
     user_data = %{
       ap_id: data["id"],
@@ -1449,7 +1450,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       following_address: data["following"],
       bio: data["summary"],
       actor_type: actor_type,
-      also_known_as: Map.get(data, "alsoKnownAs", [])
+      also_known_as: Map.get(data, "alsoKnownAs", []),
+      public_key: public_key
     }
 
     # nickname can be nil because of virtual actors
diff --git a/priv/repo/migrations/20200401030751_users_add_public_key.exs b/priv/repo/migrations/20200401030751_users_add_public_key.exs
new file mode 100644 (file)
index 0000000..04e5ad1
--- /dev/null
@@ -0,0 +1,17 @@
+defmodule Pleroma.Repo.Migrations.UsersAddPublicKey do
+  use Ecto.Migration
+
+  def up do
+    alter table(:users) do
+      add_if_not_exists(:public_key, :text)
+    end
+
+    execute("UPDATE users SET public_key = source_data->'publicKey'->>'publicKeyPem'")
+  end
+
+  def down do
+    alter table(:users) do
+      remove_if_exists(:public_key, :text)
+    end
+  end
+end
index 04736d8b9b788e7a0b85265c53840704679a4414..d5a2a62c4c90a715ae70298bdad00adc28b6f90f 100644 (file)
@@ -19,12 +19,7 @@ defmodule Pleroma.SignatureTest do
 
   @private_key "-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEA48qb4v6kqigZutO9Ot0wkp27GIF2LiVaADgxQORZozZR63jH\nTaoOrS3Xhngbgc8SSOhfXET3omzeCLqaLNfXnZ8OXmuhJfJSU6mPUvmZ9QdT332j\nfN/g3iWGhYMf/M9ftCKh96nvFVO/tMruzS9xx7tkrfJjehdxh/3LlJMMImPtwcD7\nkFXwyt1qZTAU6Si4oQAJxRDQXHp1ttLl3Ob829VM7IKkrVmY8TD+JSlV0jtVJPj6\n1J19ytKTx/7UaucYvb9HIiBpk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gftgYWUZfM5gE7T2mTkBYq8W+U\n8LetF+S9qAM2gDnaDx0kuUTCq7t87DKk6URuQ/SbI0wCzYjjRD99KxvChVGPBHKo\n1DjqMuECgYEAgJGNm7/lJCS2wk81whfy/ttKGsEIkyhPFYQmdGzSYC5aDc2gp1R3\nxtOkYEvdjfaLfDGEa4UX8CHHF+w3t9u8hBtcdhMH6GYb9iv6z0VBTt4A/11HUR49\n3Z7TQ18Iyh3jAUCzFV9IJlLIExq5Y7P4B3ojWFBN607sDCt8BMPbDYs=\n-----END RSA PRIVATE KEY-----"
 
-  @public_key %{
-    "id" => "https://mastodon.social/users/lambadalambda#main-key",
-    "owner" => "https://mastodon.social/users/lambadalambda",
-    "publicKeyPem" =>
-      "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0P/Tq4gb4G/QVuMGbJo\nC/AfMNcv+m7NfrlOwkVzcU47jgESuYI4UtJayissCdBycHUnfVUd9qol+eznSODz\nCJhfJloqEIC+aSnuEPGA0POtWad6DU0E6/Ho5zQn5WAWUwbRQqowbrsm/GHo2+3v\neR5jGenwA6sYhINg/c3QQbksyV0uJ20Umyx88w8+TJuv53twOfmyDWuYNoQ3y5cc\nHKOZcLHxYOhvwg3PFaGfFHMFiNmF40dTXt9K96r7sbzc44iLD+VphbMPJEjkMuf8\nPGEFOBzy8pm3wJZw2v32RNW2VESwMYyqDzwHXGSq1a73cS7hEnc79gXlELsK04L9\nQQIDAQAB\n-----END PUBLIC KEY-----\n"
-  }
+  @public_key "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0P/Tq4gb4G/QVuMGbJo\nC/AfMNcv+m7NfrlOwkVzcU47jgESuYI4UtJayissCdBycHUnfVUd9qol+eznSODz\nCJhfJloqEIC+aSnuEPGA0POtWad6DU0E6/Ho5zQn5WAWUwbRQqowbrsm/GHo2+3v\neR5jGenwA6sYhINg/c3QQbksyV0uJ20Umyx88w8+TJuv53twOfmyDWuYNoQ3y5cc\nHKOZcLHxYOhvwg3PFaGfFHMFiNmF40dTXt9K96r7sbzc44iLD+VphbMPJEjkMuf8\nPGEFOBzy8pm3wJZw2v32RNW2VESwMYyqDzwHXGSq1a73cS7hEnc79gXlELsK04L9\nQQIDAQAB\n-----END PUBLIC KEY-----\n"
 
   @rsa_public_key {
     :RSAPublicKey,
@@ -42,7 +37,7 @@ defmodule Pleroma.SignatureTest do
     test "it returns key" do
       expected_result = {:ok, @rsa_public_key}
 
-      user = insert(:user, source_data: %{"publicKey" => @public_key})
+      user = insert(:user, public_key: @public_key)
 
       assert Signature.fetch_public_key(make_fake_conn(user.ap_id)) == expected_result
     end
@@ -53,8 +48,8 @@ defmodule Pleroma.SignatureTest do
              end) =~ "[error] Could not decode user"
     end
 
-    test "it returns error if public key is empty" do
-      user = insert(:user, source_data: %{"publicKey" => %{}})
+    test "it returns error if public key is nil" do
+      user = insert(:user, public_key: nil)
 
       assert Signature.fetch_public_key(make_fake_conn(user.ap_id)) == {:error, :error}
     end