2018-12-27 11:24:04 +01:00
|
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/plugs/http_signature.ex
|
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
|
defmodule Mobilizon.Web.Plugs.HTTPSignatures do
|
2018-06-14 18:15:27 +02:00
|
|
|
|
@moduledoc """
|
|
|
|
|
Plug to check HTTP Signatures on every incoming request
|
|
|
|
|
"""
|
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
|
import Plug.Conn, only: [get_req_header: 2, put_req_header: 3, assign: 3]
|
2020-01-23 00:55:07 +01:00
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
|
|
def init(options) do
|
|
|
|
|
options
|
|
|
|
|
end
|
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
|
@spec call(Plug.Conn.t(), any) :: Plug.Conn.t()
|
2018-05-17 11:32:23 +02:00
|
|
|
|
def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
|
|
|
|
|
conn
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call(conn, _opts) do
|
2021-09-24 16:46:42 +02:00
|
|
|
|
signature = conn |> get_req_header("signature") |> List.first()
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
|
if is_nil(signature) do
|
|
|
|
|
Logger.debug("No signature header!")
|
|
|
|
|
conn
|
|
|
|
|
else
|
|
|
|
|
# set (request-target) header to the appropriate value
|
|
|
|
|
# we also replace the digest header with the one we computed
|
|
|
|
|
conn =
|
|
|
|
|
conn
|
|
|
|
|
|> put_req_header(
|
|
|
|
|
"(request-target)",
|
|
|
|
|
String.downcase("#{conn.method}") <> " #{conn.request_path}"
|
|
|
|
|
)
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
|
conn =
|
|
|
|
|
if conn.assigns[:digest] do
|
|
|
|
|
conn
|
|
|
|
|
|> put_req_header("digest", conn.assigns[:digest])
|
2019-07-30 16:40:59 +02:00
|
|
|
|
else
|
|
|
|
|
conn
|
|
|
|
|
end
|
2018-11-12 18:17:53 +01:00
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
|
signature_valid = HTTPSignatures.validate_conn(conn)
|
|
|
|
|
Logger.debug("Is signature valid ? #{inspect(signature_valid)}")
|
|
|
|
|
date_valid = date_valid?(conn)
|
|
|
|
|
assign(conn, :valid_signature, signature_valid && date_valid)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2020-02-14 09:22:17 +01:00
|
|
|
|
|
|
|
|
|
@spec date_valid?(Plug.Conn.t()) :: boolean()
|
|
|
|
|
defp date_valid?(conn) do
|
2021-09-24 16:46:42 +02:00
|
|
|
|
date = conn |> get_req_header("date") |> List.first()
|
|
|
|
|
|
|
|
|
|
if is_nil(date) do
|
|
|
|
|
false
|
2020-02-14 09:22:17 +01:00
|
|
|
|
else
|
2021-09-24 16:46:42 +02:00
|
|
|
|
case Timex.parse(date, "{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT") do
|
|
|
|
|
{:ok, %NaiveDateTime{} = date} ->
|
|
|
|
|
date
|
|
|
|
|
|> DateTime.from_naive!("Etc/UTC")
|
|
|
|
|
|> date_diff_ok?()
|
|
|
|
|
|
|
|
|
|
{:ok, %DateTime{} = date} ->
|
|
|
|
|
date_diff_ok?(date)
|
|
|
|
|
|
|
|
|
|
{:error, _err} ->
|
|
|
|
|
false
|
|
|
|
|
end
|
2020-02-14 09:22:17 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2021-09-24 16:46:42 +02:00
|
|
|
|
|
|
|
|
|
@spec date_diff_ok?(DateTime.t()) :: boolean()
|
|
|
|
|
defp date_diff_ok?(%DateTime{} = date) do
|
|
|
|
|
DateTime.diff(date, DateTime.utc_now()) <= 12 * 3600 &&
|
|
|
|
|
DateTime.diff(date, DateTime.utc_now()) >= -12 * 3600
|
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|