Skip to content

Commit

Permalink
Crystal 0.33.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha authored Mar 11, 2020
1 parent 4ea84ee commit 29f65f0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ language: crystal
crystal:
- latest
- nightly
script:
- crystal spec
- crystal tool format --check
matrix:
allow_failures:
- crystal: nightly
Expand Down
19 changes: 10 additions & 9 deletions spec/base_spec.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require "./spec_helper"

# Config Options
#
Kemal::Session.config.engine = Kemal::Session::MemoryEngine.new
Kemal::Session.config.secret = "kemal_rocks"
SIGNED_SESSION = "#{SESSION_ID}--#{Kemal::Session.sign_value(SESSION_ID)}"

describe "Session" do
before_all do
Kemal::Session.config.engine = Kemal::Session::MemoryEngine.new
Kemal::Session.config.secret = "kemal_rocks"
end

describe ".start" do
it "returns a Session instance" do
typeof(Kemal::Session.new(create_context(SESSION_ID))).should eq Kemal::Session
Expand Down Expand Up @@ -275,18 +274,20 @@ describe "Session" do
end

describe "signed cookies" do
signed_session = "#{SESSION_ID}--#{Kemal::Session.sign_value(SESSION_ID)}"

it "should use the same session_id" do
context = create_context(SESSION_ID)
session = Kemal::Session.new(context)
context.response.cookies[Kemal::Session.config.cookie_name].value.should eq(SIGNED_SESSION)
context.response.cookies[Kemal::Session.config.cookie_name].value.should eq(signed_session)
end

it "should return a new session if signed token has been tampered" do
tampered_session = "123" + SIGNED_SESSION[3..-1]
tampered_session = "123" + signed_session[3..-1]
context = create_context(tampered_session)
session = Kemal::Session.new(context)
name = Kemal::Session.config.cookie_name
context.response.cookies[name].value.should_not eq(SIGNED_SESSION)
context.response.cookies[name].value.should_not eq(signed_session)
context.response.cookies[name].value.should_not eq(tampered_session)
end

Expand Down
20 changes: 10 additions & 10 deletions spec/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ require "random/secure"
SESSION_DIR = File.join(Dir.tempdir, Random::Secure.hex) + "/"
Dir.mkdir(SESSION_DIR)

# Set the engine for all of these tests
#
Kemal::Session.config.secret = "super-awesome-secret"
Kemal::Session.config.engine = Kemal::Session::FileEngine.new({:sessions_dir => SESSION_DIR})

Spec.before_each do
if Kemal::Session.config.engine.class == Kemal::Session::FileEngine
Dir.mkdir(SESSION_DIR) unless Dir.exists?(SESSION_DIR)
Expand Down Expand Up @@ -40,6 +35,11 @@ def should_be_empty_file_session(session_id)
end

describe "Session::FileEngine" do
before_all do
Kemal::Session.config.secret = "super-awesome-secret"
Kemal::Session.config.engine = Kemal::Session::FileEngine.new({:sessions_dir => SESSION_DIR})
end

describe "options" do
describe ":sessions_dir" do
it "raises an ArgumentError if option not passed" do
Expand Down Expand Up @@ -220,12 +220,12 @@ describe "Session::FileEngine" do
get_file_session_contents(SESSION_ID).should \
eq("{\"ints\":{},\"bigints\":{},\"strings\":{},\"floats\":{},\"bools\":{},\"objects\":{\"user\":{\"type\":\"User\",\"object\":{\"id\":123,\"name\":\"charlie\"}}}}")

Kemal::Session.config.engine.as(Kemal::Session::FileEngine).clear_cache
Kemal::Session.config.engine.as(Kemal::Session::FileEngine).clear_cache

session = Kemal::Session.get(SESSION_ID).not_nil!
new_u = session.object("user").as(User)
new_u.id.should eq(123)
new_u.name.should eq("charlie")
session = Kemal::Session.get(SESSION_ID).not_nil!
new_u = session.object("user").as(User)
new_u.id.should eq(123)
new_u.name.should eq("charlie")
end
end

Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ require "file_utils"

SESSION_ID = Random::Secure.hex

Kemal::Session.config.engine = Kemal::Session::MemoryEngine.new
Kemal::Session.config.secret = "kemal_rocks"

