-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx.conf
288 lines (234 loc) · 10.8 KB
/
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
## NEED CHANGE (defines the user of the nginx workers)
# user user group;
## THIS PARAMETERS BE SAFELY OVER RIDDEN BY YOUR DEFAULT NGINX CONF
worker_processes 2;
error_log stderr debug;
env REDISTOGO_URL;
daemon off;
events {
worker_connections 256;
}
http {
lua_shared_dict api_keys 10m;
server_names_hash_bucket_size 128;
lua_package_path ";;$prefix/?.lua;";
init_by_lua 'math.randomseed(ngx.time()) ; cjson = require("cjson")';
resolver 8.8.8.8 8.8.4.4;
upstream backend_user-goals-api.herokuapp.com {
server user-goals-api.herokuapp.com:443 max_fails=5 fail_timeout=30;
}
server {
lua_code_cache off;
listen ${{PORT}};
## CHANGE YOUR SERVER_NAME TO YOUR CUSTOM DOMAIN OR LEAVE IT BLANK IF ONLY HAVE ONE
server_name user-goals-api.herokuapp.com;
underscores_in_headers on;
set_by_lua $deployment 'return os.getenv("THREESCALE_DEPLOYMENT_ENV")';
set $threescale_backend "https://su1.3scale.net:443";
location /authorize {
set $cors '';
if ($http_origin ~* 'https?://(localhost|user-goals-api\.3scale\.net|user-goals-api-admin\.3scale\.net)') {
set $cors 'true';
}
if ($cors = 'true') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
}
if ($request_method = 'OPTIONS') {
return 204;
}
error_log logs/error.log debug;
set $red_url "";
set $client_id "";
set $login_url "https://user-goals-api.herokuapp.com/authorize";
set $service_id 2555417724321;
proxy_set_header Content-Type "application/x-www-form-urlencoded";
content_by_lua_file authorize.lua;
}
location = /_threescale/oauth_store_token {
internal;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host "su1.3scale.net"; #needed. backend discards other hosts
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
set $service_id ${{THREESCALE_SERVICE_ID}};
proxy_method POST;
proxy_pass $threescale_backend/services/$service_id/oauth_access_tokens.xml;
}
location = /_threescale/redirect_uri_matches {
internal;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host "su1.3scale.net"; #needed. backend discards other hosts
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
set $service_id ${{THREESCALE_SERVICE_ID}};
proxy_pass $threescale_backend/transactions/oauth_authorize.xml?provider_key=$provider_key&service_id=$service_id&app_id=$client_id&redirect_url=$red_url;
}
location /_threescale/toauth_authorize {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host "su1.3scale.net" ;
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
set $service_id ${{THREESCALE_SERVICE_ID}};
proxy_ignore_client_abort on;
proxy_pass $threescale_backend/transactions/oauth_authorize.xml?provider_key=$provider_key&service_id=$service_id&access_token=$arg_access_token&$usage;
}
location /_threescale/oauth_report {
internal;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host "su1.3scale.net";
proxy_method POST;
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
set $service_id ${{THREESCALE_SERVICE_ID}};
proxy_pass $threescale_backend/transactions.xml?provider_key=$provider_key&service_id=$service_id&transactions[0][access_token]=$arg_access_token&$usage;
}
location = /_threescale/client_secret_matches {
internal;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host "su1.3scale.net"; #needed. backend discards other hosts
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
set $service_id ${{THREESCALE_SERVICE_ID}};
proxy_pass $threescale_backend/transactions/oauth_authorize.xml?provider_key=$provider_key&service_id=$service_id&app_id=$client_id;
}
location /callback {
set $service_id ${{THREESCALE_SERVICE_ID}};
content_by_lua_file 'authorized_callback.lua';
}
location /oauth/token {
set $cors '';
if ($http_origin ~* 'https?://(localhost|user-goals-api\.3scale\.net|user-goals-api-admin\.3scale\.net)') {
set $cors 'true';
}
if ($cors = 'true') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
}
if ($request_method = 'OPTIONS') {
return 204;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Content-Type "application/x-www-form-urlencoded";
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
content_by_lua_file get_token.lua ;
}
location /_oauth/token {
internal;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Content-Type "application/x-www-form-urlencoded";
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
content_by_lua_file get_token.lua ;
}
location = /threescale_authrep {
internal;
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
proxy_pass $threescale_backend/transactions/authrep.xml?provider_key=$provider_key&$usage&$credentials;
proxy_set_header Host "su1.3scale.net";
}
location = /out_of_band_authrep_action {
internal;
proxy_pass_request_headers off;
##set $provider_key "YOUR_3SCALE_PROVIDER_KEY";
##needs to be in both places, better not to have it on location / for potential security issues, req. are internal
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
content_by_lua '
local cached_key = ngx.var.cached_key
if cached_key ~= nil and cached_key ~= "null" then
local status_code = ngx.var.status
local res1 = ngx.location.capture("/threescale_authrep?code="..status_code, { share_all_vars = true })
if res1.status ~= 200 then
local api_keys = ngx.shared.api_keys
api_keys:delete(cached_key)
end
end
ngx.exit(ngx.HTTP_OK)
';
}
location = /out_of_band_oauth_report_action {
internal;
proxy_pass_request_headers off;
set $provider_key "${{THREESCALE_PROVIDER_KEY}}";
content_by_lua '
if ngx.var.cached_key ~= nil then
local res1 = ngx.location.capture("/_threescale/toauth_authorize?access_token="..ngx.var.access_token, { share_all_vars = true })
if res1.status ~= 200 then
local api_keys = ngx.shared.api_keys
api_keys:delete(ngx.var.cached_key )
else
local res2 = ngx.location.capture("/_threescale/oauth_report?access_token="..ngx.var.access_token, {method = ngx.HTTP_POST, share_all_vars = true})
ngx.status = 200
ngx.header.content_length = 0
ngx.exit(ngx.HTTP_OK)
end
else
local res2 = ngx.location.capture("/_threescale/oauth_report?access_token="..ngx.var.access_token, {method = ngx.HTTP_POST, share_all_vars = true})
ngx.status = 200
ngx.header.content_length = 0
ngx.exit(ngx.HTTP_OK)
end
';
}
location = /threescale_oauth_authrep {
internal;
set $provider_key 0b0ec0f760c715b4dc437d1ae7e13116;
proxy_pass $threescale_backend/transactions/oauth_authrep.xml?provider_key=$provider_key&service_id=$service_id&$usage&$credentials&log%5Bcode%5D=$arg_code&log%5Brequest%5D=$arg_req&log%5Bresponse%5D=$arg_resp;
proxy_set_header Host "su1.3scale.net";
proxy_set_header X-3scale-User-Agent "nginx$deployment";
}
location = /out_of_band_oauth_authrep_action {
internal;
proxy_pass_request_headers off;
##set $provider_key "YOUR_3SCALE_PROVIDER_KEY";
##needs to be in both places, better not to have it on location / for potential security issues, req. are internal
set $provider_key 0b0ec0f760c715b4dc437d1ae7e13116;
content_by_lua '
local method, path, headers = ngx.req.get_method(), ngx.var.request_uri, ngx.req.get_headers()
ngx.log(0,"out of band oauth_authrep")
local req = cjson.encode{method=method, path=path, headers=headers}
local resp = cjson.encode{ body = ngx.var.resp_body, headers = cjson.decode(ngx.var.resp_headers)}
local cached_key = ngx.var.cached_key
if cached_key ~= nil and cached_key ~= "null" then
local status_code = ngx.var.status
local res1 = ngx.location.capture("/threescale_oauth_authrep?code=".. status_code .. "&req=" .. ngx.escape_uri(req) .. "&resp=" .. ngx.escape_uri(resp), { share_all_vars = true })
if res1.status ~= 200 then
local access_tokens = ngx.shared.api_keys
access_tokens:delete(cached_key)
end
end
ngx.exit(ngx.HTTP_OK)
';
}
location / {
set $provider_key null;
set $cached_key null;
set $credentials null;
set $usage null;
set $service_id ${{THREESCALE_SERVICE_ID}};
set $proxy_pass null;
set $secret_token null;
set $resp_body null;
set $resp_headers null;
set $access_token null;
proxy_ignore_client_abort on;
## CHANGE THE PATH TO POINT TO THE RIGHT FILE ON YOUR FILESYSTEM
access_by_lua_file nginx.lua;
body_filter_by_lua 'ngx.ctx.buffered = (ngx.ctx.buffered or "") .. string.sub(ngx.arg[1], 1, 1000)
if ngx.arg[2] then ngx.var.resp_body = ngx.ctx.buffered end';
header_filter_by_lua 'ngx.var.resp_headers = cjson.encode(ngx.resp.get_headers())';
proxy_pass $proxy_pass ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host user-goals-api.herokuapp.com;
proxy_set_header X-3scale-proxy-secret-token $secret_token;
post_action /out_of_band_oauth_authrep_action;
}
}
}