Simplify and fix public key stuff

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-11-07 18:12:21 +01:00
parent 0bd52b7bee
commit bd41864fcf
2 changed files with 9 additions and 8 deletions

View File

@ -183,7 +183,6 @@ defmodule Mobilizon.Actors.Actor do
def get_public_key_for_url(url) do def get_public_key_for_url(url) do
with {:ok, %Actor{} = actor} <- Actors.get_or_fetch_by_url(url) do with {:ok, %Actor{} = actor} <- Actors.get_or_fetch_by_url(url) do
actor.keys actor.keys
|> Mobilizon.Service.ActivityPub.Utils.pem_to_public_key()
else else
_ -> :error _ -> :error
end end

View File

@ -41,13 +41,17 @@ defmodule Mobilizon.Service.HTTPSignatures do
:public_key.verify(sigstring, :sha256, sig, public_key) :public_key.verify(sigstring, :sha256, sig, public_key)
end end
defp prepare_public_key(public_key_code) do
with [public_key_entry] <- :public_key.pem_decode(public_key_code) do
:public_key.pem_entry_decode(public_key_entry)
end
end
def validate_conn(conn) do def validate_conn(conn) do
# TODO: How to get the right key and see if it is actually valid for that request. # TODO: How to get the right key and see if it is actually valid for that request.
# For now, fetch the key for the actor. # For now, fetch the key for the actor.
with actor_id <- conn.params["actor"], with public_key <-
public_key_code <- Actor.get_public_key_for_url(actor_id), conn.params["actor"] |> Actor.get_public_key_for_url() |> prepare_public_key() do
[public_key] <- :public_key.pem_decode(public_key_code),
public_key <- :public_key.pem_entry_decode(public_key) do
if validate_conn(conn, public_key) do if validate_conn(conn, public_key) do
true true
else else
@ -55,9 +59,7 @@ defmodule Mobilizon.Service.HTTPSignatures do
# Fetch user anew and try one more time # Fetch user anew and try one more time
with actor_id <- conn.params["actor"], with actor_id <- conn.params["actor"],
{:ok, _actor} <- ActivityPub.make_actor_from_url(actor_id), {:ok, _actor} <- ActivityPub.make_actor_from_url(actor_id),
public_key_code <- Actor.get_public_key_for_url(actor_id), public_key <- actor_id |> Actor.get_public_key_for_url() |> prepare_public_key() do
[public_key] <- :public_key.pem_decode(public_key_code),
public_key <- :public_key.pem_entry_decode(public_key) do
validate_conn(conn, public_key) validate_conn(conn, public_key)
end end
end end