Skip to content

Commit

Permalink
fix(session) adjust params of :update for new dao
Browse files Browse the repository at this point in the history
From #6
  • Loading branch information
darrenjennings authored Apr 1, 2019
2 parents 558bb6e + 026eecf commit c040532
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
9 changes: 4 additions & 5 deletions kong/plugins/session/storage/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function kong_storage:open(cookie, lifetime)
expires = db_s.expires
end
end

return id, expires, data, hmac
end

Expand All @@ -89,15 +89,15 @@ function kong_storage:insert_session(sid, data, expires)
data = data,
expires = expires,
}, { ttl = self.lifetime })

if err then
ngx.log(ngx.ERR, "Error inserting session: ", err)
end
end


function kong_storage:update_session(id, params, ttl)
local _, err = self.db.sessions:update(params, { id = id }, { ttl = ttl })
local _, err = self.db.sessions:update({ id = id }, params, { ttl = ttl })
if err then
ngx.log(ngx.ERR, "Error updating session: ", err)
end
Expand All @@ -109,7 +109,6 @@ function kong_storage:save(id, expires, data, hmac)
local value = concat({key, expires, self.encode(hmac)}, self.delimiter)

if life > 0 then

if ngx.get_phase() == 'header_filter' then
ngx.timer.at(0, function()
self:insert_session(key, self.encode(data), expires)
Expand All @@ -131,7 +130,7 @@ function kong_storage:destroy(id)
if not db_s then
return
end

local _, err = self.db.sessions:delete({
id = db_s.id
})
Expand Down
30 changes: 15 additions & 15 deletions spec/02-kong_storage_adapter_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local helpers = require "spec.helpers"
local utils = require "kong.tools.utils"


function get_sid_from_cookie(cookie)
local function get_sid_from_cookie(cookie)
local cookie_parts = utils.split(cookie, "; ")
return utils.split(utils.split(cookie_parts[1], "|")[1], "=")[2]
end
Expand Down Expand Up @@ -31,7 +31,7 @@ for _, strategy in helpers.each_strategy() do
paths = {"/test2"},
hosts = {"httpbin.org"}
}

assert(bp.plugins:insert {
name = "session",
route = {
Expand All @@ -42,7 +42,7 @@ for _, strategy in helpers.each_strategy() do
secret = "ultra top secret session",
}
})

assert(bp.plugins:insert {
name = "session",
route = {
Expand Down Expand Up @@ -113,10 +113,10 @@ for _, strategy in helpers.each_strategy() do

after_each(function()
if client then client:close() end
end)
end)

describe("kong adapter - ", function()
it("kong adapter stores consumer", function()
it("kong adapter stores consumer", function()
local res, cookie
local request = {
method = "GET",
Expand All @@ -127,18 +127,18 @@ for _, strategy in helpers.each_strategy() do
-- make sure the anonymous consumer can't get in (request termination)
res = assert(client:send(request))
assert.response(res).has.status(403)

-- make a request with a valid key, grab the cookie for later
request.headers.apikey = "kong"
res = assert(client:send(request))
assert.response(res).has.status(200)
cookie = assert.response(res).has.header("Set-Cookie")

ngx.sleep(2)

-- use the cookie without the key to ensure cookie still lets them in
request.headers.apikey = nil
request.headers.cookie = cookie
request.headers.cookie = cookie
res = assert(client:send(request))
assert.response(res).has.status(200)

Expand All @@ -151,7 +151,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(sid, db.sessions:select_by_session_id(sid).session_id)
end)

it("renews cookie", function()
it("renews cookie", function()
local res, cookie
local request = {
method = "GET",
Expand All @@ -160,9 +160,9 @@ for _, strategy in helpers.each_strategy() do
}

local function send_requests(request, number, step)
local cookie = request.headers.cookie
cookie = request.headers.cookie

for i = 1, number do
for _ = 1, number do
request.headers.cookie = cookie
res = assert(client:send(request))
assert.response(res).has.status(200)
Expand All @@ -174,7 +174,7 @@ for _, strategy in helpers.each_strategy() do
-- make sure the anonymous consumer can't get in (request termination)
res = assert(client:send(request))
assert.response(res).has.status(403)

-- make a request with a valid key, grab the cookie for later
request.headers.apikey = "kong"
res = assert(client:send(request))
Expand All @@ -188,13 +188,13 @@ for _, strategy in helpers.each_strategy() do
request.headers.cookie = cookie
res = assert(client:send(request))
assert.response(res).has.status(200)

-- renewal period, make sure requests still come through and
-- if set-cookie header comes through, attach it to subsequent requests
send_requests(request, 5, 0.5)
end)

it("destroys session on logout", function()
it("destroys session on logout", function()
local res, cookie
local request = {
method = "GET",
Expand All @@ -205,7 +205,7 @@ for _, strategy in helpers.each_strategy() do
-- make sure the anonymous consumer can't get in (request termination)
res = assert(client:send(request))
assert.response(res).has.status(403)

-- make a request with a valid key, grab the cookie for later
request.headers.apikey = "kong"
res = assert(client:send(request))
Expand Down

0 comments on commit c040532

Please sign in to comment.