add anonyhash

This commit is contained in:
byte[] 2019-11-11 11:56:34 -05:00
parent e231451abd
commit f21eaa87bb
7 changed files with 63 additions and 1 deletions

View file

@ -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"),

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -2,4 +2,5 @@
strong<> = @object.user.name
- else
strong<>
' Background Pony
| Background Pony #
= anonymous_name(@object)

View file

@ -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