Packages
electric_client
0.2.4
0.10.3
0.10.2
0.10.1
0.10.1-beta-1
0.10.0
0.9.5-beta-1
0.9.4
0.9.4-beta-1
0.9.3
0.9.2
0.9.1
0.9.0
0.8.3
0.8.3-beta-1
0.8.2
0.8.1
0.8.0
0.8.0-beta-1
0.7.3
0.7.2
0.7.1
0.7.0
0.6.5
0.6.5-beta-5
0.6.5-beta-4
0.6.5-beta-3
0.6.5-beta-2
0.6.5-beta-1
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.0
0.5.0-beta-1
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.3.0-beta.4
0.3.0-beta.3
0.3.0-beta.2
0.2.6-pre-1
retired
0.2.6-beta.1
0.2.6-beta.0
0.2.5
0.2.4
0.2.4-pre-8
0.2.4-pre-7
0.2.4-pre-6
0.2.4-pre-5
0.2.4-pre-4
0.2.4-pre-3
0.2.4-pre-2
0.2.4-pre-1
0.2.3
0.2.3-rc-1
0.2.2
0.2.2-rc-1
0.2.1
0.2.1-rc-3
0.2.1-rc-2
0.2.1-rc-1
0.2.0
0.1.2
0.1.1
0.1.0
0.1.0-dev-9
0.1.0-dev-8
0.1.0-dev-7
0.1.0-dev-6
0.1.0-dev-5
0.1.0-dev-4
0.1.0-dev-3
0.1.0-dev-2
0.1.0-dev-17
0.1.0-dev-16
0.1.0-dev-15
0.1.0-dev-14
0.1.0-dev-13
0.1.0-dev-12
0.1.0-dev-11
0.1.0-dev-10
0.1.0-dev
Elixir client for ElectricSQL
Current section
Files
Jump to
Current section
Files
lib/electric/client/authenticator/mock_authenticator.ex
defmodule Electric.Client.Authenticator.MockAuthenticator do
@moduledoc """
A pseudo-authenticating `Electric.Client.Authenticator` implementation.
This generates fake authentication headers, useful for validating that the
`Authenticator` behaviour is being called correctly.
"""
@behaviour Electric.Client.Authenticator
@joiner "\n"
@header "electric-mock-auth"
def authenticate_request(request, config) do
put_request_params(request, shape_hash(request.shape, config))
end
def authenticate_shape(shape, config) do
shape
|> shape_hash(config)
|> auth_headers()
end
defp shape_hash(shape, config) do
auth_hash([:namespace, :table, :columns, :where], shape, config)
end
defp put_request_params(request, hash) do
Map.update!(%{request | authenticated: true}, :headers, &auth_headers(&1, hash))
end
defp auth_headers(base \\ %{}, hash) do
Map.put(base, @header, hash)
end
defp auth_hash(keys, struct, config) do
keys
|> Enum.map(&Map.fetch!(struct, &1))
|> Enum.map(&to_string/1)
|> Enum.concat(List.wrap(config[:salt]))
|> Enum.join(@joiner)
|> then(&:crypto.hash(:sha256, &1))
|> Base.encode16(case: :lower)
end
end