Skip to content

Commit 32b5aa9

Browse files
authored
Merge pull request wselfjes#1 from wselfjes/feat/add-pipeline-params
Feat/add pipeline params
2 parents 829886f + 63237b4 commit 32b5aa9

File tree

4 files changed

+122
-7
lines changed

4 files changed

+122
-7
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ARG NGINX_VERSION
22

33

4-
FROM debian:bullseye-slim as BASE_IMAGE
4+
FROM debian:bullseye-slim as base
55
LABEL stage=builder
66
RUN apt-get update \
77
&& apt-get install -y curl build-essential
88

99

10-
FROM BASE_IMAGE as BUILD_IMAGE
10+
FROM base as build
1111
LABEL stage=builder
1212
ENV LD_LIBRARY_PATH=/usr/local/lib
1313
ARG NGINX_VERSION
@@ -38,4 +38,4 @@ RUN apt-get update \
3838

3939
LABEL stage=
4040
LABEL maintainer="TeslaGov" email="[email protected]"
41-
COPY --from=BUILD_IMAGE /root/build/nginx/objs/ngx_http_auth_jwt_module.so /usr/lib64/nginx/modules/
41+
COPY --from=build /root/build/nginx/objs/ngx_http_auth_jwt_module.so /usr/lib64/nginx/modules/

