Fixed Session timeouts.
This commit is contained in:
parent
e8bebb49fb
commit
839646b154
@ -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.
|
||||
|
@ -36,8 +36,7 @@
|
||||
}).
|
||||
|
||||
-record(ts_api_session, {
|
||||
username,
|
||||
expires
|
||||
username
|
||||
}).
|
||||
|
||||
% extensible data for arbitrary entities
|
||||
|
Loading…
x
Reference in New Issue
Block a user