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 authored and dndx committed Oct 22, 2019
1 parent 88eca6e commit fb1060d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
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
7 changes: 7 additions & 0 deletions kong/plugins/key-auth/migrations/002_130_to_140.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ return {
-- Do nothing, accept existing state
END$$;
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 = {
Expand Down
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 @@ -119,6 +119,31 @@ for _, strategy in helpers.each_strategy() do
assert.equal("tag1", json.tags[1])
assert.equal("tag2", json.tags[2])
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 fb1060d

Please sign in to comment.