class NaiveCookie

Public Class Methods

new(cookie_str) click to toggle source

Super naive cookie implementation It is not meant in any way to check the validity of the cookie It is only meant to check specific properties of cookies that are assumed to be present.

# File lib/naive_cookie.rb, line 6
def initialize(cookie_str)
    @data = cookie_str.split("; ").map{ |s|
        s.index('=') ? s.split('=') : [s, true]
    }.to_h
end

Public Instance Methods

error(text) click to toggle source
# File lib/naive_cookie.rb, line 38
def error(text)
    return "\t\t👺   #{text}"
end
expires() click to toggle source
# File lib/naive_cookie.rb, line 66
def expires
    @data[@data.keys.find{ |item| item.downcase == "expires"}]
end
http_only?() click to toggle source
# File lib/naive_cookie.rb, line 50
def http_only?
    !@data.keys.find{ |item| item.downcase == "httponly"}.nil?
end
name() click to toggle source
# File lib/naive_cookie.rb, line 62
def name
    @data.keys[0]
end
path() click to toggle source
# File lib/naive_cookie.rb, line 58
def path
    @data[@data.keys.find{ |item| item.downcase == "path"}]
end
same_site() click to toggle source
# File lib/naive_cookie.rb, line 54
def same_site
    @data[@data.keys.find{ |item| item.downcase == "samesite"}]
end
secure?() click to toggle source
# File lib/naive_cookie.rb, line 46
def secure?
    !@data.keys.find{ |item| item.downcase == "secure"}.nil?
end
to_s() click to toggle source
# File lib/naive_cookie.rb, line 42
def to_s
    "#{self.name}, #{self.path}"
end
validate!(rules) click to toggle source
# File lib/naive_cookie.rb, line 12
def validate!(rules)
    errors = []

    if rules.key?("Path") && self.path != rules["Path"]
        errors.push(error("Path #{self.path} not matching #{rules["Path"]}."))
    end

    if rules.key?("Secure") && self.secure? != rules["Secure"]
        errors.push(error("Cookie not secure."))
    end

    if rules.key?("HttpOnly") && self.http_only? != rules["HttpOnly"]
        errors.push(error("Cookie expected to be set as HttpOnly."))
    end

    if rules.key?("SameSite") && self.same_site != rules["SameSite"]
        errors.push(error("SameSite #{self.same_site} not matching #{rules["SameSite"]}."))
    end

    if errors.length > 0
        return errors
    else
        return nil
    end
end