-
Notifications
You must be signed in to change notification settings - Fork 461
/
crossbar_session.erl
334 lines (288 loc) · 12.2 KB
/
crossbar_session.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
%%%-------------------------------------------------------------------
%%% @author James Aimonetti <james@2600hz.org>
%%% @copyright (C) 2010, James Aimonetti
%%% @doc
%%% Session server
%%% @end
%%% Created : 9 Dec 2010 by James Aimonetti <james@2600hz.org>
%%%-------------------------------------------------------------------
-module(crossbar_session).
-behaviour(gen_server).
%% API
-export([start_link/0, is_authorized/1, start_session/1, finish_session/2, end_session/2]).
%% Access functions to the session storage
-export([incr/2, incr/3, decr/2, decr/3, store/3, retrieve/2, retrieve/3, delete/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-import(logger, [format_log/3]).
-define(SERVER, ?MODULE).
-define(MAX_AGE, 1800). % 30 minutes
-define(VIEW_FILE, "views/sessions.json").
-define(COOKIE_NAME, "crossbar_session").
-define(SESSION_DB, "crossbar%2Fsessions").
-define(SESSION_EXPIRED, {"session", "expired_time"}).
-include_lib("webmachine/include/webmachine.hrl").
-include("../include/crossbar.hrl").
%%%===================================================================
%%% API
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%% Begin or resume a session, returning the session record
-spec(start_session/1 :: (AuthToken :: undefined | binary()) -> #session{}).
start_session(AuthToken) ->
gen_server:call(?MODULE, {start_session, AuthToken}, infinity).
%% return the modified request datastructure with the updated session
-spec(finish_session/2 :: (S :: #session{}, RD :: #wm_reqdata{}) -> #wm_reqdata{}).
finish_session(#session{}=S, #wm_reqdata{}=RD) ->
gen_server:call(?MODULE, {finish_session, S, RD}, infinity).
%% close the session and delete the cookie from the request datastructure
-spec(end_session/2 :: (S :: #session{}, RD :: #wm_reqdata{}) -> #wm_reqdata{}).
end_session(#session{}=S, #wm_reqdata{}=RD) ->
gen_server:call(?MODULE, {stop_session, S, RD}, infinity).
-spec(is_authorized/1 :: (S :: #session{}) -> boolean()).
is_authorized(#session{}=S) ->
gen_server:call(?MODULE, {is_authorized, S}, infinity).
%% increment the field's value
-spec(incr/2 :: (S :: #session{}, K :: term()) -> #session{}).
incr(#session{}=S, K) -> incr(S, K, 0).
-spec(incr/3 :: (S :: #session{}, K :: term(), D :: integer()) -> #session{}).
incr(#session{storage=Storage}=S, K, D) ->
case props:get_value(K, Storage) of
undefined -> store(S, K, D);
V -> store(S, K, V+1)
end.
-spec(decr/2 :: (S :: #session{}, K :: term()) -> #session{}).
decr(#session{}=S, K) -> decr(S, K, 0).
-spec(decr/3 :: (S :: #session{}, K :: term(), D :: integer()) -> #session{}).
decr(#session{storage=Storage}=S, K, D) ->
case props:get_value(K, Storage) of
undefined -> store(S, K, D);
V -> store(S, K, V-1)
end.
%% store(#session(), key(), value()) -> #session().
store(#session{storage=Storage}=S, K, V) ->
S#session{storage=[{K, V} | lists:keydelete(K, 1, Storage)]}.
%% retrieve(#session(), key()) -> undefined | term()
retrieve(#session{}=S, K) -> retrieve(S, K, undefined).
%% retrieve(#session(), key()) -> D | term()
retrieve(#session{storage=Storage}, K, D) -> props:get_value(K, Storage, D).
%% delete(#session(), key()) -> #session().
delete(#session{storage=Storage}=S, K) ->
S#session{storage=lists:keydelete(K, 1, Storage)}.
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
couch_mgr:db_create(?SESSION_DB),
couch_mgr:load_doc_from_file(?SESSION_DB, crossbar, ?VIEW_FILE),
{ok, _} = timer:send_interval(60000, ?MODULE, clean_expired),
{ok, ok}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @spec handle_call(Request, From, State) ->
%% {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call({start_session, AuthToken}, _, State) ->
{reply, get_session(AuthToken), State};
%% return the modified request datastructure with the updated session
handle_call({finish_session, #session{}=S, #wm_reqdata{}=RD}, _, State) ->
{reply, save_session(S, RD), State};
%% close the session and delete the cookie from the request datastructure
handle_call({stop_session, #session{}=S, #wm_reqdata{}=RD}, _, State) ->
{reply, delete_session(S, RD), State};
handle_call({is_authorized, #session{account_id=AccountId, created=Created, expires=Expires}}, _, State) ->
{reply, AccountId =/= undefined andalso (not has_expired(Created + Expires)), State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info(clean_expired, State) ->
spawn(fun() -> clean_expired() end),
{noreply, State};
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
%% get_session_doc(request()) -> not_found | json_object()
-spec(get_session_doc/1 :: (AuthToken :: binary()) -> not_found | json_object()).
get_session_doc(AuthToken) ->
case couch_mgr:open_doc(?SESSION_DB, AuthToken) of
{error, _} -> not_found;
{ok, JObj} -> JObj
end.
%% get_session_rec(request()) -> session_rec()
-spec(get_session/1 :: (AuthToken :: binary()) -> undefined | #session{}).
get_session(<<>>) ->
undefined;
get_session(AuthToken) ->
case get_session_doc(AuthToken) of
not_found -> new(AuthToken);
JObj ->
#session{created=Created, expires=Expires}=S = from_doc(JObj),
case has_expired(Created+Expires) of
true -> new(AuthToken);
false -> S
end
end.
-spec(save_session/2 :: (S :: #session{}, RD :: #wm_reqdata{}) -> #wm_reqdata{}).
save_session(#session{created=Created, expires=Expires}=S, RD) ->
DateTime = calendar:universal_time(),
Now = calendar:datetime_to_gregorian_seconds(DateTime),
case has_expired(Created + Expires) of
true ->
delete_session(S, RD);
false ->
case couch_mgr:save_doc(?SESSION_DB, to_doc(S, Now)) of
{error, _Error} ->
delete_session(S, RD);
{ok, JObj} ->
set_cookie_header(RD, from_doc(JObj), DateTime, ?MAX_AGE)
end
end.
-spec(delete_session/2 :: (S :: #session{}, RD :: #wm_reqdata{}) -> #wm_reqdata{}).
delete_session(S, RD) ->
_ = crossbar_bindings:map(<<"session.delete">>, S),
couch_mgr:del_doc(?SESSION_DB, to_doc(S)),
set_cookie_header(RD, S, calendar:local_time(), -1).
-spec(to_doc/1 :: (S :: #session{}) -> json_object()).
to_doc(#session{created=Created}=S) -> to_doc(S, Created).
-spec(to_doc/2 :: (S :: #session{}, Now :: integer()) -> json_object()).
to_doc(#session{'_id'=Id, '_rev'=undefined, storage=Storage}=S, Now) ->
{struct, [{<<"_id">>, Id}
,{<<"session">>, tuple_to_list(S#session{created=whistle_util:to_integer(Now)
,expires=?MAX_AGE
,storage=encode_storage(Storage)
})}
]};
to_doc(#session{'_id'=Id, '_rev'=Rev, storage=Storage}=S, Now) ->
{struct, [{<<"_id">>, Id}
,{<<"_rev">>, Rev}
,{<<"session">>, tuple_to_list(S#session{created= whistle_util:to_integer(Now)
,expires=?MAX_AGE
,storage=encode_storage(Storage)
})}
]}.
-spec(encode_storage/1 :: (L :: proplist()) -> [] | json_object()).
encode_storage([]) -> [];
encode_storage(L) when is_list(L) -> {struct, L}.
-spec(from_doc/1 :: (Doc :: json_object()) -> #session{}).
from_doc({struct, Doc}) ->
Id = props:get_value(<<"_id">>, Doc),
Rev = props:get_value(<<"_rev">>, Doc),
#session{created=Created, expires=Expires}=S = setelement(1, list_to_tuple(props:get_value(<<"session">>, Doc)), session),
S#session{'_id'=Id, '_rev'=Rev
,created=whistle_util:to_integer(Created)
,expires=whistle_util:to_integer(Expires)
}.
%% create a new session record
%% new() -> #session()
-spec(new/1 :: (Id :: binary()) -> #session{}).
new(Id) ->
Now = current_seconds(),
#session{created=Now, expires=?MAX_AGE, '_id'=Id}.
%% set_cookie_header(request(), #session(), {date(), time()}, seconds()) -> request()
-spec(set_cookie_header/4 :: (RD :: #wm_reqdata{}, Session :: #session{}, DateTime :: tuple(), MaxAge :: integer()) -> #wm_reqdata{}).
set_cookie_header(RD, #session{'_id'=Id}, DateTime, MaxAge) ->
{CookieHeader, CookieValue} = mochiweb_cookies:cookie(?COOKIE_NAME, Id, [{max_age, MaxAge},
{local_time, DateTime}
]),
wrq:set_resp_header(CookieHeader, CookieValue, RD).
%% Maintenance Functions
%% Retrieve expired sessions and delete them from the DB
-spec(clean_expired/0 :: () -> no_return()).
clean_expired() ->
case couch_mgr:get_results(?SESSION_DB, ?SESSION_EXPIRED, [{"startkey", 0},
{"endkey", current_seconds()}
]) of
{error, _} -> ok;
{ok, Sessions} ->
Docs = [ D || D <- [ begin
case couch_mgr:open_doc(?SESSION_DB, props:get_value(<<"id">>, Prop)) of
{ok, Doc} -> Doc;
Else -> Else
end
end || {struct, Prop} <- Sessions ]
,clean_expired_(D)],
format_log(info, "CB_SESSION(~p): Cleaned ~p sessions~n", [self(), length(Docs)]),
lists:foreach(fun(D) -> couch_mgr:del_doc(?SESSION_DB, D) end, Docs)
end.
clean_expired_({struct, _}) -> true;
clean_expired_(_) -> false.
%% pass the secs representation of the expires time and compare to now
-spec(has_expired/1 :: (E :: integer()) -> boolean()).
has_expired(E) ->
E < current_seconds().
-spec(current_seconds/0 :: () -> integer()).
current_seconds() ->
whistle_util:current_tstamp().