summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2020-04-21 02:50:49 -0400
committerHubert Chathi <hubert@uhoreg.ca>2020-04-21 02:50:49 -0400
commitfd9d9ed9b14f34fb9d7863f5c03e4942b16a7adc (patch)
treead7da10f745fa7e596fcd7237bbc56b22522faa5
parentallow listeners to be functions instead of processes (diff)
fix login endpoint add logout endpoint, and add functions for logging in/out
-rw-r--r--examples/login.exs5
-rw-r--r--examples/logout.exs6
-rw-r--r--lib/polyjuice/client.ex68
-rw-r--r--lib/polyjuice/client/endpoint/post_login.ex30
-rw-r--r--lib/polyjuice/client/endpoint/post_logout.ex59
-rw-r--r--test/polyjuice/client/endpoint/post_login_test.exs2
6 files changed, 154 insertions, 16 deletions
diff --git a/examples/login.exs b/examples/login.exs
new file mode 100644
index 0000000..1b3085d
--- /dev/null
+++ b/examples/login.exs
@@ -0,0 +1,5 @@
+Polyjuice.Client.log_in_with_password(
+ "http://localhost:8008",
+ "user",
+ "password"
+) |> inspect() |> IO.puts()
diff --git a/examples/logout.exs b/examples/logout.exs
new file mode 100644
index 0000000..af47d0b
--- /dev/null
+++ b/examples/logout.exs
@@ -0,0 +1,6 @@
+Polyjuice.Client.log_out(
+ %Polyjuice.Client{
+ base_url: "http://localhost:8008",
+ access_token: "accesstoken"
+ }
+) |> inspect() |> IO.puts()
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
diff --git a/lib/polyjuice/client/endpoint/post_login.ex b/lib/polyjuice/client/endpoint/post_login.ex
index 6d516c2..bb068e2 100644
--- a/lib/polyjuice/client/endpoint/post_login.ex
+++ b/lib/polyjuice/client/endpoint/post_login.ex
@@ -52,21 +52,20 @@ defmodule Polyjuice.Client.Endpoint.PostLogin do
base_url
)
when is_binary(base_url) do
- {:ok, body} =
- Poison.encode(
- Map.new(
- Enum.concat([
- [{"type", type}, {"identifier", identifier}],
- if(password != nil, do: [{"password", password}], else: []),
- if(token != nil, do: [{"token", token}], else: []),
- if(device_id != nil, do: [{"device_id", device_id}], else: []),
- if(initial_device_display_name != nil,
- do: [{"initial_device_display_name", initial_device_display_name}],
- else: []
- )
- ])
+ body =
+ [
+ [{"type", type}, {"identifier", identifier}],
+ if(password != nil, do: [{"password", password}], else: []),
+ if(token != nil, do: [{"token", token}], else: []),
+ if(device_id != nil, do: [{"device_id", device_id}], else: []),
+ if(initial_device_display_name != nil,
+ do: [{"initial_device_display_name", initial_device_display_name}],
+ else: []
)
- )
+ ]
+ |> Enum.concat()
+ |> Map.new()
+ |> Poison.encode!()
%Polyjuice.Client.Endpoint.HttpSpec{
method: :post,
@@ -81,7 +80,8 @@ defmodule Polyjuice.Client.Endpoint.PostLogin do
)
|> to_string(),
body: body,
- transform: &Polyjuice.Client.Endpoint.PostLogin.transform/3
+ transform: &Polyjuice.Client.Endpoint.PostLogin.transform/3,
+ auth_required: false
}
end
end
diff --git a/lib/polyjuice/client/endpoint/post_logout.ex b/lib/polyjuice/client/endpoint/post_logout.ex
new file mode 100644
index 0000000..d23f725
--- /dev/null
+++ b/lib/polyjuice/client/endpoint/post_logout.ex
@@ -0,0 +1,59 @@
+# Copyright 2020 Hubert Chathi <hubert@uhoreg.ca>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+defmodule Polyjuice.Client.Endpoint.PostLogout do
+ @moduledoc """
+ Logs out a session
+
+ https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout
+ """
+
+ @type t :: %__MODULE__{}
+
+ defstruct []
+
+ defimpl Polyjuice.Client.Endpoint.Proto do
+ def http_spec(
+ %Polyjuice.Client.Endpoint.PostLogout{},
+ base_url
+ )
+ when is_binary(base_url) do
+ %Polyjuice.Client.Endpoint.HttpSpec{
+ method: :post,
+ headers: [
+ {"Accept", "application/json"}
+ ],
+ url:
+ URI.merge(
+ base_url,
+ "#{Polyjuice.Client.prefix_r0()}/logout"
+ )
+ |> to_string(),
+ body: "",
+ transform: &Polyjuice.Client.Endpoint.PostLogout.transform/3
+ }
+ end
+ end
+
+ @doc false
+ def transform(status_code, _resp_headers, body) do
+ case status_code do
+ 200 ->
+ :ok
+
+ _ ->
+ {:error, status_code, body}
+ end
+ end
+end
diff --git a/test/polyjuice/client/endpoint/post_login_test.exs b/test/polyjuice/client/endpoint/post_login_test.exs
index b1dabb0..8d816a1 100644
--- a/test/polyjuice/client/endpoint/post_login_test.exs
+++ b/test/polyjuice/client/endpoint/post_login_test.exs
@@ -28,7 +28,7 @@ defmodule Polyjuice.Client.Endpoint.PostLoginTest do
http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, "https://example.com")
assert %{http_spec | transform: nil, body: nil} == %Polyjuice.Client.Endpoint.HttpSpec{
- auth_required: true,
+ auth_required: false,
body: nil,
headers: [
{"Accept", "application/json"},