-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor session management and cookie handling; streamline event not…
…ifications and improve session lifecycle methods
- Loading branch information
Showing
2 changed files
with
85 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,58 @@ | ||
require "redis" | ||
class CookieStore(T) < Store(T) | ||
property cookies = HTTP::Cookies.new | ||
getter cookie_name = "_data_" | ||
|
||
module Session | ||
class CookieStore(T) < Store(T) | ||
property cookies = HTTP::Cookies.new | ||
getter cookie_name = "_data_" | ||
def storage : String | ||
self.class.name | ||
end | ||
|
||
# Gets the session manager store type | ||
def storage : String | ||
self.class.name | ||
end | ||
def [](key : String) : SessionId(T) | ||
data = cookies[prefixed(cookie_name + key)] || raise InvalidSessionExeception.new | ||
deserialize_session(data.value) | ||
end | ||
|
||
def [](key : String) : SessionId(T) | ||
if data = cookies[prefixed(cookie_name + key)] | ||
payload = String.new(verify_and_decrypt(data.value)) | ||
SessionId(T).from_json payload | ||
else | ||
raise InvalidSessionExeception.new | ||
end | ||
def []?(key : String) : SessionId(T)? | ||
if data = cookies[prefixed(cookie_name + key)]? | ||
deserialize_session(data.value) | ||
end | ||
end | ||
|
||
def []?(key : String) : SessionId(T)? | ||
if data = cookies[prefixed(cookie_name + key)]? | ||
payload = String.new(verify_and_decrypt(data.value)) | ||
SessionId(T).from_json payload | ||
end | ||
end | ||
def []=(key : String, session : SessionId(T)) : SessionId(T) | ||
cookies << create_session_cookie(key, session) | ||
session | ||
end | ||
|
||
def []=(key : String, session : SessionId(T)) : SessionId(T) | ||
cookies << HTTP::Cookie.new( | ||
name: prefixed(cookie_name + session.session_id), | ||
value: encrypt_and_sign(session.to_json), | ||
expires: timeout.from_now, | ||
secure: true, | ||
http_only: true, | ||
creation_time: Time.local, | ||
) | ||
session | ||
end | ||
def delete(key : String) | ||
cookies.delete(prefixed(cookie_name + key)) | ||
end | ||
|
||
def delete(key : String) | ||
cookies.delete prefixed(cookie_name + key) | ||
end | ||
def size : Int64 | ||
count_cookies(prefixed(cookie_name)) | ||
end | ||
|
||
def size : Int64 | ||
name = prefixed(cookie_name) | ||
cookies.reduce(0_i64) do |acc, cookie| | ||
acc + 1 if cookie.name.starts_width? name | ||
end | ||
end | ||
def clear | ||
cookies.reject! { |cookie| cookie.name.starts_with?(prefixed(cookie_name)) } | ||
end | ||
|
||
def clear | ||
cookies.each do |cookie| | ||
cookies.delete cookie.name if cookie.name.starts_width? name | ||
end | ||
end | ||
private | ||
|
||
def create_session_cookie(key : String, session : SessionId(T)) | ||
HTTP::Cookie.new( | ||
name: prefixed(cookie_name + key), | ||
value: encrypt_and_sign(session.to_json), | ||
expires: timeout.from_now, | ||
secure: true, | ||
http_only: true, | ||
creation_time: Time.local | ||
) | ||
end | ||
|
||
def deserialize_session(value : String) : SessionId(T) | ||
SessionId(T).from_json(verify_and_decrypt(value)) | ||
end | ||
|
||
def count_cookies(name_prefix : String) : Int64 | ||
cookies.reduce(0_i64) { |acc, cookie| acc + 1 if cookie.name.starts_with?(name_prefix) } | ||
end | ||
end | ||
end |