Fixed Session timeouts.

This commit is contained in:
Jonathan Bernard 2013-10-25 15:47:20 +00:00
parent e8bebb49fb
commit 839646b154
2 changed files with 10 additions and 32 deletions

View File

@ -4,16 +4,13 @@
-include("ts_db_records.hrl").
-include("yaws_api.hrl").
% 6 hours: sec min hr
-define(TTL, 60*60* 6).
new(Username) ->
DateTime = calendar:now_to_universal_time(erlang:now()),
Seconds = calendar:datetime_to_gregorian_seconds(DateTime),
Session = #ts_api_session{ username = Username },
Session = #ts_api_session{
username = Username,
expires = Seconds + 60*60*6
},
CookieVal = yaws_api:new_cookie_session(Session),
CookieVal = yaws_api:new_cookie_session(Session, ?TTL),
{CookieVal, Session}.
logout(CookieVal) ->
@ -23,36 +20,18 @@ get_session(YArg) ->
% get the cookie header
Cookie = (YArg#arg.headers)#headers.cookie,
% get the current server time
Now = calendar:now_to_universal_time(erlang:now()),
NowSeconds = calendar:datetime_to_gregorian_seconds(Now),
% look up the cookie in the session server
case yaws_api:find_cookie_val("ts_api_session", Cookie) of
% no cookie, not logged in
[] -> not_logged_in;
% found the cookie
CookieVal ->
% get the session data
case yaws_api:cookieval_to_opaque(CookieVal) of
{error, _} -> not_logged_in;
{ok, Session} ->
if
% if the cookie has expired
NowSeconds > Session#ts_api_session.expires ->
logout(CookieVal),
session_expired;
% cookie is fresh
true ->
% update the expiry time 6 hours: sec min hr
NewSession = Session#ts_api_session{expires = NowSeconds + 60*60* 6},
yaws_api:replace_cookie_session(CookieVal, NewSession),
% return cookie
NewSession
end
{error, _Err} -> session_expired;
{ok, Session} -> Session
end
end.

View File

@ -36,8 +36,7 @@
}).
-record(ts_api_session, {
username,
expires
username
}).
% extensible data for arbitrary entities