-
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: rename Databag to SessionData and update…
… related callbacks in configuration and documentation
- Loading branch information
Showing
7 changed files
with
67 additions
and
96 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
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
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
File renamed without changes.
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,96 +1,59 @@ | ||
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 | ||
|
||
module Session | ||
class CookieStore(T) < Store(T) | ||
property cookies = HTTP::Cookies.new | ||
|
||
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[data_key] | ||
payload = String.new(verify_and_decrypt(data.value)) | ||
SessionId(T).from_json payload | ||
else | ||
raise InvalidSessionExeception.new | ||
end | ||
end | ||
|
||
def []?(key : String) : SessionId(T)? | ||
if data = cookies[data_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 << HTTP::Cookie.new( | ||
name: data_key, | ||
value: encrypt_and_sign(session.to_json), | ||
expires: timeout.from_now, | ||
secure: true, | ||
http_only: true, | ||
creation_time: Time.local, | ||
) | ||
session | ||
end | ||
def storage : String | ||
self.class.name | ||
end | ||
|
||
def delete(key : String) | ||
cookies.delete(data_key) | ||
end | ||
def [](key : String) : SessionId(T) | ||
data = cookies[prefixed(cookie_name + key)] || raise InvalidSessionExeception.new | ||
deserialize_session(data.value) | ||
end | ||
|
||
def size : Int64 | ||
name = data_key | ||
cookies.reduce(0_i64) do |acc, cookie| | ||
acc + 1 if cookie.name.starts_width? name | ||
end | ||
def [](key : String) : SessionId(T) | ||
if data = cookies[data_key] | ||
payload = String.new(verify_and_decrypt(data.value)) | ||
SessionId(T).from_json payload | ||
else | ||
raise InvalidSessionExeception.new | ||
end | ||
end | ||
|
||
def clear | ||
cookies.each do |cookie| | ||
cookies.delete cookie.name if cookie.name.starts_width? name | ||
end | ||
def []?(key : String) : SessionId(T)? | ||
if data = cookies[data_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) | ||
cookies << HTTP::Cookie.new( | ||
name: data_key, | ||
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)) | ||
cookies.delete(data_key) | ||
end | ||
|
||
def size : Int64 | ||
count_cookies(prefixed(cookie_name)) | ||
name = data_key | ||
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 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) } | ||
cookies.each do |cookie| | ||
cookies.delete cookie.name if cookie.name.starts_width? name | ||
end | ||
end | ||
end | ||
end |