Skip to content

Commit 95b2e1e

Browse files
author
root
committed
grabbing the URI from a different place that contains the request parameters
1 parent d83bb40 commit 95b2e1e

File tree

1 file changed

+110
-79
lines changed

1 file changed

+110
-79
lines changed

src/ngx_http_auth_jwt_module.c

Lines changed: 110 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include <jansson.h>
1212

1313
typedef struct {
14-
ngx_str_t auth_jwt_loginurl;
15-
ngx_str_t auth_jwt_key;
14+
ngx_str_t auth_jwt_loginurl;
15+
ngx_str_t auth_jwt_key;
1616
ngx_flag_t auth_jwt_enabled;
1717
} ngx_http_auth_jwt_loc_conf_t;
1818

@@ -25,59 +25,59 @@ static int hex_to_binary( const char* str, u_char* buf, int len );
2525

2626
static ngx_command_t ngx_http_auth_jwt_commands[] = {
2727

28-
{ ngx_string("auth_jwt_loginurl"),
29-
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
30-
ngx_conf_set_str_slot,
31-
NGX_HTTP_LOC_CONF_OFFSET,
32-
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_loginurl),
33-
NULL },
34-
35-
{ ngx_string("auth_jwt_key"),
36-
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
37-
ngx_conf_set_str_slot,
38-
NGX_HTTP_LOC_CONF_OFFSET,
39-
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_key),
40-
NULL },
28+
{ ngx_string("auth_jwt_loginurl"),
29+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
30+
ngx_conf_set_str_slot,
31+
NGX_HTTP_LOC_CONF_OFFSET,
32+
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_loginurl),
33+
NULL },
34+
35+
{ ngx_string("auth_jwt_key"),
36+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
37+
ngx_conf_set_str_slot,
38+
NGX_HTTP_LOC_CONF_OFFSET,
39+
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_key),
40+
NULL },
4141

42-
{ ngx_string("auth_jwt_enabled"),
43-
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
44-
ngx_conf_set_flag_slot,
45-
NGX_HTTP_LOC_CONF_OFFSET,
46-
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_enabled),
47-
NULL },
48-
49-
ngx_null_command
42+
{ ngx_string("auth_jwt_enabled"),
43+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
44+
ngx_conf_set_flag_slot,
45+
NGX_HTTP_LOC_CONF_OFFSET,
46+
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_enabled),
47+
NULL },
48+
49+
ngx_null_command
5050
};
5151

5252

5353
static ngx_http_module_t ngx_http_auth_jwt_module_ctx = {
54-
NULL, /* preconfiguration */
55-
ngx_http_auth_jwt_init, /* postconfiguration */
54+
NULL, /* preconfiguration */
55+
ngx_http_auth_jwt_init, /* postconfiguration */
5656

57-
NULL, /* create main configuration */
58-
NULL, /* init main configuration */
57+
NULL, /* create main configuration */
58+
NULL, /* init main configuration */
5959

60-
NULL, /* create server configuration */
61-
NULL, /* merge server configuration */
60+
NULL, /* create server configuration */
61+
NULL, /* merge server configuration */
6262

63-
ngx_http_auth_jwt_create_loc_conf, /* create ___location configuration */
64-
ngx_http_auth_jwt_merge_loc_conf /* merge ___location configuration */
63+
ngx_http_auth_jwt_create_loc_conf, /* create ___location configuration */
64+
ngx_http_auth_jwt_merge_loc_conf /* merge ___location configuration */
6565
};
6666

6767

6868
ngx_module_t ngx_http_auth_jwt_module = {
69-
NGX_MODULE_V1,
70-
&ngx_http_auth_jwt_module_ctx, /* module context */
71-
ngx_http_auth_jwt_commands, /* module directives */
72-
NGX_HTTP_MODULE, /* module type */
73-
NULL, /* init master */
74-
NULL, /* init module */
75-
NULL, /* init process */
76-
NULL, /* init thread */
77-
NULL, /* exit thread */
78-
NULL, /* exit process */
79-
NULL, /* exit master */
80-
NGX_MODULE_V1_PADDING
69+
NGX_MODULE_V1,
70+
&ngx_http_auth_jwt_module_ctx, /* module context */
71+
ngx_http_auth_jwt_commands, /* module directives */
72+
NGX_HTTP_MODULE, /* module type */
73+
NULL, /* init master */
74+
NULL, /* init module */
75+
NULL, /* init process */
76+
NULL, /* init thread */
77+
NULL, /* exit thread */
78+
NULL, /* exit process */
79+
NULL, /* exit master */
80+
NGX_MODULE_V1_PADDING
8181
};
8282

8383

@@ -97,7 +97,6 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
9797
time_t exp;
9898
time_t now;
9999

100-
101100
jwtcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_jwt_module);
102101

103102
if (!jwtcf->auth_jwt_enabled)
@@ -169,20 +168,48 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
169168

