@@ -27,6 +27,7 @@ typedef struct {
27
27
ngx_flag_t auth_jwt_redirect ;
28
28
ngx_str_t auth_jwt_validation_type ;
29
29
ngx_str_t auth_jwt_algorithm ;
30
+ ngx_flag_t auth_jwt_extract_sub ;
30
31
ngx_flag_t auth_jwt_validate_email ;
31
32
ngx_str_t auth_jwt_keyfile_path ;
32
33
ngx_flag_t auth_jwt_use_keyfile ;
@@ -84,6 +85,13 @@ static ngx_command_t ngx_http_auth_jwt_commands[] = {
84
85
offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_algorithm ),
85
86
NULL },
86
87
88
+ { ngx_string ("auth_jwt_extract_sub" ),
89
+ NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_FLAG ,
90
+ ngx_conf_set_flag_slot ,
91
+ NGX_HTTP_LOC_CONF_OFFSET ,
92
+ offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_extract_sub ),
93
+ NULL },
94
+
87
95
{ ngx_string ("auth_jwt_validate_email" ),
88
96
NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_FLAG ,
89
97
ngx_conf_set_flag_slot ,
@@ -152,10 +160,6 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
152
160
jwt_t * jwt = NULL ;
153
161
int jwtParseReturnCode ;
154
162
jwt_alg_t alg ;
155
- const char * sub ;
156
- const char * email ;
157
- ngx_str_t sub_t ;
158
- ngx_str_t email_t ;
159
163
time_t exp ;
160
164
time_t now ;
161
165
ngx_str_t auth_jwt_algorithm ;
@@ -175,6 +179,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
175
179
}
176
180
177
181
jwtCookieValChrPtr = getJwt (r , jwtcf -> auth_jwt_validation_type );
182
+
178
183
if (jwtCookieValChrPtr == NULL )
179
184
{
180
185
ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "failed to find a jwt" );
@@ -184,6 +189,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
184
189
// convert key from hex to binary, if a symmetric key
185
190
186
191
auth_jwt_algorithm = jwtcf -> auth_jwt_algorithm ;
192
+
187
193
if (auth_jwt_algorithm .len == 0 || (auth_jwt_algorithm .len == sizeof ("HS256" ) - 1 && ngx_strncmp (auth_jwt_algorithm .data , "HS256" , sizeof ("HS256" ) - 1 )== 0 ))
188
194
{
189
195
keylen = jwtcf -> auth_jwt_key .len / 2 ;
@@ -218,6 +224,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
218
224
219
225
// validate the jwt
220
226
jwtParseReturnCode = jwt_decode (& jwt , jwtCookieValChrPtr , keyBinary , keylen );
227
+
221
228
if (jwtParseReturnCode != 0 )
222
229
{
223
230
ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "failed to parse jwt" );
@@ -226,6 +233,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
226
233
227
234
// validate the algorithm
228
235
alg = jwt_get_alg (jwt );
236
+
229
237
if (alg != JWT_ALG_HS256 && alg != JWT_ALG_RS256 )
230
238
{
231
239
ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "invalid algorithm in jwt %d" , alg );
@@ -235,45 +243,51 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
235
243
// validate the exp date of the JWT
236
244
exp = (time_t )jwt_get_grant_int (jwt , "exp" );
237
245
now = time (NULL );
246
+
238
247
if (exp < now )
239
248
{
240
249
ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "the jwt has expired" );
241
250
goto redirect ;
242
251
}
243
252
244
253
// extract the userid
245
- sub = jwt_get_grant (jwt , "sub" );
246
- if (sub == NULL )
254
+ if (jwtcf -> auth_jwt_extract_sub == 1 )
247
255
{
248
- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "the jwt does not contain a subject" );
249
- }
250
- else
251
- {
252
- sub_t = ngx_char_ptr_to_str_t (r -> pool , (char * )sub );
253
- set_custom_header_in_headers_out (r , & useridHeaderName , & sub_t );
256
+ const char * sub = jwt_get_grant (jwt , "sub" );
257
+
258
+ if (sub == NULL )
259
+ {
260
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "the jwt does not contain a subject" );
261
+ }
262
+ else
263
+ {
264
+ ngx_str_t sub_t = ngx_char_ptr_to_str_t (r -> pool , (char * )sub );
265
+
266
+ set_custom_header_in_headers_out (r , & useridHeaderName , & sub_t );
267
+ }
254
268
}
255
269
256
270
if (jwtcf -> auth_jwt_validate_email == 1 )
257
271
{
258
- email = jwt_get_grant (jwt , "emailAddress" );
272
+ const char * email = jwt_get_grant (jwt , "emailAddress" );
273
+
259
274
if (email == NULL )
260
275
{
261
276
ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "the jwt does not contain an email address" );
262
277
}
263
278
else
264
279
{
265
- email_t = ngx_char_ptr_to_str_t (r -> pool , (char * )email );
280
+ ngx_str_t email_t = ngx_char_ptr_to_str_t (r -> pool , (char * )email );
281
+
266
282
set_custom_header_in_headers_out (r , & emailHeaderName , & email_t );
267
283
}
268
284
}
269
285
270
286
jwt_free (jwt );
271
-
272
287
273
288
return NGX_OK ;
274
289
275
290
redirect :
276
-
277
291
if (jwt )
278
292
{
279
293
jwt_free (jwt );
@@ -303,7 +317,6 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
303
317
uintptr_t escaped_len ;
304
318
305
319
loginlen = jwtcf -> auth_jwt_loginurl .len ;
306
-
307
320
scheme = (r -> connection -> ssl ) ? "https" : "http" ;
308
321
server = r -> headers_in .server ;
309
322
@@ -318,15 +331,11 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
318
331
uri .data = ngx_palloc (r -> pool , request_uri_var -> len );
319
332
uri .len = request_uri_var -> len ;
320
333
ngx_memcpy (uri .data , request_uri_var -> data , request_uri_var -> len );
321
-
322
- // ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "found uri with querystring %s", ngx_str_t_to_char_ptr(r->pool, uri));
323
334
}
324
335
else
325
336
{
326
337
// fallback to the querystring without params
327
338
uri = r -> uri ;
328
-
329
- // ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "fallback to querystring without params");
330
339
}
331
340
332
341
// escape the URI
@@ -350,8 +359,6 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
350
359
ngx_memcpy (return_url + return_url_idx , uri_escaped .data , uri_escaped .len );
351
360
return_url_idx += uri_escaped .len ;
352
361
r -> headers_out .location -> value .data = (u_char * )return_url ;
353
-
354
- // ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "return_url: %s", ngx_str_t_to_char_ptr(r->pool, r->headers_out.location->value));
355
362
}
356
363
else
357
364
{
@@ -403,6 +410,7 @@ ngx_http_auth_jwt_create_loc_conf(ngx_conf_t *cf)
403
410
// set the flag to unset
404
411
conf -> auth_jwt_enabled = (ngx_flag_t ) - 1 ;
405
412
conf -> auth_jwt_redirect = (ngx_flag_t ) - 1 ;
413
+ conf -> auth_jwt_extract_sub = (ngx_flag_t ) - 1 ;
406
414
conf -> auth_jwt_validate_email = (ngx_flag_t ) - 1 ;
407
415
conf -> auth_jwt_use_keyfile = (ngx_flag_t ) - 1 ;
408
416
@@ -453,6 +461,7 @@ ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
453
461
ngx_conf_merge_str_value (conf -> auth_jwt_validation_type , prev -> auth_jwt_validation_type , "" );
454
462
ngx_conf_merge_str_value (conf -> auth_jwt_algorithm , prev -> auth_jwt_algorithm , "HS256" );
455
463
ngx_conf_merge_str_value (conf -> auth_jwt_keyfile_path , prev -> auth_jwt_keyfile_path , "" );
464
+ ngx_conf_merge_off_value (conf -> auth_jwt_extract_sub , prev -> auth_jwt_extract_sub , 1 );
456
465
ngx_conf_merge_off_value (conf -> auth_jwt_validate_email , prev -> auth_jwt_validate_email , 1 );
457
466
458
467
if (conf -> auth_jwt_enabled == ((ngx_flag_t ) - 1 ))
0 commit comments