~umgeher/utsession

54e981a53746f801cc4c019a35b3ef08f74fe693 — Umgeher Torgersen 1 year, 5 months ago 5e9d7e0
refresh token added
5 files changed, 83 insertions(+), 23 deletions(-)

M Makefile
M src/utsession.erl
M src/utsession_struct.erl
M test/actor_test.erl
M test/struct_test.erl
M Makefile => Makefile +3 -1
@@ 5,7 5,9 @@ PROJECT_VERSION = 0.1.0
# Whitespace to be used when creating files from templates.
SP = 2

DEPS = qdate
DEPS = qdate utools

dep_utools = git https://git.sr.ht/~umgeher/utools 0.3

BUILD_DEPS = gitversion.mk
DEP_PLUGINS = gitversion.mk

M src/utsession.erl => src/utsession.erl +25 -2
@@ 14,6 14,9 @@
-export([start/1]).
-export([stop/1]).
-export([store/1]).
-export([rt/1]).
-export([rt_update/2]).
-export([ttl/1]).
-export([ttl_update/1]).
-export([ttl_validate/1]).



@@ 94,6 97,15 @@ stop(PID) ->
store(PID) ->
  gen_server:call(PID, store).

rt(PID) ->
  gen_server:call(PID, {rt, get}).

rt_update(PID, RT) ->
  gen_server:call(PID, {rt, update, RT}).

ttl(PID) ->
  gen_server:call(PID, {ttl, get}).

ttl_update(PID) ->
  gen_server:call(PID, {ttl, update}).



