From f21eaa87bb0cdcfa251f819ae7d40ab6ecb27a70 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 11 Nov 2019 11:56:34 -0500 Subject: [PATCH] add anonyhash --- config/prod.secret.exs | 1 + lib/philomena/attribution.ex | 17 +++++++++++++++++ lib/philomena/comments/attribution.ex | 9 +++++++++ lib/philomena/images/attribution.ex | 9 +++++++++ lib/philomena/posts/attribution.ex | 9 +++++++++ .../user_attribution/_anon_user.html.slime | 3 ++- .../views/user_attribution_view.ex | 16 ++++++++++++++++ 7 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 lib/philomena/attribution.ex create mode 100644 lib/philomena/comments/attribution.ex create mode 100644 lib/philomena/images/attribution.ex create mode 100644 lib/philomena/posts/attribution.ex diff --git a/config/prod.secret.exs b/config/prod.secret.exs index b857aab1..fa57d8f4 100644 --- a/config/prod.secret.exs +++ b/config/prod.secret.exs @@ -15,6 +15,7 @@ config :bcrypt_elixir, log_rounds: String.to_integer(System.get_env("BCRYPT_ROUNDS") || "12") config :philomena, + anonymous_name_salt: System.get_env("ANONYMOUS_NAME_SALT"), password_pepper: System.get_env("PASSWORD_PEPPER"), otp_secret_key: System.get_env("OTP_SECRET_KEY"), image_url_root: System.get_env("IMAGE_URL_ROOT"), diff --git a/lib/philomena/attribution.ex b/lib/philomena/attribution.ex new file mode 100644 index 00000000..3fa0a843 --- /dev/null +++ b/lib/philomena/attribution.ex @@ -0,0 +1,17 @@ +defprotocol Philomena.Attribution do + @doc """ + Provides the "parent object" identifier for this object. This is so + that anonymous posts under the same topic id can return the same hash + for the same user. + """ + @spec object_identifier(struct()) :: String.t() + def object_identifier(object) + + @doc """ + Provides the "best" user identifier for an object. Usually this will be + the user_id, but may also be the fingerprint or IP address if other + information is unavailable. + """ + @spec best_user_identifier(struct()) :: String.t() + def best_user_identifier(object) +end \ No newline at end of file diff --git a/lib/philomena/comments/attribution.ex b/lib/philomena/comments/attribution.ex new file mode 100644 index 00000000..07c97467 --- /dev/null +++ b/lib/philomena/comments/attribution.ex @@ -0,0 +1,9 @@ +defimpl Philomena.Attribution, for: Philomena.Comments.Comment do + def object_identifier(comment) do + to_string(comment.image_id || comment.id) + end + + def best_user_identifier(comment) do + to_string(comment.user_id || comment.fingerprint || comment.ip) + end +end \ No newline at end of file diff --git a/lib/philomena/images/attribution.ex b/lib/philomena/images/attribution.ex new file mode 100644 index 00000000..dd161b5b --- /dev/null +++ b/lib/philomena/images/attribution.ex @@ -0,0 +1,9 @@ +defimpl Philomena.Attribution, for: Philomena.Images.Image do + def object_identifier(image) do + to_string(image.id) + end + + def best_user_identifier(image) do + to_string(image.user_id || image.fingerprint || image.ip) + end +end \ No newline at end of file diff --git a/lib/philomena/posts/attribution.ex b/lib/philomena/posts/attribution.ex new file mode 100644 index 00000000..8ac81c1a --- /dev/null +++ b/lib/philomena/posts/attribution.ex @@ -0,0 +1,9 @@ +defimpl Philomena.Attribution, for: Philomena.Posts.Post do + def object_identifier(post) do + to_string(post.topic_id || post.id) + end + + def best_user_identifier(post) do + to_string(post.user_id || post.fingerprint || post.ip) + end +end \ No newline at end of file diff --git a/lib/philomena_web/templates/user_attribution/_anon_user.html.slime b/lib/philomena_web/templates/user_attribution/_anon_user.html.slime index e752214b..56521a19 100644 --- a/lib/philomena_web/templates/user_attribution/_anon_user.html.slime +++ b/lib/philomena_web/templates/user_attribution/_anon_user.html.slime @@ -2,4 +2,5 @@ strong<> = @object.user.name - else strong<> - ' Background Pony \ No newline at end of file + | Background Pony # + = anonymous_name(@object) \ No newline at end of file diff --git a/lib/philomena_web/views/user_attribution_view.ex b/lib/philomena_web/views/user_attribution_view.ex index 945aa164..45aea15e 100644 --- a/lib/philomena_web/views/user_attribution_view.ex +++ b/lib/philomena_web/views/user_attribution_view.ex @@ -1,3 +1,19 @@ defmodule PhilomenaWeb.UserAttributionView do + alias Philomena.Attribution + use Bitwise use PhilomenaWeb, :view + + def anonymous_name(object) do + salt = anonymous_name_salt() + id = Attribution.object_identifier(object) + user_id = Attribution.best_user_identifier(object) + + (:erlang.crc32(salt <> id <> user_id) &&& 0xffff) + |> Integer.to_string(16) + end + + defp anonymous_name_salt do + Application.get_env(:philomena, :anonymous_name_salt) + |> to_string() + end end