@@ -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,9 +160,7 @@ 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
163
const char * email ;
157
- ngx_str_t sub_t ;
158
164
ngx_str_t email_t ;
159
165
time_t exp ;
160
166
time_t now ;
@@ -242,15 +248,20 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
242
248
}
243
249
244
250
// extract the userid
245
- sub = jwt_get_grant (jwt , "sub" );
246
- if (sub == NULL )
247
- {
248
- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "the jwt does not contain a subject" );
249
- }
250
- else
251
+ if (jwtcf -> auth_jwt_extract_sub == 1 )
251
252
{
252
- sub_t = ngx_char_ptr_to_str_t (r -> pool , (char * )sub );
253
- set_custom_header_in_headers_out (r , & useridHeaderName , & sub_t );
253
+ const char * sub = jwt_get_grant (jwt , "sub" );
254
+
255
+ if (sub == NULL )
256
+ {
257
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "the jwt does not contain a subject" );
258
+ }
259
+ else
260
+ {
261
+ ngx_str_t sub_t = ngx_char_ptr_to_str_t (r -> pool , (char * )sub );
262
+
263
+ set_custom_header_in_headers_out (r , & useridHeaderName , & sub_t );
264
+ }
254
265
}
255
266
256
267
if (jwtcf -> auth_jwt_validate_email == 1 )
@@ -403,6 +414,7 @@ ngx_http_auth_jwt_create_loc_conf(ngx_conf_t *cf)
403
414
// set the flag to unset
404
415
conf -> auth_jwt_enabled = (ngx_flag_t ) - 1 ;
405
416
conf -> auth_jwt_redirect = (ngx_flag_t ) - 1 ;
417
+ conf -> auth_jwt_extract_sub = (ngx_flag_t ) - 1 ;
406
418
conf -> auth_jwt_validate_email = (ngx_flag_t ) - 1 ;
407
419
conf -> auth_jwt_use_keyfile = (ngx_flag_t ) - 1 ;
408
420
@@ -453,6 +465,7 @@ ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
453
465
ngx_conf_merge_str_value (conf -> auth_jwt_validation_type , prev -> auth_jwt_validation_type , "" );
454
466
ngx_conf_merge_str_value (conf -> auth_jwt_algorithm , prev -> auth_jwt_algorithm , "HS256" );
455
467
ngx_conf_merge_str_value (conf -> auth_jwt_keyfile_path , prev -> auth_jwt_keyfile_path , "" );
468
+ ngx_conf_merge_off_value (conf -> auth_jwt_extract_sub , prev -> auth_jwt_extract_sub , 1 );
456
469
ngx_conf_merge_off_value (conf -> auth_jwt_validate_email , prev -> auth_jwt_validate_email , 1 );
457
470
458
471
if (conf -> auth_jwt_enabled == ((ngx_flag_t ) - 1 ))
0 commit comments