summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2020-08-23 20:02:03 -0400
committerHubert Chathi <hubert@uhoreg.ca>2020-08-23 20:02:03 -0400
commitcc5153957888101d566e97013b7c1640574e835a (patch)
tree6cbec0c8113e3c0c3d26b1019c166826019ed8bb /lib
parentexecute room requests in a queue (diff)
remember access token, user ID, and device ID in storage
Diffstat (limited to 'lib')
-rw-r--r--lib/polyjuice/client.ex81
1 files changed, 76 insertions, 5 deletions
diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex
index 39cadf0..03ae518 100644
--- a/lib/polyjuice/client.ex
+++ b/lib/polyjuice/client.ex
@@ -87,13 +87,60 @@ defmodule Polyjuice.Client do
client_id = Agent.get_and_update(Polyjuice.Client.ID, fn id -> {id, id + 1} end)
- access_token = Keyword.get(opts, :access_token)
- user_id = Keyword.get(opts, :user_id)
- device_id = Keyword.get(opts, :device_id)
sync = Keyword.get(opts, :sync, true)
storage = Keyword.get(opts, :storage)
handler = Keyword.get(opts, :handler)
+ # get/store access token, user ID, and device ID from/to storage
+ # - if they're specified, use the specified values, and store them
+ # - otherwise, see if we have stored values, and use them if so
+ # - otherwise, use nil
+ access_token =
+ case Keyword.get(opts, :access_token) do
+ nil ->
+ if storage do
+ Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "access_token")
+ end
+
+ x ->
+ if storage do
+ Polyjuice.Client.Storage.kv_put(storage, "ca.uhoreg.polyjuice", "access_token", x)
+ end
+
+ x
+ end
+
+ user_id =
+ case Keyword.get(opts, :user_id) do
+ nil ->
+ if storage do
+ Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "user_id")
+ end
+
+ x ->
+ if storage do
+ Polyjuice.Client.Storage.kv_put(storage, "ca.uhoreg.polyjuice", "user_id", x)
+ end
+
+ x
+ end
+
+ device_id =
+ case Keyword.get(opts, :device_id) do
+ nil ->
+ if storage && user_id do
+ # it doesn't make sense to try to fetch a device ID if there's no user ID
+ Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "device_id")
+ end
+
+ x ->
+ if storage do
+ Polyjuice.Client.Storage.kv_put(storage, "ca.uhoreg.polyjuice", "device_id", x)
+ end
+
+ x
+ end
+
children = [
%{
id: Polyjuice.Client,
@@ -359,7 +406,7 @@ defmodule Polyjuice.Client do
password :: String.t(),
opts :: list()
) :: {:ok, map()} | any
- def log_in_with_password(client, identifier, password, opts \\ [])
+ def log_in_with_password(%Polyjuice.Client{} = client, identifier, password, opts \\ [])
when (is_binary(identifier) or is_tuple(identifier) or is_map(identifier)) and
is_binary(password) and is_list(opts) do
ret =
@@ -379,6 +426,24 @@ defmodule Polyjuice.Client do
%{state | access_token: access_token, user_id: user_id, device_id: device_id}
end)
+ if client.storage do
+ Polyjuice.Client.Storage.kv_put(
+ client.storage,
+ "ca.uhoreg.polyjuice",
+ "access_token",
+ access_token
+ )
+
+ Polyjuice.Client.Storage.kv_put(client.storage, "ca.uhoreg.polyjuice", "user_id", user_id)
+
+ Polyjuice.Client.Storage.kv_put(
+ client.storage,
+ "ca.uhoreg.polyjuice",
+ "device_id",
+ device_id
+ )
+ end
+
if client.handler do
Polyjuice.Client.Handler.handle(
client.handler,
@@ -403,7 +468,7 @@ defmodule Polyjuice.Client do
Log out an existing session.
"""
@spec log_out(client :: Polyjuice.Client.t()) :: {:ok} | any
- def log_out(client) do
+ def log_out(%Polyjuice.Client{} = client) do
supervisor_name = process_name(client.id, :supervisor)
Supervisor.terminate_child(supervisor_name, Polyjuice.Client.Sync)
Supervisor.delete_child(supervisor_name, Polyjuice.Client.Sync)
@@ -416,6 +481,12 @@ defmodule Polyjuice.Client do
Agent.cast(process_name(client.id, :state), fn state -> %{state | access_token: nil} end)
+ if client.storage do
+ Polyjuice.Client.Storage.kv_del(client.storage, "ca.uhoreg.polyjuice", "access_token")
+ Polyjuice.Client.Storage.kv_del(client.storage, "ca.uhoreg.polyjuice", "user_id")
+ Polyjuice.Client.Storage.kv_del(client.storage, "ca.uhoreg.polyjuice", "device_id")
+ end
+
if client.handler do
Polyjuice.Client.Handler.handle(client.handler, :logged_out)
end