Skip to content

Commit 066d261

Browse files
rename extract_claims to extract_var_claims for clarity
1 parent ef5d253 commit 066d261

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ This module requires several new `nginx.conf` directives, which can be specified
2323
| `auth_jwt_algorithm` | The algorithm to use. One of: HS256, HS384, HS512, RS256, RS384, RS512 |
2424
| `auth_jwt_location` | Indicates where the JWT is located in the request -- see below. |
2525
| `auth_jwt_validate_sub` | Set to "on" to validate the `sub` claim (e.g. user id) in the JWT. |
26-
| `auth_jwt_extract_claims` | Set to a space-delimited list of claims to extract from the JWT and make available as NGINX variables. These will be accessible via e.g: `$jwt_claim_sub` |
26+
| `auth_jwt_extract_var_claims` | Set to a space-delimited list of claims to extract from the JWT and make available as NGINX variables. These will be accessible via e.g: `$jwt_claim_sub` |
2727
| `auth_jwt_extract_request_claims` | Set to a space-delimited list of claims to extract from the JWT and set as request headers. These will be accessible via e.g: `$http_jwt_sub` |
2828
| `auth_jwt_extract_response_claims` | Set to a space-delimited list of claims to extract from the JWT and set as response headers. These will be accessible via e.g: `$sent_http_jwt_sub` |
2929
| `auth_jwt_use_keyfile` | Set to "on" to read the key from a file rather than from the `auth_jwt_key` directive. |
@@ -93,7 +93,7 @@ auth_jwt_validate_sub on;
9393

9494
You may specify claims to be extracted from the JWT and placed on the request and/or response headers. This is especially handly because the claims will then also be available as NGINX variables.
9595

96-
If you only wish to access a claim as an NGINX variable, you should use `auth_jwt_extract_claims` so that the claim does not end up being sent to the client as a response header. However, if you do want the claim to be sent to the client in the response, you may use `auth_jwt_extract_response_claims` instead.
96+
If you only wish to access a claim as an NGINX variable, you should use `auth_jwt_extract_var_claims` so that the claim does not end up being sent to the client as a response header. However, if you do want the claim to be sent to the client in the response, you may use `auth_jwt_extract_response_claims` instead.
9797

9898
_Please note that `number`, `boolean`, `array`, and `object` claims are not supported at this time -- only `string` claims are supported._ An error will be thrown if you attempt to extract a non-string claim.
9999

@@ -103,7 +103,7 @@ For example, you could configure an NGINX location which redirects to the curren
103103

