-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsr_sessions.erl
164 lines (140 loc) · 4.65 KB
/
sr_sessions.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
%%% @doc Sessions Model
-module(sr_sessions).
-behaviour(sumo_doc).
-behaviour(sumo_rest_doc).
-type id() :: binary().
-type token() :: binary().
-type user() :: binary().
-type agent() :: binary().
%% Change type per opaque when https://bugs.erlang.org/browse/ERL-249
-type session() ::
#{ id => undefined | id()
, token => token()
, agent => undefined | agent()
, user => user()
, created_at => calendar:datetime()
, expires_at => calendar:datetime()
}.
-export_type(
[ session/0
, id/0
, token/0
, agent/0
]).
-export(
[ sumo_schema/0
, sumo_wakeup/1
, sumo_sleep/1
]).
-export(
[ new/1
, unique_id/1
, token/1
, user/1
, user/2
, expires_at/1
]).
-export(
[ to_json/1
, from_ctx/1
, from_json/2
, location/2
, update/2
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% BEHAVIOUR CALLBACKS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec sumo_schema() -> sumo:schema().
sumo_schema() ->
sumo:new_schema(sessions,
[ sumo:new_field(id, string, [id, not_null])
, sumo:new_field(token, binary, [not_null])
, sumo:new_field(agent, binary, [])
, sumo:new_field(user, binary, [not_null])
, sumo:new_field(created_at, datetime, [not_null])
, sumo:new_field(expires_at, datetime, [not_null])
]).
-spec sumo_sleep(session()) -> sumo:model().
sumo_sleep(Session) -> Session.
-spec sumo_wakeup(sumo:model()) -> session().
sumo_wakeup(Session) -> Session.
-spec to_json(session()) -> sr_json:json().
to_json(Session) ->
#{ id => sr_json:encode_null(maps:get(id, Session))
, token => maps:get(token, Session)
, agent => sr_json:encode_null(maps:get(agent, Session))
, created_at => sr_json:encode_date(maps:get(created_at, Session))
, expires_at => sr_json:encode_date(maps:get(expires_at, Session))
}.
-spec from_json(id(), sumo_rest_doc:json()) ->
{ok, session()} | {error, iodata()}.
from_json(Id, Json) -> from_json_internal(Json#{<<"id">> => Id}).
-spec from_json_internal(sumo_rest_doc:json()) ->
{ok, session()} | {error, iodata()}.
from_json_internal(Json) ->
Now = sr_json:encode_date(calendar:universal_time()),
ExpiresAt = sr_json:encode_date(expires_at()),
try
{ ok
, #{ id => sr_json:decode_null(maps:get(<<"id">>, Json, null))
, token => maps:get(<<"token">>, Json, generate_token())
, agent => sr_json:decode_null(maps:get(<<"agent">>, Json, null))
, created_at =>
sr_json:decode_date(maps:get(<<"created_at">>, Json, Now))
, expires_at =>
sr_json:decode_date(maps:get(<<"expires_at">>, Json, ExpiresAt))
}
}
catch
_:{badkey, Key} ->
{error, <<"missing field: ", Key/binary>>}
end.
-spec from_ctx(sumo_rest_doc:context()) -> {ok, session()} | {error, iodata()}.
from_ctx(#{req := SrRequest, state := State}) ->
Json = sr_request:body(SrRequest),
{User, _} = sr_state:retrieve(user, State, undefined),
case from_json_internal(Json) of
{ok, Session} -> {ok, user(Session, User)};
MissingField -> MissingField
end.
-spec update(session(), sumo_rest_doc:json()) ->
{ok, session()} | {error, iodata()}.
update(Session, Json) ->
#{id := SessionId} = Session,
case from_json(SessionId, Json) of
{error, Reason} -> {error, Reason};
{ok, Updates} ->
UpdatedSession = maps:merge(Session, Updates),
{ok, UpdatedSession#{ expires_at => expires_at()
, token => generate_token()
}}
end.
-spec location(session(), sumo_rest_doc:path()) -> binary().
location(Session, Path) -> iolist_to_binary([Path, "/", unique_id(Session)]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PUBLIC API
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec new(user()) -> session().
new(User) ->
Now = calendar:universal_time(),
#{ id => undefined
, user => User
, agent => undefined
, token => generate_token()
, created_at => Now
, expires_at => expires_at()
}.
unique_id(#{id := Id}) -> Id.
-spec token(session()) -> token().
token(#{token := Token}) -> Token.
-spec user(session()) -> user().
user(#{user := User}) -> User.
-spec user(session(), user()) -> session().
user(Session, User) -> Session#{user => User}.
-spec expires_at(session()) -> calendar:datetime().
expires_at(#{expires_at := ExpiresAt}) -> ExpiresAt.
generate_token() -> base64:encode(crypto:strong_rand_bytes(32)).
expires_at() ->
calendar:gregorian_seconds_to_datetime(
calendar:datetime_to_gregorian_seconds(
calendar:universal_time()) + 60000).