def create_new_session
create_context(Random::Secure.hex)
end
Expand Down
2 changes: 1 addition & 1 deletion src/kemal-session/base.cr
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module Kemal
HTTP::Cookie.new(
name: Session.config.cookie_name,
value: self.encode(session_id),
expires: Time.now.to_utc + Session.config.timeout,
expires: Time.utc + Session.config.timeout,
http_only: true,
secure: Session.config.secure,
path: Session.config.path,
Expand Down
16 changes: 8 additions & 8 deletions src/kemal-session/engines/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ module Kemal
@sessions_dir = uninitialized String
@sessions_dir = options[:sessions_dir]
@cache = StorageInstance.new
@cached_session_read_time = Time.utc_now
@cached_session_read_time = Time.utc
@cached_session_id = ""
end

def run_gc
each_session do |session|
full_path = session_filename(session.id)
age = Time.utc_now - File.info(full_path).modification_time # mtime is always saved in utc
age = Time.utc - File.info(full_path).modification_time # mtime is always saved in utc
session.destroy if age.total_seconds > Session.config.timeout.total_seconds
end
end
Expand All @@ -85,10 +85,10 @@ module Kemal
end

def is_in_cache?(session_id : String) : Bool
if (@cached_session_read_time.to_unix / 60) < (Time.utc_now.to_unix / 60)
@cached_session_read_time = Time.utc_now
if (@cached_session_read_time.to_unix / 60) < (Time.utc.to_unix / 60)
@cached_session_read_time = Time.utc
begin
File.utime(Time.now, Time.now, session_filename(session_id))
File.utime(Time.local, Time.local, session_filename(session_id))
rescue ex
puts "Kemal-session cannot update time of a sesssion file. This may not be possible on your current file system"
end
Expand All @@ -110,7 +110,7 @@ module Kemal
end
end
instance = StorageInstance.new
@cached_session_read_time = Time.utc_now
@cached_session_read_time = Time.utc
File.write(session_filename(session_id), instance.to_json)
return instance
end
Expand All @@ -123,7 +123,7 @@ module Kemal
read_or_create_storage_instance(session_id)
end

def get_session(session_id : String)
def get_session(session_id : String) : Session?
return Session.new(session_id) if session_exists?(session_id)
nil
end
Expand All @@ -140,7 +140,7 @@ module Kemal
end
end

def all_sessions
def all_sessions : Array(Session)
array = [] of Session

each_session do |session|
Expand Down
32 changes: 16 additions & 16 deletions src/kemal-session/engines/memory.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ module Kemal

{% for name, type in vars %}
@{{name.id}}s = Hash(String, {{type}}).new
@last_access_at = Time.new.to_unix_ms
@last_access_at = Time.utc.to_unix_ms
getter {{name.id}}s

def {{name.id}}(k : String) : {{type}}
@last_access_at = Time.new.to_unix_ms
@last_access_at = Time.utc.to_unix_ms
return @{{name.id}}s[k]
end

def {{name.id}}?(k : String) : {{type}}?
@last_access_at = Time.new.to_unix_ms
@last_access_at = Time.utc.to_unix_ms
return @{{name.id}}s[k]?
end

def {{name.id}}(k : String, v : {{type}})
@last_access_at = Time.new.to_unix_ms
@last_access_at = Time.utc.to_unix_ms
@{{name.id}}s[k] = v
end

def delete_{{name.id}}(k : String)
@last_access_at = Time.new.to_unix_ms
@last_access_at = Time.utc.to_unix_ms
@{{name.id}}s.delete(k) if @{{name.id}}s.has_key?(k)
end
{% end %}
Expand All @@ -50,11 +50,11 @@ module Kemal
end

define_storage({
int: Int32,
int: Int32,
bigint: Int64,
string: String,
float: Float64,
bool: Bool,
string: String,
float: Float64,
bool: Bool,
object: Kemal::Session::StorableObject::StorableObjectContainer,
})
end
Expand All @@ -66,15 +66,15 @@ module Kemal
end

def run_gc
before = (Time.now - Kemal::Session.config.timeout.as(Time::Span)).to_unix_ms
before = (Time.local - Kemal::Session.config.timeout.as(Time::Span)).to_unix_ms
@store.delete_if do |id, entry|
last_access_at = Int64.from_json(entry, root: "last_access_at")
last_access_at < before
end
sleep Kemal::Session.config.gc_interval
end

def all_sessions
def all_sessions : Array(Session)
@store.each_with_object([] of Session) do |vals, arr|
arr << Kemal::Session.new(vals.first)
end
Expand All @@ -90,7 +90,7 @@ module Kemal
end
end

def get_session(session_id : String)
def get_session(session_id : String) : Session?
return nil if !@store.has_key?(session_id)
Session.new(session_id)
end
Expand Down Expand Up @@ -148,11 +148,11 @@ module Kemal
end

define_delegators({
int: Int32,
int: Int32,
bigint: Int64,
string: String,
float: Float64,
bool: Bool,
string: String,
float: Float64,
bool: Bool,
object: Session::StorableObject::StorableObjectContainer,
})
end
Expand Down

0 comments on commit 29f65f0

Please sign in to comment.