Make tests great again !
(Also use only one field for public/private key pem) Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -253,7 +253,7 @@ defmodule Eventos.Service.ActivityPub do
|
||||
preferred_username: data["preferredUsername"],
|
||||
follower_address: data["followers"],
|
||||
summary: data["summary"],
|
||||
public_key: data["publicKey"]["publicKeyPem"],
|
||||
keys: data["publicKey"]["publicKeyPem"],
|
||||
inbox_url: data["inbox"],
|
||||
outbox_url: data["outbox"],
|
||||
following_url: data["following"],
|
||||
|
||||
@@ -304,4 +304,31 @@ defmodule Eventos.Service.ActivityPub.Utils do
|
||||
}
|
||||
|> Map.merge(additional)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts PEM encoded keys to a public key representation
|
||||
"""
|
||||
def pem_to_public_key(pem) do
|
||||
[private_key_code] = :public_key.pem_decode(pem)
|
||||
private_key = :public_key.pem_entry_decode(private_key_code)
|
||||
{:RSAPrivateKey, _, modulus, exponent, _, _, _, _, _, _, _} = private_key
|
||||
{:RSAPublicKey, modulus, exponent}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts PEM encoded keys to a private key representation
|
||||
"""
|
||||
def pem_to_private_key(pem) do
|
||||
[private_key_code] = :public_key.pem_decode(pem)
|
||||
:public_key.pem_entry_decode(private_key_code)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts PEM encoded keys to a PEM public key representation
|
||||
"""
|
||||
def pem_to_public_key_pem(pem) do
|
||||
public_key = pem_to_public_key(pem)
|
||||
public_key = :public_key.pem_entry_encode(:RSAPublicKey, public_key)
|
||||
:public_key.pem_encode([public_key])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,7 +32,7 @@ defmodule Eventos.Service.HTTPSignatures do
|
||||
# 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.
|
||||
with actor_id <- conn.params["actor"],
|
||||
{:ok, public_key_code} <- Actor.get_public_key_for_url(actor_id),
|
||||
public_key_code <- Actor.get_public_key_for_url(actor_id),
|
||||
[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
|
||||
@@ -42,7 +42,7 @@ defmodule Eventos.Service.HTTPSignatures do
|
||||
# Fetch user anew and try one more time
|
||||
with actor_id <- conn.params["actor"],
|
||||
{:ok, _actor} <- ActivityPub.make_actor_from_url(actor_id),
|
||||
{:ok, public_key_code} <- Actor.get_public_key_for_url(actor_id),
|
||||
public_key_code <- Actor.get_public_key_for_url(actor_id),
|
||||
[public_key] = :public_key.pem_decode(public_key_code),
|
||||
public_key = :public_key.pem_entry_decode(public_key) do
|
||||
validate_conn(conn, public_key)
|
||||
@@ -70,10 +70,8 @@ defmodule Eventos.Service.HTTPSignatures do
|
||||
|> Enum.join("\n")
|
||||
end
|
||||
|
||||
def sign(actor, headers) do
|
||||
with {:ok, private_key_code} = Actor.get_private_key_for_actor(actor),
|
||||
[private_key] = :public_key.pem_decode(private_key_code),
|
||||
private_key = :public_key.pem_entry_decode(private_key) do
|
||||
def sign(%Actor{} = actor, headers) do
|
||||
with private_key = Actor.get_keys_for_actor(actor) do
|
||||
sigstring = build_signing_string(headers, Map.keys(headers))
|
||||
|
||||
signature =
|
||||
|
||||
Reference in New Issue
Block a user