iGeeky commented on a change in pull request #1204: Wolf-Rbac: Add new api
`change_pwd` and `user_info`.
URL: https://github.com/apache/incubator-apisix/pull/1204#discussion_r388029992
##########
File path: lua/apisix/plugins/wolf-rbac.lua
##########
@@ -351,71 +359,154 @@ local function login()
local consumer = consumers[appid]
if not consumer then
core.log.info("request appid [", appid, "] not found")
- return core.response.exit(400,
- {message = "appid [" .. tostring(appid) .. "] not found"}
- )
+ core.response.exit(400,
+ fail_response("appid [" .. tostring(appid) .. "] not found")
+ )
end
+ return consumer
+end
- core.log.info("consumer: ", core.json.delay_encode(consumer))
-
- local uri = consumer.auth_conf.server .. '/wolf/rbac/login.rest'
- local headers = new_headers()
+local function request_to_wolf_server(method, uri, headers, body)
headers["Content-Type"] = "application/json; charset=utf-8"
local timeout = 1000 * 5
local request_debug = core.json.delay_encode(
{
- method = 'POST', uri = uri, body = args,
+ method = method, uri = uri, body = body,
headers = headers,timeout = timeout
}
)
- core.log.info("login request [", request_debug, "] ....")
- local res, err = http_post(uri, core.json.encode(args), headers, timeout)
+ core.log.info("request [", request_debug, "] ....")
+ local res, err = http_req(method, uri, core.json.encode(body), headers,
timeout)
if err or not res then
- core.log.error("login request [", request_debug, "] failed! err: ",
err)
+ core.log.error("request [", request_debug, "] failed! err: ", err)
return core.response.exit(500,
- {message = "request to wolf-server failed! " .. tostring(err)})
+ fail_response("request to wolf-server failed! " .. tostring(err))
+ )
end
- core.log.info("login request [", request_debug, "] status: ", res.status,
+ core.log.info("request [", request_debug, "] status: ", res.status,
", body: ", res.body)
if res.status ~= 200 then
- core.log.error("login request [", request_debug, "] failed! status: ",
- res.status)
+ core.log.error("request [", request_debug, "] failed! status: ",
+ res.status)
return core.response.exit(500,
- {
- message = "request to wolf-server failed! status:"
- .. tostring(res.status)
- }
+ fail_response("request to wolf-server failed! status:"
+ .. tostring(res.status))
)
end
local body, err = json.decode(res.body)
if err or not body then
- core.log.error("login request [", request_debug, "] failed! err:", err)
- return core.response.exit(500, {message = "request to wolf-server
failed!"})
+ core.log.error("request [", request_debug, "] failed! err:", err)
+ return core.response.exit(500, fail_response("request to wolf-server
failed!"))
end
if not body.ok then
- core.log.error("user login [", request_debug, "] failed! response
body:",
+ core.log.error("request [", request_debug, "] failed! response body:",
core.json.delay_encode(body))
- return core.response.exit(200, {message = body.reason})
+ return core.response.exit(200, fail_response(body.reason))
end
- core.log.info("user login [", request_debug, "] success! response body:",
+
+ core.log.info("request [", request_debug, "] success! response body:",
core.json.delay_encode(body))
+ return body
+end
+
+local function wolf_rbac_login()
+ local args = get_args()
+ if not args then
+ return core.response.exit(400, fail_response("invalid request"))
+ end
+ if not args.appid then
+ return core.response.exit(400, fail_response("appid is missing"))
+ end
+
+ local appid = args.appid
+ local consumer = get_consumer(appid)
+ core.log.info("consumer: ", core.json.delay_encode(consumer))
+
+ local uri = consumer.auth_conf.server .. '/wolf/rbac/login.rest'
+ local headers = new_headers()
+ local body = request_to_wolf_server('POST', uri, headers, args)
local userInfo = body.data.userInfo
local wolf_token = body.data.token
local rbac_token = create_rbac_token(appid, wolf_token)
- core.response.exit(200, {rbac_token = rbac_token, user_info = userInfo})
+ core.response.exit(200, success_response(nil, {rbac_token = rbac_token,
user_info = userInfo}))
+end
+
+local function get_wolf_token(ctx)
+ local url = ctx.var.uri
+ local action = ctx.var.request_method
+ local clientIP = core.request.get_ip(ctx)
+ local permItem = {action = action, url = url, clientIP = clientIP}
+ core.log.info("hit wolf-rbac change_password api")
+ local rbac_token = fetch_rbac_token(ctx)
Review comment:
Is it not recommend to directly use `core.response.exit` to return error
information in the called function? When I thought about it at that time, all
the calls would no longer need to be judged. The code would look simpler
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services