scripts.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ all() {
2020
fetch_headers() {
2121
printf "${BLUE} Fetching NGINX headers...${NC}"
2222
local files='src/core/ngx_core.h src/http/ngx_http.h'
23+
mkdir -p src/lib
2324

2425
for f in ${files}; do
2526
curl "https://raw.githubusercontent.com/nginx/nginx/release-${NGINX_VERSION}/${f}" -o src/lib/$(basename ${f})

src/ngx_http_auth_jwt_module.c

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ngx_http_auth_jwt_string.h"
2020

2121
#include <stdio.h>
22+
#include <string.h>
2223

2324
typedef struct {
2425
ngx_str_t auth_jwt_loginurl;
@@ -509,19 +510,44 @@ ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
509510

510511
static char * getJwt(ngx_http_request_t *r, ngx_str_t auth_jwt_validation_type)
511512
{
512-
static const ngx_str_t authorizationHeaderName = ngx_string("Authorization");
513+
ngx_str_t authorizationHeaderName = ngx_string("Authorization");
513514
ngx_table_elt_t *authorizationHeader;
514515
char* jwtPtr = NULL;
515516
ngx_str_t jwtCookieVal;
517+
ngx_str_t jwtQueryVal;
516518
ngx_int_t n;
517519
ngx_int_t bearer_length;
518520
ngx_str_t authorizationHeaderStr;
519521

520522
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "auth_jwt_validation_type.len %d", auth_jwt_validation_type.len);
523+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "auth_jwt_validation_type: %s", auth_jwt_validation_type.data);
521524

522-
if (auth_jwt_validation_type.len == 0 || (auth_jwt_validation_type.len == sizeof("AUTHORIZATION") - 1 && ngx_strncmp(auth_jwt_validation_type.data, "AUTHORIZATION", sizeof("AUTHORIZATION") - 1)==0))
525+
if (auth_jwt_validation_type.len == 0 || (auth_jwt_validation_type.len == sizeof("AUTHORIZATION=") - 1 && ngx_strncmp(auth_jwt_validation_type.data, "AUTHORIZATION=", sizeof("AUTHORIZATION=") - 1)==0))
523526
{
524-
// using authorization header
527+
auth_jwt_validation_type.data += sizeof("AUTHORIZATION=");
528+
auth_jwt_validation_type.len -= sizeof("AUTHORIZATION=");
529+
530+
authorizationHeader = search_headers_in(r, authorizationHeaderName.data, authorizationHeaderName.len);
531+
if (authorizationHeader != NULL)
532+
{
533+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Found authorization header len %d", authorizationHeader->value.len);
534+
535+
bearer_length = authorizationHeader->value.len - (auth_jwt_validation_type.len);
536+
537+
if (bearer_length > 0)
538+
{
539+
authorizationHeaderStr.data = authorizationHeader->value.data + auth_jwt_validation_type.len - 1;
540+
authorizationHeaderStr.len = bearer_length;
541+
542+
jwtPtr = ngx_str_t_to_char_ptr(r->pool, authorizationHeaderStr);
543+
544+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Authorization header: %s", jwtPtr);
545+
}
546+
}
547+
}
548+
else if (auth_jwt_validation_type.len == 0 || (auth_jwt_validation_type.len == sizeof("AUTHORIZATION") - 1 && ngx_strncmp(auth_jwt_validation_type.data, "AUTHORIZATION", sizeof("AUTHORIZATION") - 1)==0))
549+
{
550+
// using authorization header
525551
authorizationHeader = search_headers_in(r, authorizationHeaderName.data, authorizationHeaderName.len);
526552
if (authorizationHeader != NULL)
527553
{
@@ -540,6 +566,22 @@ static char * getJwt(ngx_http_request_t *r, ngx_str_t auth_jwt_validation_type)
540566
}
541567
}
542568
}
569+
else if (auth_jwt_validation_type.len > sizeof("QUERY=") && ngx_strncmp(auth_jwt_validation_type.data, "QUERY=", sizeof("QUERY=") - 1)==0)
570+
{
571+
auth_jwt_validation_type.data += sizeof("QUERY=") - 1;
572+
auth_jwt_validation_type.len -= sizeof("QUERY=") - 1;
573+
574+
// get the value from query
575+
n = ngx_http_arg(r, auth_jwt_validation_type.data, auth_jwt_validation_type.len, &jwtQueryVal);
576+
if (n != NGX_DECLINED)
577+
{
578+
jwtPtr = ngx_str_t_to_char_ptr(r->pool, jwtQueryVal);
579+
}
580+
else
581+
{
582+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "error get value from query param");
583+
}
584+
}
543585
else if (auth_jwt_validation_type.len > sizeof("COOKIE=") && ngx_strncmp(auth_jwt_validation_type.data, "COOKIE=", sizeof("COOKIE=") - 1)==0)
544586
{
545587
auth_jwt_validation_type.data += sizeof("COOKIE=") - 1;
@@ -553,6 +595,78 @@ static char * getJwt(ngx_http_request_t *r, ngx_str_t auth_jwt_validation_type)
553595
jwtPtr = ngx_str_t_to_char_ptr(r->pool, jwtCookieVal);
554596
}
555597
}
598+
else if (auth_jwt_validation_type.len > sizeof("PIPELINE=") && ngx_strncmp(auth_jwt_validation_type.data, "PIPELINE=", sizeof("PIPELINE=") - 1)==0)
599+
{
600+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "auth_jwt_validation_type: %s", auth_jwt_validation_type.data);
601+
// get the value from header first
602+
// than get value from cookie
603+
// and from query
604+
// sepparator is a comma ','
605+
// example PIPELINE=Bearer,session,token
606+
auth_jwt_validation_type.data += sizeof("PIPELINE=") - 1;
607+
auth_jwt_validation_type.len -= sizeof("PIPELINE=") - 1;
608+
609+
// auth_jwt_validation_type -> Bearer,token,session
610+
ngx_str_t pipeline_values[3];
611+
for (int i=0;i<3;i++)
612+
{
613+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "step: %d, auth_jwt_validation_type: %s", i, auth_jwt_validation_type.data);
614+
size_t j=0;
615+
for (;j<auth_jwt_validation_type.len;j++)
616+
{
617+
if(auth_jwt_validation_type.data[j] == ',' || auth_jwt_validation_type.data[j] == '\0')
618+
{
619+
pipeline_values[i].data = ngx_palloc(r->pool, j+1);
620+
pipeline_values[i].len = j;
621+
ngx_memcpy(pipeline_values[i].data, auth_jwt_validation_type.data, j);
622+
pipeline_values[i].data[j] = '\0';
623+
auth_jwt_validation_type.data += j + 1;
624+
auth_jwt_validation_type.len -= j;
625+
break;
626+
}
627+
}
628+
629+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "j: %d and len: %d", j, auth_jwt_validation_type.len);
630+
631+
}
632+
633+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "pipeline_values0: %s", pipeline_values[0].data);
634+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "pipeline_values1: %s", pipeline_values[1].data);
635+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "pipeline_values2: %s", pipeline_values[2].data);
636+
637+
// get auth header
638+
authorizationHeader = search_headers_in(r, authorizationHeaderName.data, authorizationHeaderName.len);
639+
if (authorizationHeader != NULL)
640+
{
641+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Found authorization header len %d", authorizationHeader->value.len);
642+
643+
bearer_length = authorizationHeader->value.len - pipeline_values[0].len - 1;
644+
645+
if (bearer_length > 0)
646+
{
647+
authorizationHeaderStr.data = authorizationHeader->value.data + pipeline_values[0].len + 1;
648+
authorizationHeaderStr.len = bearer_length;
649+
650+
jwtPtr = ngx_str_t_to_char_ptr(r->pool, authorizationHeaderStr);
651+
652+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Authorization header: %s", jwtPtr);
653+
}
654+
}
655+
656+
// get the cookie
657+
n = ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &pipeline_values[1], &jwtCookieVal);
658+
if (n != NGX_DECLINED)
659+
{
660+
jwtPtr = ngx_str_t_to_char_ptr(r->pool, jwtCookieVal);
661+
}
662+
663+
// get the value from query
664+
n = ngx_http_arg(r, pipeline_values[2].data, pipeline_values[2].len, &jwtQueryVal);
665+
if (n != NGX_DECLINED)
666+
{
667+
jwtPtr = ngx_str_t_to_char_ptr(r->pool, jwtQueryVal);
668+
}
669+
}
556670

557671
return jwtPtr;
558672
}

test/docker-compose-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
args:
1111
BASE_IMAGE: ${FULL_IMAGE_NAME}:${NGINX_VERSION:-latest}
1212
logging:
13-
driver: ${LOG_DRIVER:-journald}
13+
driver: ${LOG_DRIVER:-}
1414

1515
runner:
1616
container_name: ${CONTAINER_NAME_PREFIX}-runner

0 commit comments

Comments
 (0)