Skip to content

Commit 1439114

Browse files
committed
optionally redirect or return unauthorized
1 parent aba59c3 commit 1439114

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

resources/test-jwt-nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ server {
22
auth_jwt_key "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF";
33
auth_jwt_loginurl "https://teslagov.com";
44
auth_jwt_enabled off;
5+
auth_jwt_redirect on;
56

67
listen 8000;
78
server_name localhost;

src/ngx_http_auth_jwt_module.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef struct {
1414
ngx_str_t auth_jwt_loginurl;
1515
ngx_str_t auth_jwt_key;
1616
ngx_flag_t auth_jwt_enabled;
17+
ngx_flag_t auth_jwt_redirect;
1718
} ngx_http_auth_jwt_loc_conf_t;
1819

1920
static ngx_int_t ngx_http_auth_jwt_init(ngx_conf_t *cf);
@@ -48,6 +49,13 @@ static ngx_command_t ngx_http_auth_jwt_commands[] = {
4849
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_enabled),
4950
NULL },
5051

52+
{ ngx_string("auth_jwt_redirect"),
53+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
54+
ngx_conf_set_flag_slot,
55+
NGX_HTTP_LOC_CONF_OFFSET,
56+
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_redirect),
57+
NULL },
58+
5159
ngx_null_command
5260
};
5361

@@ -272,7 +280,14 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
272280
r->headers_out.___location->value.data = jwtcf->auth_jwt_loginurl.data;
273281
}
274282

275-
return NGX_HTTP_MOVED_TEMPORARILY;
283+
if (jwtcf->auth_jwt_redirect)
284+
{
285+
return NGX_HTTP_MOVED_TEMPORARILY;
286+
}
287+
else
288+
{
289+
return NGX_HTTP_UNAUTHORIZED;
290+
}
276291
}
277292

278293

@@ -308,6 +323,7 @@ ngx_http_auth_jwt_create_loc_conf(ngx_conf_t *cf)
308323

309324
// set the flag to unset
310325
conf->auth_jwt_enabled = (ngx_flag_t) -1;
326+
conf->auth_jwt_redirect = (ngx_flag_t) -1;
311327

312328
ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, "Created Location Configuration");
313329

@@ -324,11 +340,15 @@ ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
324340
ngx_conf_merge_str_value(conf->auth_jwt_loginurl, prev->auth_jwt_loginurl, "");
325341
ngx_conf_merge_str_value(conf->auth_jwt_key, prev->auth_jwt_key, "");
326342

327-
328343
if (conf->auth_jwt_enabled == ((ngx_flag_t) -1))
329344
{
330345
conf->auth_jwt_enabled = (prev->auth_jwt_enabled == ((ngx_flag_t) -1)) ? 0 : prev->auth_jwt_enabled;
331346
}
347+
348+
if (conf->auth_jwt_redirect == ((ngx_flag_t) -1))
349+
{
350+
conf->auth_jwt_redirect = (prev->auth_jwt_redirect == ((ngx_flag_t) -1)) ? 0 : prev->auth_jwt_redirect;
351+
}
332352

333353
ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, "Merged Location Configuration");
334354

0 commit comments

Comments
 (0)