@@ 142,8 154,8 @@ handle_call({data, set, Key, Value}, _, S) ->
  {reply, {ok, set}, S#state{data = utsession_struct:kset(Key, Value, S#state.data)}};
handle_call({load, <<Bin/binary>>}, _, S) ->
  try erlang:binary_to_term(public_key:decrypt_private(base64:decode(Bin), S#state.private)) of
    {utsession, M, TTL} ->
      {reply, {ok, load}, S#state{data = {utsession, M, TTL}}};
    {utsession, M, RT, TTL} ->
      {reply, {ok, load}, S#state{data = {utsession, M, RT, TTL}}};
    E ->
      {reply, {error, baddata, E}, S}
  catch


@@ 164,6 176,17 @@ handle_call(store, _, S) ->
    C:E ->
      {reply, {error, {C, E}}, S}
  end;
handle_call({rt, get}, _, S) ->
  {reply, utsession_struct:rt(S#state.data), S};
handle_call({rt, update, RT}, _, S) ->
  case utsession_struct:rt_validate(RT, S#state.data) of
    true ->
      {reply, {ok, set}, S#state{data = utsession_struct:rt_update(RT, S#state.data)}};
    false ->
      {reply, {error, baddata}, S}
  end;
handle_call({ttl, get}, _, S) ->
  {reply, utsession_struct:ttl(S#state.data), S};
handle_call({ttl, update}, _, S) ->
  {reply, {ok, set}, S#state{data = utsession_struct:ttl_update(S#state.data)}};
handle_call({ttl, validate}, _, S) ->

M src/utsession_struct.erl => src/utsession_struct.erl +35 -15
@@ 5,39 5,59 @@
-export([kset/2, kset/3]).
-export([merge/2]).
-export([new/0]).
-export([rt/1]).
-export([rt_update/2]).
-export([rt_validate/2]).
-export([ttl/1]).
-export([ttl_update/1]).
-export([ttl_validate/1]).

kdel(Key, {utsession, M, TTL}) ->
  {utsession, maps:remove(Key, M), TTL}.
kdel(Key, {utsession, M, RT, TTL}) ->
  {utsession, maps:remove(Key, M), RT, TTL}.

kget({utsession, M, _}) ->
kget({utsession, M, _, _}) ->
  M.

kget(Key, {utsession, M, _}) ->
kget(Key, {utsession, M, _, _}) ->
  maps:get(Key, M, none).

kset(Map, {utsession, _, TTL}) ->
  {utsession, Map, TTL}.
kset(Map, {utsession, _, RT, TTL}) ->
  {utsession, Map, RT, TTL}.

kset(Key, Value, {utsession, M, TTL}) ->
  {utsession, maps:put(Key, Value, M), TTL}.
kset(Key, Value, {utsession, M, RT, TTL}) ->
  {utsession, maps:put(Key, Value, M), RT, TTL}.

merge(Map, {utsession, M, TTL}) ->
  {utsession, maps:merge(M, Map), TTL}.
merge(Map, {utsession, M, RT, TTL}) ->
  {utsession, maps:merge(M, Map), RT, TTL}.

new() ->
  {utsession, #{}, none}.
  {ok, RT} = utools:rand_hash(),
  {utsession, #{}, RT, none}.

ttl_update({utsession, M, _}) ->
  {utsession, M, qdate:add_days(1, qdate:unixtime())}.
rt({utsession, _, RT, _}) ->
  RT.

ttl_validate({utsession, _, TTL}) when is_integer(TTL) ->
rt_update(RT, {utsession, Data, RT, _}) ->
  {ok, RT_} = utools:rand_hash(),
  ttl_update({utsession, Data, RT_, none}).

rt_validate(RT, {utsession, _, RT, _}) ->
  true;
rt_validate(_, _) ->
  false.  

ttl({utsession, _, _, TTL}) ->
  TTL.

ttl_update({utsession, M, RT, _}) ->
  {utsession, M, RT, qdate:add_days(1, qdate:unixtime())}.

ttl_validate({utsession, _, _, TTL}) when is_integer(TTL) ->
  case qdate:unixtime() of
    Now when Now < TTL ->
      true;
    _ ->
      false
  end;
ttl_validate({utsession, _, _}) ->
ttl_validate({utsession, _, _, _}) ->
  false.

M test/actor_test.erl => test/actor_test.erl +1 -0
@@ 61,6 61,7 @@ big_test() ->
  ?assertEqual({ok, set}, utsession:ttl_update(PID)),
  ?assertEqual(true, utsession:ttl_validate(PID)),
  {ok, Data} = utsession:store(PID),
  ?assertNotEqual(none, utsession:rt(PID)),
  utsession:stop(PID),
  {ok, PID_} = utsession:start(Options),
  ?assertEqual({error, baddata}, utsession:load(PID_, base64:encode(<<"some">>))),

M test/struct_test.erl => test/struct_test.erl +19 -5
@@ 5,26 5,40 @@ qdate_start_test() ->
  qdate:start().

new_test() ->
  ?assertEqual({utsession, #{}, none}, utsession_struct:new()).
  ?assertMatch({utsession, #{}, _, none}, utsession_struct:new()).

k_e__test() ->
  N = utsession_struct:new(),
  ?assertEqual(none, utsession_struct:kget(<<"test">>, N)),
  N_ = utsession_struct:kset(<<"test">>, 123, N),
  ?assertEqual({utsession, #{<<"test">> => 123}, none}, N_),
  ?assertMatch({utsession, #{<<"test">> := 123}, _, none}, N_),
  ?assertEqual(123, utsession_struct:kget(<<"test">>, N_)),
  ?assertEqual({utsession, #{}, none}, utsession_struct:kdel(<<"test">>, N_)).
  ?assertMatch({utsession, #{}, _, none}, utsession_struct:kdel(<<"test">>, N_)).

map_test() ->
  M = #{<<"test">> => 123, <<"none">> => test},
  ?assertEqual({utsession, M, none}, utsession_struct:kset(M, utsession_struct:new())),
  ?assertMatch({utsession, M, _, none}, utsession_struct:kset(M, utsession_struct:new())),
  ?assertEqual(M, utsession_struct:kget(utsession_struct:kset(M, utsession_struct:kset(<<"1">>, 1, utsession_struct:new())))),
  ?assertEqual(maps:merge(#{<<"1">> => 1}, M), utsession_struct:kget(utsession_struct:merge(M, utsession_struct:kset(<<"1">>, 1, utsession_struct:new())))).

ttl_update_test() ->
  {utsession, _, TTL} = utsession_struct:ttl_update(utsession_struct:new()),
  S = utsession_struct:ttl_update(utsession_struct:new()),
  {utsession, _, _, TTL} = S,
  ?assertEqual(TTL, utsession_struct:ttl(S)),
  ?assertNotEqual(none, TTL).

ttl_validate_test() ->
  ?assertEqual(false, utsession_struct:ttl_validate(utsession_struct:new())),
  ?assertEqual(true, utsession_struct:ttl_validate(utsession_struct:ttl_update(utsession_struct:new()))).

rt_test() ->
  N = utsession_struct:new(),
  {utsession, _, RT, TTL_0} = N,
  ?assertEqual(RT, utsession_struct:rt(N)),
  ?assertEqual(false, utsession_struct:rt_validate(RT, utsession_struct:new())),
  ?assertEqual(true, utsession_struct:rt_validate(RT, N)),
  N_ = utsession_struct:rt_update(RT, N),
  {utsession, _, RT_0, TTL_1} = N_,
  ?assertNotEqual(TTL_0, TTL_1),
  ?assertNotEqual(RT, RT_0).