104104
```nginx
105105
location /profile/me {
106-
auth_jwt_extract_claims sub;
106+
auth_jwt_extract_var_claims sub;
107107
108108
return 301 /profile/$jwt_claim_sub;
109109
}

src/ngx_http_auth_jwt_module.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef struct
3131
ngx_str_t jwt_location;
3232
ngx_str_t algorithm;
3333
ngx_flag_t validate_sub;
34-
ngx_array_t *extract_claims;
34+
ngx_array_t *extract_var_claims;
3535
ngx_array_t *extract_request_claims;
3636
ngx_array_t *extract_response_claims;
3737
ngx_str_t keyfile_path;
@@ -117,11 +117,11 @@ static ngx_command_t auth_jwt_directives[] = {
117117
offsetof(auth_jwt_conf_t, validate_sub),
118118
NULL},
119119

120-
{ngx_string("auth_jwt_extract_claims"),
120+
{ngx_string("auth_jwt_extract_var_claims"),
121121
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_1MORE,
122122
merge_extract_var_claims,
123123
NGX_HTTP_LOC_CONF_OFFSET,
124-
offsetof(auth_jwt_conf_t, extract_claims),
124+
offsetof(auth_jwt_conf_t, extract_var_claims),
125125
NULL},
126126

127127
{ngx_string("auth_jwt_extract_request_claims"),
@@ -212,7 +212,7 @@ static void *create_conf(ngx_conf_t *cf)
212212
conf->validate_sub = NGX_CONF_UNSET;
213213
conf->redirect = NGX_CONF_UNSET;
214214
conf->validate_sub = NGX_CONF_UNSET;
215-
conf->extract_claims = NULL;
215+
conf->extract_var_claims = NULL;
216216
conf->extract_request_claims = NULL;
217217
conf->extract_response_claims = NULL;
218218
conf->use_keyfile = NGX_CONF_UNSET;
@@ -232,7 +232,7 @@ static char *merge_conf(ngx_conf_t *cf, void *parent, void *child)
232232
ngx_conf_merge_str_value(conf->algorithm, prev->algorithm, "HS256");
233233
ngx_conf_merge_str_value(conf->keyfile_path, prev->keyfile_path, "");
234234
ngx_conf_merge_off_value(conf->validate_sub, prev->validate_sub, 0);
235-
merge_array(cf->pool, &conf->extract_claims, prev->extract_claims, sizeof(ngx_str_t));
235+
merge_array(cf->pool, &conf->extract_var_claims, prev->extract_var_claims, sizeof(ngx_str_t));
236236
merge_array(cf->pool, &conf->extract_request_claims, prev->extract_request_claims, sizeof(ngx_str_t));
237237
merge_array(cf->pool, &conf->extract_response_claims, prev->extract_response_claims, sizeof(ngx_str_t));
238238

@@ -275,17 +275,17 @@ static char *merge_conf(ngx_conf_t *cf, void *parent, void *child)
275275
static char *merge_extract_var_claims(ngx_conf_t *cf, ngx_command_t *cmd, void *c)
276276
{
277277
auth_jwt_conf_t *conf = c;
278-
ngx_array_t *claims = conf->extract_claims;
278+
ngx_array_t *claims = conf->extract_var_claims;
279279

280280
if (claims == NULL)
281281
{
282282
claims = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
283-
conf->extract_claims = claims;
283+
conf->extract_var_claims = claims;
284284
}
285285

286286
ngx_str_t *values = cf->args->elts;
287287

288-
// start at 1 because the first element is the directive (auth_jwt_extract_claims)
288+
// start at 1 because the first element is the directive (auth_jwt_extract_var_claims)
289289
for (ngx_uint_t i = 1; i < cf->args->nelts; ++i)
290290
{
291291
// add this claim's name to the config struct
@@ -427,29 +427,30 @@ static auth_jwt_ctx_t *get_or_init_jwt_module_ctx(ngx_http_request_t *r, auth_jw
427427
}
428428
else
429429
{
430-
// context does not yet exist, so let's create one, initialize it, and set it
431430
ctx = ngx_pcalloc(r->pool, sizeof(auth_jwt_ctx_t));
432-
431+
433432
if (ctx == NULL)
434433
{
435434
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "error allocating jwt module context");
436435
return ctx;
437436
}
438-
else if (jwtcf->extract_claims != NULL)
439-
{
440-
ctx->claim_values = ngx_array_create(r->pool, jwtcf->extract_claims->nelts, sizeof(ngx_str_t));
441-
442-
if (ctx->claim_values == NULL)
437+
else {
438+
if (jwtcf->extract_var_claims != NULL)
443439
{
444-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "error initializing jwt module context");
445-
return NULL;
440+
ctx->claim_values = ngx_array_create(r->pool, jwtcf->extract_var_claims->nelts, sizeof(ngx_str_t));
441+
442+
if (ctx->claim_values == NULL)
443+
{
444+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "error initializing jwt module context");
445+
return NULL;
446+
}
446447
}
448+
449+
ctx->validation_status = NGX_AGAIN;
450+
ngx_http_set_ctx(r, ctx, ngx_http_auth_jwt_module);
451+
452+
return ctx;
447453
}
448-
449-
ctx->validation_status = NGX_AGAIN;
450-
ngx_http_set_ctx(r, ctx, ngx_http_auth_jwt_module);
451-
452-
return ctx;
453454
}
454455
}
455456

@@ -623,7 +624,7 @@ static int validate_sub(auth_jwt_conf_t *jwtcf, jwt_t *jwt)
623624

624625
static ngx_int_t extract_var_claims(ngx_http_request_t *r, auth_jwt_conf_t *jwtcf, jwt_t *jwt, auth_jwt_ctx_t *ctx)
625626
{
626-
ngx_array_t *claims = jwtcf->extract_claims;
627+
ngx_array_t *claims = jwtcf->extract_var_claims;
627628

628629
if (claims == NULL || claims->nelts == 0)
629630
{
@@ -636,17 +637,16 @@ static ngx_int_t extract_var_claims(ngx_http_request_t *r, auth_jwt_conf_t *jwtc
636637
for (uint i = 0; i < claims->nelts; ++i)
637638
{
638639
const ngx_str_t claim = claimsPtr[i];
639-
const char *value = jwt_get_grant(jwt, (char *)claim.data);
640-
641-
ngx_str_t nsval = ngx_string("");
640+
const char *claimValue = jwt_get_grant(jwt, (char *)claim.data);
641+
ngx_str_t value = ngx_string("");
642642

643-
if (value != NULL && strlen(value) > 0)
643+
if (claimValue != NULL && strlen(claimValue) > 0)
644644
{
645-
nsval = char_ptr_to_ngx_str_t(r->pool, value);
645+
value = char_ptr_to_ngx_str_t(r->pool, claimValue);
646646
}
647647

648-
((ngx_str_t*) ctx->claim_values->elts)[i] = nsval;
649-
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "set jwt var %V to value %s", &claim, nsval.data);
648+
((ngx_str_t*) ctx->claim_values->elts)[i] = value;
649+
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "set var %V to JWT claim value %s", &claim, value.data);
650650
}
651651

652652
return NGX_OK;

test/etc/nginx/conf.d/test.conf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ vXjq39xtcIBRTO1c2zs=
400400
auth_jwt_enabled on;
401401
auth_jwt_redirect off;
402402
auth_jwt_location HEADER=Authorization;
403-
auth_jwt_extract_claims sub;
403+
auth_jwt_extract_var_claims sub;
404404

405405
if ($jwt_claim_sub = 'some-long-uuid') {
406406
return 200;
@@ -412,7 +412,7 @@ vXjq39xtcIBRTO1c2zs=
412412
auth_jwt_enabled on;
413413
auth_jwt_redirect off;
414414
auth_jwt_location HEADER=Authorization;
415-
auth_jwt_extract_claims sub;
415+
auth_jwt_extract_var_claims sub;
416416

417417
return 200 "sub: $jwt_claim_sub";
418418
}
@@ -422,7 +422,7 @@ vXjq39xtcIBRTO1c2zs=
422422
auth_jwt_redirect off;
423423
auth_jwt_location HEADER=Authorization;
424424
auth_jwt_validate_sub on;
425-
auth_jwt_extract_claims firstName middleName lastName;
425+
auth_jwt_extract_var_claims firstName middleName lastName;
426426

427427
return 200 "you are: $jwt_claim_firstName $jwt_claim_middleName $jwt_claim_lastName";
428428
}
@@ -434,7 +434,7 @@ vXjq39xtcIBRTO1c2zs=
434434
auth_jwt_validate_sub on;
435435

436436
location /profile/me {
437-
auth_jwt_extract_claims sub;
437+
auth_jwt_extract_var_claims sub;
438438

439439
return 301 /profile/$jwt_claim_sub;
440440
}

0 commit comments

Comments
 (0)