Skip to content

Commit 2fdd613

Browse files
add option to not extract sub
1 parent 60b6f4b commit 2fdd613

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ which can be specified in on the `main` `server` or `___location` level.
4141
auth_jwt_key "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF"; # see docs for format based on algorithm
4242
auth_jwt_loginurl "https://yourdomain.com/loginpage";
4343
auth_jwt_enabled on;
44-
auth_jwt_algorithm HS256; # or RS256
44+
auth_jwt_algorithm HS256; # or RS256
45+
auth_jwt_extract_sub on; # or off
4546
auth_jwt_validate_email on; # or off
46-
auth_jwt_use_keyfile off; # or on
47+
auth_jwt_use_keyfile off; # or on
4748
auth_jwt_keyfile_path "/app/pub_key";
4849
```
4950

@@ -87,6 +88,13 @@ auth_jwt_validation_type COOKIE=rampartjwt;
8788
By default the authorization header is used to provide a JWT for validation.
8889
However, you may use the `auth_jwt_validation_type` configuration to specify the name of a cookie that provides the JWT.
8990

91+
```
92+
auth_jwt_extract_sub
93+
```
94+
By default, the module will attempt to extract the `sub` claim (e.g. the user's id) from the JWT. If successful, the
95+
value will be set in the `x-userid` HTTP header. An error will be logged if this option is enabled and the JWT does not
96+
contain the `sub` claim.
97+
9098
```
9199
auth_jwt_validate_email off;
92100
```

src/ngx_http_auth_jwt_module.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef struct {
2727
ngx_flag_t auth_jwt_redirect;
2828
ngx_str_t auth_jwt_validation_type;
2929
ngx_str_t auth_jwt_algorithm;
30+
ngx_flag_t auth_jwt_extract_sub;
3031
ngx_flag_t auth_jwt_validate_email;
3132
ngx_str_t auth_jwt_keyfile_path;
3233
ngx_flag_t auth_jwt_use_keyfile;
@@ -84,6 +85,13 @@ static ngx_command_t ngx_http_auth_jwt_commands[] = {
8485
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_algorithm),
8586
NULL },
8687

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+
8795
{ ngx_string("auth_jwt_validate_email"),
8896
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
8997
ngx_conf_set_flag_slot,
@@ -152,9 +160,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
152160
jwt_t *jwt = NULL;
153161
int jwtParseReturnCode;
154162
jwt_alg_t alg;
155-
const char* sub;
156163
const char* email;
157-
ngx_str_t sub_t;
158164
ngx_str_t email_t;
159165
time_t exp;
160166
time_t now;
@@ -242,15 +248,20 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
242248
}
243249

244250
// 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)
251252
{
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+
}
254265
}
255266

256267
if (jwtcf->auth_jwt_validate_email == 1)
@@ -403,6 +414,7 @@ ngx_http_auth_jwt_create_loc_conf(ngx_conf_t *cf)
403414
// set the flag to unset
404415
conf->auth_jwt_enabled = (ngx_flag_t) -1;
405416
conf->auth_jwt_redirect = (ngx_flag_t) -1;
417+
conf->auth_jwt_extract_sub = (ngx_flag_t) -1;
406418
conf->auth_jwt_validate_email = (ngx_flag_t) -1;
407419
conf->auth_jwt_use_keyfile = (ngx_flag_t) -1;
408420

@@ -453,6 +465,7 @@ ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
453465
ngx_conf_merge_str_value(conf->auth_jwt_validation_type, prev->auth_jwt_validation_type, "");
454466
ngx_conf_merge_str_value(conf->auth_jwt_algorithm, prev->auth_jwt_algorithm, "HS256");
455467
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);
456469
ngx_conf_merge_off_value(conf->auth_jwt_validate_email, prev->auth_jwt_validate_email, 1);
457470

458471
if (conf->auth_jwt_enabled == ((ngx_flag_t) -1))

0 commit comments

Comments
 (0)