Skip to content

Commit 2be3681

Browse files
authored
Merge pull request TeslaGov#15 from TeslaGov/joefitz/optionally-redirect
Joefitz/optionally redirect
2 parents aba59c3 + 20026b6 commit 2be3681

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

build.sh

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,23 @@ else
2727
echo -e "${RED}Secure test without jwt fail ${TEST_SECURE_EXPECT_302}${NONE}";
2828
fi
2929

30-
TEST_SECURE_EXPECT_200=`curl -X GET -o /dev/null --silent --head --write-out '%{http_code}\n' http://${MACHINE_IP}:8000/secure/index.html -H 'cache-control: no-cache' --cookie "rampartjwt=${VALIDJWT}"`
31-
if [ "$TEST_SECURE_EXPECT_200" -eq "200" ];then
32-
echo -e "${GREEN}Secure test with jwt pass ${TEST_SECURE_EXPECT_200}${NONE}";
30+
TEST_SECURE_COOKIE_EXPECT_200=`curl -X GET -o /dev/null --silent --head --write-out '%{http_code}\n' http://${MACHINE_IP}:8000/secure/index.html -H 'cache-control: no-cache' --cookie "rampartjwt=${VALIDJWT}"`
31+
if [ "$TEST_SECURE_COOKIE_EXPECT_200" -eq "200" ];then
32+
echo -e "${GREEN}Secure test with jwt pass ${TEST_SECURE_COOKIE_EXPECT_200}${NONE}";
3333
else
34-
echo -e "${RED}Secure test with jwt fail ${TEST_SECURE_EXPECT_200}${NONE}";
34+
echo -e "${RED}Secure test with jwt fail ${TEST_SECURE_COOKIE_EXPECT_200}${NONE}";
3535
fi
3636

37-
TEST_SECURE_EXPECT_200=`curl -X GET -o /dev/null --silent --head --write-out '%{http_code}\n' http://${MACHINE_IP}:8000/secure/index.html -H 'cache-control: no-cache' --header "Authorization: Bearer ${VALIDJWT}" --cookie "rampartjwt=${VALIDJWT}"`
38-
if [ "$TEST_SECURE_EXPECT_200" -eq "200" ];then
39-
echo -e "${GREEN}Secure test with jwt and auth header pass ${TEST_SECURE_EXPECT_200}${NONE}";
37+
TEST_SECURE_HEADER_EXPECT_200=`curl -X GET -o /dev/null --silent --head --write-out '%{http_code}\n' http://${MACHINE_IP}:8000/secure/index.html -H 'cache-control: no-cache' --header "Authorization: Bearer ${VALIDJWT}" --cookie "rampartjwt=${VALIDJWT}"`
38+
if [ "$TEST_SECURE_HEADER_EXPECT_200" -eq "200" ];then
39+
echo -e "${GREEN}Secure test with jwt and auth header pass ${TEST_SECURE_HEADER_EXPECT_200}${NONE}";
4040
else
41-
echo -e "${RED}Secure test with jwt and auth header fail ${TEST_SECURE_EXPECT_200}${NONE}";
41+
echo -e "${RED}Secure test with jwt and auth header fail ${TEST_SECURE_HEADER_EXPECT_200}${NONE}";
42+
fi
43+
44+
TEST_SECURE_NO_REDIRECT_EXPECT_401=`curl -X GET -o /dev/null --silent --head --write-out '%{http_code}\n' http://${MACHINE_IP}:8000/secure-no-redirect/index.html`
45+
if [ "$TEST_SECURE_NO_REDIRECT_EXPECT_401" -eq "401" ];then
46+
echo -e "${GREEN}Secure test without jwt no redirect pass ${TEST_SECURE_NO_REDIRECT_EXPECT_401}${NONE}";
47+
else
48+
echo -e "${RED}Secure test without jwt no redirect fail ${TEST_SECURE_NO_REDIRECT_EXPECT_401}${NONE}";
4249
fi

resources/test-jwt-nginx.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ 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;
89

10+
___location ~ ^/secure-no-redirect/ {
11+
auth_jwt_enabled on;
12+
auth_jwt_redirect off;
13+
alias /usr/share/nginx/secure/;
14+
}
15+
916
___location ~ ^/secure/ {
1017
auth_jwt_enabled on;
1118
root /usr/share/nginx;

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)