170169
redirect:
171170
r->headers_out.___location = ngx_list_push(&r->headers_out.headers);
172-
if (r->headers_out.___location == NULL) {
171+
172+
if (r->headers_out.___location == NULL)
173+
{
173174
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
174175
}
176+
175177
r->headers_out.___location->hash = 1;
176178
r->headers_out.___location->key.len = sizeof("Location") - 1;
177179
r->headers_out.___location->key.data = (u_char *) "Location";
178180

179181
if (r->method == NGX_HTTP_GET)
180182
{
181-
int loginlen = jwtcf->auth_jwt_loginurl.len;
182-
183-
char *scheme = (r->connection->ssl) ? "https" : "http";
184-
ngx_str_t server = r->headers_in.server;
185-
ngx_str_t uri = r->uri;
183+
int loginlen;
184+
char * scheme;
185+
ngx_str_t server;
186+
ngx_str_t uri_variable_name = ngx_string("request_uri");;
187+
ngx_int_t uri_variable_hash;
188+
ngx_http_variable_value_t * request_uri_var;
189+
ngx_str_t uri;
190+
191+
loginlen = jwtcf->auth_jwt_loginurl.len;
192+
193+
scheme = (r->connection->ssl) ? "https" : "http";
194+
server = r->headers_in.server;
195+
196+
// get the URI
197+
uri_variable_hash = ngx_hash_key(uri_variable_name.data, uri_variable_name.len);
198+
request_uri_var = ngx_http_get_variable(r, &uri_variable_name, uri_variable_hash);
199+
200+
// get the uri
201+
if(request_uri_var && !request_uri_var->not_found && request_uri_var->valid)
202+
{
203+
// ideally we would like the uri with the querystring parameters
204+
uri.data = ngx_palloc(r->pool, request_uri_var->len);
205+
uri.len = request_uri_var->len;
206+
ngx_memcpy(uri.data, request_uri_var->data, request_uri_var->len);
207+
}
208+
else
209+
{
210+
// fallback to the querystring without params
211+
uri = r->uri;
212+
}
186213

187214
r->headers_out.___location->value.len = loginlen + sizeof("?return_url=") - 1 + strlen(scheme) + sizeof("://") - 1 + server.len + uri.len;
188215
return_url = ngx_alloc(r->headers_out.___location->value.len, r->connection->log);
@@ -204,6 +231,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
204231
}
205232
else
206233
{
234+
// for non-get requests, redirect to the login page without a return URL
207235
r->headers_out.___location->value.len = jwtcf->auth_jwt_loginurl.len;
208236
r->headers_out.___location->value.data = jwtcf->auth_jwt_loginurl.data;
209237
}
@@ -215,53 +243,56 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
215243
static ngx_int_t ngx_http_auth_jwt_init(ngx_conf_t *cf)
216244
{
217245
ngx_http_handler_pt *h;
218-
ngx_http_core_main_conf_t *cmcf;
246+
ngx_http_core_main_conf_t *cmcf;
219247

220-
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
248+
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
221249

222-
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
223-
if (h == NULL) {
224-
return NGX_ERROR;
225-
}
250+
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
251+
if (h == NULL)
252+
{
253+
return NGX_ERROR;
254+
}
226255

227-
*h = ngx_http_auth_jwt_handler;
256+
*h = ngx_http_auth_jwt_handler;
228257

229-
return NGX_OK;
258+
return NGX_OK;
230259
}
231260

232261

233262
static void *
234263
ngx_http_auth_jwt_create_loc_conf(ngx_conf_t *cf)
235264
{
236-
ngx_http_auth_jwt_loc_conf_t *conf;
265+
ngx_http_auth_jwt_loc_conf_t *conf;
237266

238-
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_auth_jwt_loc_conf_t));
239-
if (conf == NULL) {
240-
return NULL;
241-
}
267+
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_auth_jwt_loc_conf_t));
268+
if (conf == NULL)
269+
{
270+
return NULL;
271+
}
242272

243273
// set the flag to unset
244274
conf->auth_jwt_enabled = (ngx_flag_t) -1;
245275

246276
ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, "Created Location Configuration");
247277

248-
return conf;
278+
return conf;
249279
}
250280

251281

252282
static char *
253283
ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
254284
{
255-
ngx_http_auth_jwt_loc_conf_t *prev = parent;
256-
ngx_http_auth_jwt_loc_conf_t *conf = child;
285+
ngx_http_auth_jwt_loc_conf_t *prev = parent;
286+
ngx_http_auth_jwt_loc_conf_t *conf = child;
257287

258288
ngx_conf_merge_str_value(conf->auth_jwt_loginurl, prev->auth_jwt_loginurl, "");
259289
ngx_conf_merge_str_value(conf->auth_jwt_key, prev->auth_jwt_key, "");
260290

261291

262-
if (conf->auth_jwt_enabled == ((ngx_flag_t) -1)) {
263-
conf->auth_jwt_enabled = (prev->auth_jwt_enabled == ((ngx_flag_t) -1)) ? 0 : prev->auth_jwt_enabled;
264-
}
292+
if (conf->auth_jwt_enabled == ((ngx_flag_t) -1))
293+
{
294+
conf->auth_jwt_enabled = (prev->auth_jwt_enabled == ((ngx_flag_t) -1)) ? 0 : prev->auth_jwt_enabled;
295+
}
265296

266297
ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, "Merged Location Configuration");
267298

@@ -274,16 +305,16 @@ ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
274305
static int
275306
hex_char_to_binary( char ch, char* ret )
276307
{
277-
ch = tolower( ch );
278-
if( isdigit( ch ) )
279-
*ret = ch - '0';
280-
else if( ch >= 'a' && ch <= 'f' )
281-
*ret = ( ch - 'a' ) + 10;
282-
else if( ch >= 'A' && ch <= 'F' )
283-
*ret = ( ch - 'A' ) + 10;
284-
else
285-
return *ret = 0;
286-
return 1;
308+
ch = tolower( ch );
309+
if( isdigit( ch ) )
310+
*ret = ch - '0';
311+
else if( ch >= 'a' && ch <= 'f' )
312+
*ret = ( ch - 'a' ) + 10;
313+
else if( ch >= 'A' && ch <= 'F' )
314+
*ret = ( ch - 'A' ) + 10;
315+
else
316+
return *ret = 0;
317+
return 1;
287318
}
288319

289320
static int

0 commit comments

Comments
 (0)