@@ -23,7 +23,7 @@ static char * ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, voi
23
23
static int hex_char_to_binary ( char ch , char * ret );
24
24
static int hex_to_binary ( const char * str , u_char * buf , int len );
25
25
26
- static ngx_command_t ngx_http_auth_jwt_commands [] = {
26
+ static ngx_command_t ngx_http_auth_jwt_commands [] = {
27
27
28
28
{ ngx_string ("auth_jwt_loginurl" ),
29
29
NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
@@ -38,19 +38,19 @@ static ngx_command_t ngx_http_auth_jwt_commands[] = {
38
38
NGX_HTTP_LOC_CONF_OFFSET ,
39
39
offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_key ),
40
40
NULL },
41
-
41
+
42
42
{ ngx_string ("auth_jwt_enabled" ),
43
43
NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_FLAG ,
44
44
ngx_conf_set_flag_slot ,
45
45
NGX_HTTP_LOC_CONF_OFFSET ,
46
46
offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_enabled ),
47
47
NULL },
48
48
49
- ngx_null_command
49
+ ngx_null_command
50
50
};
51
51
52
52
53
- static ngx_http_module_t ngx_http_auth_jwt_module_ctx = {
53
+ static ngx_http_module_t ngx_http_auth_jwt_module_ctx = {
54
54
NULL , /* preconfiguration */
55
55
ngx_http_auth_jwt_init , /* postconfiguration */
56
56
@@ -65,7 +65,7 @@ static ngx_http_module_t ngx_http_auth_jwt_module_ctx = {
65
65
};
66
66
67
67
68
- ngx_module_t ngx_http_auth_jwt_module = {
68
+ ngx_module_t ngx_http_auth_jwt_module = {
69
69
NGX_MODULE_V1 ,
70
70
& ngx_http_auth_jwt_module_ctx , /* module context */
71
71
ngx_http_auth_jwt_commands , /* module directives */
@@ -89,7 +89,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
89
89
ngx_str_t jwtCookieVal ;
90
90
char * jwtCookieValChrPtr ;
91
91
char * return_url ;
92
- ngx_http_auth_jwt_loc_conf_t * jwtcf ;
92
+ ngx_http_auth_jwt_loc_conf_t * jwtcf ;
93
93
u_char * keyBinary ;
94
94
jwt_t * jwt ;
95
95
int jwtParseReturnCode ;
@@ -117,7 +117,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
117
117
n = ngx_http_parse_multi_header_lines (& r -> headers_in .cookies , & passportKeyCookieName , & jwtCookieVal );
118
118
if (n == NGX_DECLINED )
119
119
{
120
- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "failed to obtain a jwt cookie " );
120
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "failed to find a jwt" );
121
121
goto redirect ;
122
122
}
123
123
}
@@ -183,10 +183,12 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
183
183
int loginlen ;
184
184
char * scheme ;
185
185
ngx_str_t server ;
186
- ngx_str_t uri_variable_name = ngx_string ("request_uri" );;
186
+ ngx_str_t uri_variable_name = ngx_string ("request_uri" );
187
187
ngx_int_t uri_variable_hash ;
188
188
ngx_http_variable_value_t * request_uri_var ;
189
189
ngx_str_t uri ;
190
+ ngx_str_t uri_escaped ;
191
+ uintptr_t escaped_len ;
190
192
191
193
loginlen = jwtcf -> auth_jwt_loginurl .len ;
192
194
@@ -197,21 +199,36 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
197
199
uri_variable_hash = ngx_hash_key (uri_variable_name .data , uri_variable_name .len );
198
200
request_uri_var = ngx_http_get_variable (r , & uri_variable_name , uri_variable_hash );
199
201
200
- // get the uri
202
+ // get the URI
201
203
if (request_uri_var && !request_uri_var -> not_found && request_uri_var -> valid )
202
204
{
203
205
// 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
+ uri .data = ngx_palloc (r -> pool , request_uri_var -> len );
207
+ uri .len = request_uri_var -> len ;
206
208
ngx_memcpy (uri .data , request_uri_var -> data , request_uri_var -> len );
209
+
210
+
211
+ char * tmp = ngx_alloc (uri .len + 1 , r -> connection -> log );
212
+ ngx_memcpy (tmp , uri .data , uri .len );
213
+ * (tmp + uri .len ) = '\0' ;
214
+
215
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "found uri with querystring %s" , tmp );
207
216
}
208
217
else
209
218
{
210
219
// fallback to the querystring without params
211
220
uri = r -> uri ;
221
+
222
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "fallback to querystring without params" );
212
223
}
213
224
214
- r -> headers_out .___location -> value .len = loginlen + sizeof ("?return_url=" ) - 1 + strlen (scheme ) + sizeof ("://" ) - 1 + server .len + uri .len ;
225
+ // escape the URI
226
+ escaped_len = 2 * ngx_escape_uri (NULL , uri .data , uri .len , NGX_ESCAPE_URI ) + uri .len ;
227
+ uri_escaped .data = ngx_palloc (r -> pool , escaped_len );
228
+ uri_escaped .len = escaped_len ;
229
+ ngx_escape_uri (uri_escaped .data , uri .data , uri .len , NGX_ESCAPE_URI );
230
+
231
+ r -> headers_out .___location -> value .len = loginlen + sizeof ("?return_url=" ) - 1 + strlen (scheme ) + sizeof ("://" ) - 1 + server .len + uri_escaped .len ;
215
232
return_url = ngx_alloc (r -> headers_out .___location -> value .len , r -> connection -> log );
216
233
ngx_memcpy (return_url , jwtcf -> auth_jwt_loginurl .data , jwtcf -> auth_jwt_loginurl .len );
217
234
int return_url_idx = jwtcf -> auth_jwt_loginurl .len ;
@@ -223,11 +240,11 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
223
240
return_url_idx += sizeof ("://" ) - 1 ;
224
241
ngx_memcpy (return_url + return_url_idx , server .data , server .len );
225
242
return_url_idx += server .len ;
226
- ngx_memcpy (return_url + return_url_idx , uri .data , uri .len );
227
- return_url_idx += uri .len ;
243
+ ngx_memcpy (return_url + return_url_idx , uri_escaped .data , uri_escaped .len );
244
+ return_url_idx += uri_escaped .len ;
228
245
r -> headers_out .___location -> value .data = (u_char * )return_url ;
229
246
230
- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "redirect for get request" );
247
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "return_url: %s" , return_url );
231
248
}
232
249
else
233
250
{
@@ -262,7 +279,7 @@ static ngx_int_t ngx_http_auth_jwt_init(ngx_conf_t *cf)
262
279
static void *
263
280
ngx_http_auth_jwt_create_loc_conf (ngx_conf_t * cf )
264
281
{
265
- ngx_http_auth_jwt_loc_conf_t * conf ;
282
+ ngx_http_auth_jwt_loc_conf_t * conf ;
266
283
267
284
conf = ngx_pcalloc (cf -> pool , sizeof (ngx_http_auth_jwt_loc_conf_t ));
268
285
if (conf == NULL )
@@ -282,45 +299,45 @@ ngx_http_auth_jwt_create_loc_conf(ngx_conf_t *cf)
282
299
static char *
283
300
ngx_http_auth_jwt_merge_loc_conf (ngx_conf_t * cf , void * parent , void * child )
284
301
{
285
- ngx_http_auth_jwt_loc_conf_t * prev = parent ;
286
- ngx_http_auth_jwt_loc_conf_t * conf = child ;
302
+ ngx_http_auth_jwt_loc_conf_t * prev = parent ;
303
+ ngx_http_auth_jwt_loc_conf_t * conf = child ;
287
304
288
305
ngx_conf_merge_str_value (conf -> auth_jwt_loginurl , prev -> auth_jwt_loginurl , "" );
289
306
ngx_conf_merge_str_value (conf -> auth_jwt_key , prev -> auth_jwt_key , "" );
290
307
291
308
292
309
if (conf -> auth_jwt_enabled == ((ngx_flag_t ) - 1 ))
293
310
{
294
- conf -> auth_jwt_enabled = (prev -> auth_jwt_enabled == ((ngx_flag_t ) - 1 )) ? 0 : prev -> auth_jwt_enabled ;
311
+ conf -> auth_jwt_enabled = (prev -> auth_jwt_enabled == ((ngx_flag_t ) - 1 )) ? 0 : prev -> auth_jwt_enabled ;
295
312
}
296
313
297
314
ngx_conf_log_error (NGX_LOG_DEBUG , cf , 0 , "Merged Location Configuration" );
298
315
299
316
// ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "Key: %s, Enabled: %d",
300
317
// conf->auth_jwt_key.data,
301
318
// conf->auth_jwt_enabled);
302
- return NGX_CONF_OK ;
319
+ return NGX_CONF_OK ;
303
320
}
304
321
305
322
static int
306
323
hex_char_to_binary ( char ch , char * ret )
307
324
{
308
- ch = tolower ( ch );
325
+ ch = tolower ( ch );
309
326
if ( isdigit ( ch ) )
310
- * ret = ch - '0' ;
327
+ * ret = ch - '0' ;
311
328
else if ( ch >= 'a' && ch <= 'f' )
312
329
* ret = ( ch - 'a' ) + 10 ;
313
330
else if ( ch >= 'A' && ch <= 'F' )
314
- * ret = ( ch - 'A' ) + 10 ;
331
+ * ret = ( ch - 'A' ) + 10 ;
315
332
else
316
333
return * ret = 0 ;
317
- return 1 ;
334
+ return 1 ;
318
335
}
319
336
320
337
static int
321
338
hex_to_binary ( const char * str , u_char * buf , int len ) {
322
339
u_char
323
- * cpy = buf ;
340
+ * cpy = buf ;
324
341
char
325
342
low ,
326
343
high ;
@@ -337,6 +354,6 @@ hex_to_binary( const char* str, u_char* buf, int len ) {
337
354
338
355
* cpy ++ = low | (high << 4 );
339
356
}
340
- return 0 ;
357
+ return 0 ;
341
358
}
342
359
0 commit comments