Skip to content

Commit 1ec288e

Browse files
authored
Merge pull request TeslaGov#1 from TeslaGov/joefitz/either-cookie
instead of requiring both cookies, either one will work
2 parents 7fc13a9 + 7af44b6 commit 1ec288e

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

src/ngx_http_auth_jwt_module.c

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static ngx_command_t ngx_http_auth_jwt_commands[] = {
3939
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_key),
4040
NULL },
4141

42-
{ ngx_string("auth_jwt_enabled"),
42+
{ ngx_string("auth_jwt_enabled"),
4343
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
4444
ngx_conf_set_flag_slot,
4545
NGX_HTTP_LOC_CONF_OFFSET,
@@ -85,6 +85,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
8585
{
8686
ngx_int_t n;
8787
ngx_str_t jwtCookieName = ngx_string("rampartjwt");
88+
ngx_str_t passportKeyCookieName = ngx_string("PassportKey");
8889
ngx_str_t jwtCookieVal;
8990
char* jwtCookieValChrPtr;
9091
ngx_http_auth_jwt_loc_conf_t *jwtcf;
@@ -94,16 +95,14 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
9495
jwt_alg_t alg;
9596
time_t exp;
9697
time_t now;
97-
ngx_str_t passportKeyCookieName = ngx_string("PassportKey");
98-
ngx_str_t passportKeyCookieVal;
9998

10099

101-
jwtcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_jwt_module);
100+
jwtcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_jwt_module);
102101

103102
if (!jwtcf->auth_jwt_enabled)
104103
{
105-
return NGX_DECLINED;
106-
}
104+
return NGX_DECLINED;
105+
}
107106

108107
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Key: %s, Enabled: %d",
109108
// jwtcf->auth_jwt_key.data,
@@ -114,9 +113,14 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
114113
n = ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &jwtCookieName, &jwtCookieVal);
115114
if (n == NGX_DECLINED)
116115
{
117-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to obtain rampartjwt cookie");
118-
goto redirect;
119-
}
116+
// if we can't find the first cookie, check the legacy ___location
117+
n = ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &passportKeyCookieName, &jwtCookieVal);
118+
if (n == NGX_DECLINED)
119+
{
120+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to obtain a jwt cookie");
121+
goto redirect;
122+
}
123+
}
120124

121125
// the cookie data is not necessarily null terminated... we need a null terminated character pointer
122126
jwtCookieValChrPtr = ngx_alloc(jwtCookieVal.len + 1, r->connection->log);
@@ -160,22 +164,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
160164
goto redirect;
161165
}
162166

163-
// ensure that the user has a matching PassportKey cookie.
164-
// this can be removed once we and our partners no longer use the PassportKey cookie
165-
n = ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &passportKeyCookieName, &passportKeyCookieVal);
166-
if (n == NGX_DECLINED) {
167-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to obtain passport cookie");
168-
goto redirect;
169-
};
170-
171-
// compare both cookies
172-
if (ngx_strncmp(jwtCookieVal.data, passportKeyCookieVal.data, jwtCookieVal.len))
173-
{
174-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "PassportKey cookie does not match rampartjwt cookie");
175-
goto redirect;
176-
}
177-
178-
return NGX_OK;
167+
return NGX_OK;
179168

180169
redirect:
181170
r->headers_out.___location = ngx_list_push(&r->headers_out.headers);
@@ -286,4 +275,4 @@ hex_to_binary( const char* str, u_char* buf, int len ) {
286275
*cpy++ = low | (high << 4);
287276
}
288277
return 0;
289-
}
278+
}

0 commit comments

Comments
 (0)