|
| 1 | +local core = require("apisix.core") |
| 2 | +local http = require("resty.http") |
| 3 | +local json = require("apisix.core.json") |
| 4 | + |
| 5 | +local schema = { |
| 6 | + type = "object", |
| 7 | + properties = { |
| 8 | + host = {type = "string"}, |
| 9 | + ssl_verify = { |
| 10 | + type = "boolean", |
| 11 | + default = true, |
| 12 | + }, |
| 13 | + timeout = { |
| 14 | + type = "integer", |
| 15 | + minimum = 1, |
| 16 | + maximum = 60000, |
| 17 | + default = 3000, |
| 18 | + description = "timeout in milliseconds", |
| 19 | + }, |
| 20 | + keepalive = {type = "boolean", default = true}, |
| 21 | + keepalive_timeout = {type = "integer", minimum = 1000, default = 60000}, |
| 22 | + keepalive_pool = {type = "integer", minimum = 1, default = 5}, |
| 23 | + redirect_unauthorized = {type = "boolean", default = false}, |
| 24 | + redirect_uri = {type = "string"}, |
| 25 | + }, |
| 26 | + required = {"host"} |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +local _M = { |
| 31 | + version = 0.1, |
| 32 | + priority = 3000, |
| 33 | + name = "hub-orgs", |
| 34 | + schema = schema, |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +function _M.check_schema(conf) |
| 39 | + return core.schema.check(schema, conf) |
| 40 | +end |
| 41 | + |
| 42 | +local function build_json_error(code, status, reason) |
| 43 | + |
| 44 | + core.response.set_header("content", "application/json") |
| 45 | + local res = { |
| 46 | + error = { |
| 47 | + code = code, |
| 48 | + status = status, |
| 49 | + reason = reason |
| 50 | + } |
| 51 | + } |
| 52 | + return json.encode(res) |
| 53 | +end |
| 54 | + |
| 55 | +function _M.access(conf, ctx) |
| 56 | + local headers = core.request.headers(); |
| 57 | + local user_id = ctx.var.kratos_user_id |
| 58 | + |
| 59 | + if not user_id then |
| 60 | + local res = build_json_error(500, "Internal server error", "Unable to read user-id from kratos plugin") |
| 61 | + core.log.error("unable to read user-id from kratos plugin") |
| 62 | + return 500, res |
| 63 | + end |
| 64 | + -- Get Org data |
| 65 | + local params = { |
| 66 | + method = "GET", |
| 67 | + headers = { |
| 68 | + ["X-USER-ID"] = user_id, |
| 69 | + ["Content-Type"] = "application/json", |
| 70 | + ["Accept"] = "application/json", |
| 71 | + }, |
| 72 | + keepalive = conf.keepalive, |
| 73 | + ssl_verify = conf.ssl_verify |
| 74 | + } |
| 75 | + |
| 76 | + -- Get slug from header |
| 77 | + local org_slug = string.lower(string.match(headers.host, "([^.]+).")) |
| 78 | + |
| 79 | + -- make the call - get org id |
| 80 | + local endpoint = conf.host .. "/organizations/" .. org_slug |
| 81 | + local httpc = http.new() |
| 82 | + httpc:set_timeout(conf.timeout) |
| 83 | + local res, err = httpc:request_uri(endpoint, params) |
| 84 | + |
| 85 | + -- return 503 if error on response or when parsing |
| 86 | + if not res then |
| 87 | + local res = build_json_error(500, "Internal server error", "Unable to get organizations") |
| 88 | + return 500, res |
| 89 | + end |
| 90 | + |
| 91 | + local org , err = json.decode(res.body) |
| 92 | + if not org then |
| 93 | + local res = build_json_error(404, "Not found", "No organization found with slug: " .. org_slug) |
| 94 | + core.log.error("Failed to parse organization data. invalid response body: ", res.body, " err: ", err) |
| 95 | + return 404, res |
| 96 | + end |
| 97 | + |
| 98 | + if conf.keepalive then |
| 99 | + params.keepalive_timeout = conf.keepalive_timeout |
| 100 | + params.keepalive_pool = conf.keepalive_pool |
| 101 | + end |
| 102 | + |
| 103 | + |
| 104 | + -- make the call - get affiliations |
| 105 | + local endpoint = conf.host .. "/affiliations" |
| 106 | + local res, err = httpc:request_uri(endpoint, params) |
| 107 | + -- return 503 if error on response or when parsing |
| 108 | + if not res then |
| 109 | + local res = build_json_error(500, "Internal server error", "Unable to get affiliations") |
| 110 | + core.log.error("Failed to get affiliations. invalid response body: ", res.body, " err: ", err) |
| 111 | + return 500, res |
| 112 | + end |
| 113 | + |
| 114 | + local affiliations, err = json.decode(res.body) |
| 115 | + if not affiliations then |
| 116 | + local res = build_json_error(404, "Not found", "No affiliations found for user id: " .. user_id) |
| 117 | + return res.status, res |
| 118 | + end |
| 119 | + |
| 120 | + -- Expose org_id and affiliations on variables: org_id, hub_affiliations |
| 121 | + core.ctx.register_var("org_id", function(ctx) |
| 122 | + return org.id |
| 123 | + end) |
| 124 | + |
| 125 | + local affiliations = ngx.encode_base64(res.body) |
| 126 | + core.ctx.register_var("hub_affiliations", function(ctx) |
| 127 | + return affiliations |
| 128 | + end) |
| 129 | +end |
| 130 | + |
| 131 | +return _M |
0 commit comments