@@ -29,7 +29,7 @@ local schema = {
2929
3030local _M = {
3131 version = 0.1 ,
32- priority = 3000 ,
32+ priority = 1005 ,
3333 name = " hub-orgs" ,
3434 schema = schema ,
3535}
@@ -72,38 +72,20 @@ function _M.access(conf, ctx)
7272 keepalive = conf .keepalive ,
7373 ssl_verify = conf .ssl_verify
7474 }
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
75+ local org_id = ngx .var [' cookie__hub_org' ] or headers [' x-organization-id' ]
76+ if not org_id then
77+ local res = build_json_error (401 , " Unauthorized" , " X-Organization-Id header not found" )
78+ core .log .error (" Failed to get org id from header or cookie" )
79+ return 401 , res
8980 end
9081
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-
10482 -- make the call - get affiliations
10583 local endpoint = conf .host .. " /affiliations"
84+ local httpc = http .new ()
85+ httpc :set_timeout (conf .timeout )
10686 local res , err = httpc :request_uri (endpoint , params )
87+
88+ core .log .error (" Getting affiliations from hub-orgs for user: " , user_id )
10789 -- return 503 if error on response or when parsing
10890 if not res then
10991 local res = build_json_error (500 , " Internal server error" , " Unable to get affiliations" )
@@ -119,13 +101,15 @@ function _M.access(conf, ctx)
119101
120102 -- Expose org_id and affiliations on variables: org_id, hub_affiliations
121103 core .ctx .register_var (" org_id" , function (ctx )
122- return org . id
104+ return org_id
123105 end )
124106
125107 local affiliations = ngx .encode_base64 (res .body )
126108 core .ctx .register_var (" hub_affiliations" , function (ctx )
127109 return affiliations
128110 end )
111+
112+ core .response .set_header (" x-organization-id" , org_id )
129113end
130114
131115return _M
0 commit comments