ExWapp.PrivacyToken (ExWapp v0.1.2)

Copy Markdown View Source

Validates trusted-contact privacy tokens used by direct-message sends.

WhatsApp rotates these tokens in seven-day buckets and accepts the current bucket plus the previous three. Tokens outside that window must not be attached to outbound stanzas.

Summary

Types

tctoken_record()

@type tctoken_record() :: %{
  :token => binary(),
  :timestamp => non_neg_integer() | String.t(),
  optional(:sender_timestamp) => non_neg_integer() | String.t() | nil
}

Functions

cutoff_timestamp(now_seconds)

@spec cutoff_timestamp(integer()) :: integer()

newer_or_equal?(candidate, existing)

@spec newer_or_equal?(term(), term()) :: boolean()

normalize_timestamp(timestamp)

@spec normalize_timestamp(term()) :: {:ok, non_neg_integer()} | :error

put_newer(tokens, jids, record)

@spec put_newer(map(), [binary()], map()) :: map()

Stores record under every JID in jids, keeping whichever token is newer.

One contact reaches the store under several JID aliases (phone number and LID), and the same token arrives through more than one path — a notification and a history sync chunk. Overwriting blindly would let a stale copy win.

valid?(record, now_seconds \\ System.system_time(:second))

@spec valid?(term(), integer()) :: boolean()