mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-05 19:15:03 +00:00
Adds api_token.lua
This commit is contained in:
parent
e88e13450e
commit
301d71780d
1 changed files with 45 additions and 0 deletions
45
scripts/lua/rest/v1/create/ntopng/api_token.lua
Normal file
45
scripts/lua/rest/v1/create/ntopng/api_token.lua
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
--
|
||||
-- (C) 2013-20 - ntop.org
|
||||
--
|
||||
|
||||
local dirs = ntop.getDirs()
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/pools/?.lua;" .. package.path
|
||||
|
||||
require "lua_utils"
|
||||
local json = require ("dkjson")
|
||||
local rest_utils = require("rest_utils")
|
||||
|
||||
--
|
||||
-- Get a new ntopng user session (Cookie)
|
||||
-- Example: curl -u admin:admin -H "Content-Type: application/json" -d '{"username": "simone"}' http://localhost:3000/lua/rest/v1/create/ntopng/api_token.lua
|
||||
--
|
||||
-- NOTE: in case of invalid login, no error is returned but redirected to login
|
||||
--
|
||||
|
||||
local rc = rest_utils.consts.success.ok
|
||||
local res = {}
|
||||
|
||||
-- An admin user can submit a username to create token for other users
|
||||
-- A non-admin user can only create tokens for itself
|
||||
local username = _POST["username"]
|
||||
|
||||
-- Do not allow non-admins to specify usernames different from their username
|
||||
if not isAdministrator() and _POST["username"] and _POST["username"] ~= _SESSION['user'] then
|
||||
rest_utils.answer(rest_utils.consts.err.invalid_args)
|
||||
return
|
||||
end
|
||||
|
||||
-- Take the username specified in the post or the name of the currently authenticated user
|
||||
-- if no username has been submitted
|
||||
local username = _POST["username"] or _SESSION['user']
|
||||
username = string.lower(username)
|
||||
|
||||
res.api_token = ntop.createUserAPIToken(username)
|
||||
|
||||
if isEmptyString(res.api_token) then
|
||||
rest_utils.answer(rest_utils.consts.err.invalid_args)
|
||||
return
|
||||
end
|
||||
|
||||
rest_utils.answer(rc, res)
|
||||
Loading…
Add table
Add a link
Reference in a new issue