Skip to content

Commit 0cee345

Browse files
Initial attempt to add userinfo endpoint
1 parent 77a8863 commit 0cee345

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/etc/nginx/nginx.conf.template

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,23 @@ http {
299299
proxy_read_timeout 20d;
300300
}
301301
}
302+
303+
# Userinfo
304+
server {
305+
listen PROXY_USERINFO_LISTEN_ADDRESS:PROXY_USERINFO_LISTEN_PORT;
306+
307+
error_log /var/log/nginx/error.log ERROR_LOG_LEVEL;
308+
309+
location /userinfo {
310+
311+
set_by_lua $analytical_platform_tool 'return "ANALYTICAL_PLATFORM_TOOL"';
312+
set_by_lua $auth0_client_id 'return "AUTH0_CLIENT_ID"';
313+
set_by_lua $auth0_client_secret 'return "AUTH0_CLIENT_SECRET"';
314+
set_by_lua $auth0_tenant_domain 'return "AUTH0_TENANT_DOMAIN"';
315+
set_by_lua $username 'return "USERNAME"';
316+
317+
content_by_lua_file /opt/lua-scripts/userinfo-api.lua;
318+
}
319+
}
320+
302321
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
local http = require("resty.http")
2+
local httpc = http.new()
3+
local cjson = require("cjson")
4+
5+
6+
local function get_access_token()
7+
local endpoint = "https://" .. ngx.var.auth0_tenant_domain .. "/oauth/token"
8+
local headers = {
9+
["Content-Type"] = "application/x-www-form-urlencoded"
10+
}
11+
12+
local body = {
13+
client_id = ngx.var.auth0_client_id,
14+
client_secret = ngx.var.auth0_client_secret,
15+
audience = "https://" .. ngx.var.auth0_tenant_domain .. "/api/v2/",
16+
grant_type = "client_credentials"
17+
}
18+
19+
local res, err = httpc:request_uri(endpoint, {
20+
method = "POST",
21+
body = ngx.encode_args(body),
22+
headers = headers,
23+
})
24+
ngx.log(ngx.NOTICE, ngx.var.auth0_tenant_domain)
25+
if not res then
26+
ngx.log(ngx.NOTICE, err)
27+
return false, nil, err
28+
else
29+
return true, cjson.decode(res.body).access_token, nil
30+
end
31+
end
32+
33+
local function get_user_info(access_token)
34+
local user_endpoint = "https://" .. ngx.var.auth0_tenant_domain .. "/api/v2/users"
35+
local user_info_headers = {
36+
["Content-Type"] = "application/json",
37+
["Authorization"] = "Bearer " .. access_token
38+
}
39+
40+
local search_params = {
41+
q = "nickname: " .. ngx.var.username,
42+
search_engine = "v2"
43+
}
44+
45+
local user_res, err = httpc:request_uri(user_endpoint, {
46+
method = "GET",
47+
query = search_params,
48+
headers = user_info_headers,
49+
})
50+
51+
local found_user = cjson.decode(user_res.body)[1]
52+
if not found_user then
53+
return '{"error": 404, "message": "Not found"}'
54+
else
55+
return cjson.encode(found_user)
56+
end
57+
end
58+
59+
-- Main entry
60+
local succeeded, access_token, err = get_access_token()
61+
62+
if succeeded then
63+
ngx.say(get_user_info(access_token))
64+
else
65+
ngx.status = 400
66+
ngx.say(err)
67+
end

0 commit comments

Comments
 (0)