Skip to content

Commit

Permalink
feat(key-auth) support auto-expiring keys
Browse files Browse the repository at this point in the history
Fixes #87.
  • Loading branch information
p0pr0ck5 committed Feb 6, 2019
1 parent e7e2fa3 commit e17ff93
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions kong-1.0.3-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ build = {
["kong.plugins.key-auth.migrations"] = "kong/plugins/key-auth/migrations/init.lua",
["kong.plugins.key-auth.migrations.000_base_key_auth"] = "kong/plugins/key-auth/migrations/000_base_key_auth.lua",
["kong.plugins.key-auth.migrations.001_14_to_15"] = "kong/plugins/key-auth/migrations/001_14_to_15.lua",
["kong.plugins.key-auth.migrations.002_100_to_110"] = "kong/plugins/key-auth/migrations/002_100_to_110.lua",
["kong.plugins.key-auth.handler"] = "kong/plugins/key-auth/handler.lua",
["kong.plugins.key-auth.schema"] = "kong/plugins/key-auth/schema.lua",
["kong.plugins.key-auth.api"] = "kong/plugins/key-auth/api.lua",
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/key-auth/daos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local typedefs = require "kong.db.schema.typedefs"

return {
keyauth_credentials = {
ttl = true,
primary_key = { "id" },
name = "keyauth_credentials",
endpoint_key = "key",
Expand Down
15 changes: 15 additions & 0 deletions kong/plugins/key-auth/migrations/002_100_to_110.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
return {
postgres = {
up = [[
DO $$
BEGIN
ALTER TABLE IF EXISTS ONLY "keyauth_credentials" ADD "ttl" TIMESTAMP WITH TIME ZONE;
EXCEPTION WHEN DUPLICATE_COLUMN THEN
-- Do nothing, accept existing state
END$$;
]],
},
cassandra = {
up = [[]],-- nop
},
}
1 change: 1 addition & 0 deletions kong/plugins/key-auth/migrations/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
return {
"000_base_key_auth",
"001_14_to_15",
"002_100_to_110",
}
25 changes: 25 additions & 0 deletions spec/03-plugins/09-key-auth/01-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ for _, strategy in helpers.each_strategy() do

assert.not_equal(first_key, json.key)
end)
it("creates a key-auth credential with a ttl", function()
local res = assert(admin_client:send {
method = "POST",
path = "/consumers/bob/key-auth",
body = {
ttl = 1,
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = assert.res_status(201, res)
local json = cjson.decode(body)
assert.equal(consumer.id, json.consumer.id)
assert.is_string(json.key)

ngx.sleep(3)

local id = json.consumer.id
local res = assert(admin_client:send {
method = "GET",
path = "/consumers/bob/key-auth/" .. id,
})
assert.res_status(404, res)
end)
end)

describe("GET", function()
Expand Down

0 comments on commit e17ff93

Please sign in to comment.