Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins) extended plugins API to query by specific relevant field #1570

Merged
merged 1 commit into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions kong/plugins/basic-auth/api.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local crud = require "kong.api.crud_helpers"
local utils = require "kong.tools.utils"

return {
["/consumers/:username_or_id/basic-auth/"] = {
Expand All @@ -19,15 +20,18 @@ return {
crud.post(self.params, dao_factory.basicauth_credentials)
end
},
["/consumers/:username_or_id/basic-auth/:id"] = {
["/consumers/:username_or_id/basic-auth/:credential_username_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_consumer_by_username_or_id(self, dao_factory, helpers)
self.params.consumer_id = self.consumer.id

local credentials, err = dao_factory.basicauth_credentials:find_all {
local filter_keys = {
[utils.is_valid_uuid(self.params.credential_username_or_id) and "id" or "username"] = self.params.credential_username_or_id,
consumer_id = self.params.consumer_id,
id = self.params.id
}
self.params.credential_username_or_id = nil

local credentials, err = dao_factory.basicauth_credentials:find_all(filter_keys)
if err then
return helpers.yield_error(err)
elseif next(credentials) == nil then
Expand Down
17 changes: 13 additions & 4 deletions kong/plugins/hmac-auth/api.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local crud = require "kong.api.crud_helpers"
local utils = require "kong.tools.utils"

return{
["/consumers/:username_or_id/hmac-auth/"] = {
Expand All @@ -20,18 +21,26 @@ return{
end
},

["/consumers/:username_or_id/hmac-auth/:id"] = {
["/consumers/:username_or_id/hmac-auth/:credential_username_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_consumer_by_username_or_id(self, dao_factory, helpers)
self.params.consumer_id = self.consumer.id

local err
self.hmacauth_credential, err = dao_factory.hmacauth_credentials:find(self.params)

local filter_keys = {
[utils.is_valid_uuid(self.params.credential_username_or_id) and "id" or "username"] = self.params.credential_username_or_id,
consumer_id = self.params.consumer_id,
}
self.params.credential_username_or_id = nil

local credentials, err = dao_factory.hmacauth_credentials:find_all(filter_keys)
if err then
return helpers.yield_error(err)
elseif self.hmacauth_credential == nil then
elseif next(credentials) == nil then
return helpers.responses.send_HTTP_NOT_FOUND()
end

self.hmacauth_credential = credentials[1]
end,

GET = function(self, dao_factory, helpers)
Expand Down
16 changes: 12 additions & 4 deletions kong/plugins/jwt/api.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local crud = require "kong.api.crud_helpers"
local utils = require "kong.tools.utils"

return {
["/consumers/:username_or_id/jwt/"] = {
Expand All @@ -20,18 +21,25 @@ return {
end
},

["/consumers/:username_or_id/jwt/:id"] = {
["/consumers/:username_or_id/jwt/:credential_key_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_consumer_by_username_or_id(self, dao_factory, helpers)
self.params.consumer_id = self.consumer.id

local err
self.jwt_secret, err = dao_factory.jwt_secrets:find(self.params)
local filter_keys = {
[utils.is_valid_uuid(self.params.credential_key_or_id) and "id" or "key"] = self.params.credential_key_or_id,
consumer_id = self.params.consumer_id,
}
self.params.credential_key_or_id = nil

local credentials, err = dao_factory.jwt_secrets:find_all(filter_keys)
if err then
return helpers.yield_error(err)
elseif self.jwt_secret == nil then
elseif next(credentials) == nil then
return helpers.responses.send_HTTP_NOT_FOUND()
end

self.jwt_secret = credentials[1]
end,

GET = function(self, dao_factory, helpers)
Expand Down
10 changes: 7 additions & 3 deletions kong/plugins/key-auth/api.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local crud = require "kong.api.crud_helpers"
local utils = require "kong.tools.utils"

return {
["/consumers/:username_or_id/key-auth/"] = {
Expand All @@ -19,15 +20,18 @@ return {
crud.post(self.params, dao_factory.keyauth_credentials)
end
},
["/consumers/:username_or_id/key-auth/:id"] = {
["/consumers/:username_or_id/key-auth/:credential_key_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_consumer_by_username_or_id(self, dao_factory, helpers)
self.params.consumer_id = self.consumer.id

local credentials, err = dao_factory.keyauth_credentials:find_all {
local filter_keys = {
[utils.is_valid_uuid(self.params.credential_key_or_id) and "id" or "key"] = self.params.credential_key_or_id,
consumer_id = self.params.consumer_id,
id = self.params.id
}
self.params.credential_key_or_id = nil

local credentials, err = dao_factory.keyauth_credentials:find_all(filter_keys)
if err then
return helpers.yield_error(err)
elseif next(credentials) == nil then
Expand Down
47 changes: 32 additions & 15 deletions kong/plugins/oauth2/api.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local crud = require "kong.api.crud_helpers"
local utils = require "kong.tools.utils"

return {
["/oauth2_tokens/"] = {
Expand All @@ -15,21 +16,34 @@ return {
end
},

["/oauth2_tokens/:id"] = {
GET = function(self, dao_factory)
crud.get(self.params, dao_factory.oauth2_tokens)
["/oauth2_tokens/:token_or_id"] = {
before = function(self, dao_factory, helpers)
local filter_keys = {
[utils.is_valid_uuid(self.params.token_or_id) and "id" or "access_token"] = self.params.token_or_id,
consumer_id = self.params.consumer_id,
}
self.params.token_or_id = nil

local credentials, err = dao_factory.oauth2_tokens:find_all(filter_keys)
if err then
return helpers.yield_error(err)
elseif next(credentials) == nil then
return helpers.responses.send_HTTP_NOT_FOUND()
end

self.oauth2_token = credentials[1]
end,

PATCH = function(self, dao_factory)
crud.patch(self.params, dao_factory.oauth2_tokens, self.params)
GET = function(self, dao_factory, helpers)
return helpers.responses.send_HTTP_OK(self.oauth2_token)
end,

PUT = function(self, dao_factory)
crud.put(self.params, dao_factory.oauth2_tokens)
PATCH = function(self, dao_factory)
crud.patch(self.params, dao_factory.oauth2_tokens, self.oauth2_token)
end,

DELETE = function(self, dao_factory)
crud.delete(self.params, dao_factory.oauth2_tokens)
crud.delete(self.oauth2_token, dao_factory.oauth2_tokens)
end
},

Expand Down Expand Up @@ -58,15 +72,18 @@ return {
end
},

["/consumers/:username_or_id/oauth2/:id"] = {
["/consumers/:username_or_id/oauth2/:clientid_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_consumer_by_username_or_id(self, dao_factory, helpers)
self.params.consumer_id = self.consumer.id

local credentials, err = dao_factory.oauth2_credentials:find_all {
local filter_keys = {
[utils.is_valid_uuid(self.params.clientid_or_id) and "id" or "client_id"] = self.params.clientid_or_id,
consumer_id = self.params.consumer_id,
id = self.params.id
}
self.params.clientid_or_id = nil

local credentials, err = dao_factory.oauth2_credentials:find_all(filter_keys)
if err then
return helpers.yield_error(err)
elseif next(credentials) == nil then
Expand All @@ -76,16 +93,16 @@ return {
self.oauth2_credential = credentials[1]
end,

GET = function(self, dao_factory)
crud.get(self.params, dao_factory.oauth2_credentials)
GET = function(self, dao_factory, helpers)
return helpers.responses.send_HTTP_OK(self.oauth2_credential)
end,

PATCH = function(self, dao_factory)
crud.patch(self.params, dao_factory.oauth2_credentials, self.params)
crud.patch(self.params, dao_factory.oauth2_credentials, self.oauth2_credential)
end,

DELETE = function(self, dao_factory)
crud.delete(self.params, dao_factory.oauth2_credentials)
crud.delete(self.oauth2_credential, dao_factory.oauth2_credentials)
end
}
}
32 changes: 29 additions & 3 deletions spec/03-plugins/01-basic-auth/02-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ describe("Plugin: basic-auth (API)", function()
local json = cjson.decode(body)
assert.equal(credential.id, json.id)
end)
it("retrieves basic-auth credential by username", function()
local res = assert(admin_client:send {
method = "GET",
path = "/consumers/bob/basic-auth/"..credential.username
})
local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.equal(credential.id, json.id)
end)
it("retrieves credential by id only if the credential belongs to the specified consumer", function()
assert(helpers.dao.consumers:insert {
username = "alice"
Expand All @@ -181,7 +190,7 @@ describe("Plugin: basic-auth (API)", function()
end)

describe("PATCH", function()
it("updates a credential", function()
it("updates a credential by id", function()
local previous_hash = credential.password

local res = assert(admin_client:send {
Expand All @@ -198,6 +207,23 @@ describe("Plugin: basic-auth (API)", function()
local json = cjson.decode(body)
assert.not_equal(previous_hash, json.password)
end)
it("updates a credential by username", function()
local previous_hash = credential.password

local res = assert(admin_client:send {
method = "PATCH",
path = "/consumers/bob/basic-auth/"..credential.username,
body = {
password = "upd4321"
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.not_equal(previous_hash, json.password)
end)
describe("errors", function()
it("handles invalid input", function()
local res = assert(admin_client:send {
Expand Down Expand Up @@ -225,12 +251,12 @@ describe("Plugin: basic-auth (API)", function()
assert.res_status(204, res)
end)
describe("errors", function()
it("returns 400 on invalid input", function()
it("returns 404 on missing username", function()
local res = assert(admin_client:send {
method = "DELETE",
path = "/consumers/bob/basic-auth/blah"
})
assert.res_status(400, res)
assert.res_status(404, res)
end)
it("returns 404 if not found", function()
local res = assert(admin_client:send {
Expand Down
28 changes: 26 additions & 2 deletions spec/03-plugins/02-key-auth/01-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ describe("Plugin: key-auth (API)", function()
local json = cjson.decode(body)
assert.equal(credential.id, json.id)
end)
it("retrieves key-auth credential by key", function()
local res = assert(admin_client:send {
method = "GET",
path = "/consumers/bob/key-auth/"..credential.key
})
local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.equal(credential.id, json.id)
end)
it("retrieves credential by id only if the credential belongs to the specified consumer", function()
assert(helpers.dao.consumers:insert {
username = "alice"
Expand All @@ -171,7 +180,7 @@ describe("Plugin: key-auth (API)", function()
end)

describe("PATCH", function()
it("updates a credential", function()
it("updates a credential by id", function()
local res = assert(admin_client:send {
method = "PATCH",
path = "/consumers/bob/key-auth/"..credential.id,
Expand All @@ -186,6 +195,21 @@ describe("Plugin: key-auth (API)", function()
local json = cjson.decode(body)
assert.equal("4321", json.key)
end)
it("updates a credential by key", function()
local res = assert(admin_client:send {
method = "PATCH",
path = "/consumers/bob/key-auth/"..credential.key,
body = {
key = "4321UPD"
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.equal("4321UPD", json.key)
end)
describe("errors", function()
it("handles invalid input", function()
local res = assert(admin_client:send {
Expand Down Expand Up @@ -218,7 +242,7 @@ describe("Plugin: key-auth (API)", function()
method = "DELETE",
path = "/consumers/bob/key-auth/blah"
})
assert.res_status(400, res)
assert.res_status(404, res)
end)
it("returns 404 if not found", function()
local res = assert(admin_client:send {
Expand Down
27 changes: 25 additions & 2 deletions spec/03-plugins/06-jwt/02-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,17 @@ describe("Plugin: jwt (API)", function()
})
assert.res_status(200, res)
end)
it("retrieves by key", function()
local res = assert(admin_client:send {
method = "GET",
path = "/consumers/bob/jwt/"..jwt_secret.key,
})
assert.res_status(200, res)
end)
end)

describe("PATCH", function()
it("updates a credential", function()
it("updates a credential by id", function()
local res = assert(admin_client:send {
method = "PATCH",
path = "/consumers/bob/jwt/"..jwt_secret.id,
Expand All @@ -213,6 +220,22 @@ describe("Plugin: jwt (API)", function()
jwt_secret = cjson.decode(body)
assert.equal("newsecret", jwt_secret.secret)
end)
it("updates a credential by key", function()
local res = assert(admin_client:send {
method = "PATCH",
path = "/consumers/bob/jwt/"..jwt_secret.key,
body = {
key = "alice",
secret = "newsecret2"
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = assert.res_status(200, res)
jwt_secret = cjson.decode(body)
assert.equal("newsecret2", jwt_secret.secret)
end)
end)

describe("DELETE", function()
Expand All @@ -236,7 +259,7 @@ describe("Plugin: jwt (API)", function()
["Content-Type"] = "application/json"
}
})
assert.res_status(400, res)
assert.res_status(404, res)

local res = assert(admin_client:send {
method = "DELETE",
Expand Down
Loading