summaryrefslogtreecommitdiff
path: root/lib/polyjuice/client.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/polyjuice/client.ex')
-rw-r--r--lib/polyjuice/client.ex68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex
index 93b672b..7de93b1 100644
--- a/lib/polyjuice/client.ex
+++ b/lib/polyjuice/client.ex
@@ -154,4 +154,72 @@ defmodule Polyjuice.Client do
Polyjuice.Client.Sync.child_spec([client, listener | opts])
end
end
+
+ @doc """
+ Log in with a password.
+ """
+ @spec log_in_with_password(
+ client_or_url :: Polyjuice.Client.API.t() | String.t(),
+ identifier :: String.t() | tuple() | map(),
+ password :: String.t(),
+ opts :: list()
+ ) :: Any
+ def log_in_with_password(client_or_url, identifier, password, opts \\ []) do
+ client_api =
+ if is_binary(client_or_url) do
+ %Polyjuice.Client{
+ base_url: client_or_url
+ }
+ else
+ client_or_url
+ end
+
+ id =
+ case identifier do
+ x when is_binary(x) ->
+ %{
+ "type" => "m.id.user",
+ "user" => identifier
+ }
+
+ {:email, address} ->
+ %{
+ "type" => "m.id.thirdparty",
+ "medium" => "email",
+ "address" => address
+ }
+
+ {:phone, country, phone} ->
+ %{
+ "type" => "m.id.phone",
+ "country" => country,
+ "phone" => phone
+ }
+
+ x when is_map(x) ->
+ identifier
+ end
+
+ Polyjuice.Client.API.call(
+ client_api,
+ %Polyjuice.Client.Endpoint.PostLogin{
+ type: "m.login.password",
+ identifier: id,
+ password: password,
+ device_id: Keyword.get(opts, :device_id),
+ initial_device_display_name: Keyword.get(opts, :initial_device_display_name)
+ }
+ )
+ end
+
+ @doc """
+ Log out an existing session.
+ """
+ @spec log_out(client_api :: Polyjuice.Client.API.t()) :: Any
+ def log_out(client_api) do
+ Polyjuice.Client.API.call(
+ client_api,
+ %Polyjuice.Client.Endpoint.PostLogout{}
+ )
+ end
end