@@ -424,7 +424,7 @@ static ngx_int_t handler(ngx_http_request_t *request)
424424 introspection_request -> request_body = introspection_request_body ;
425425 introspection_request -> headers_in .content_length_n = ngx_buf_size (introspection_request_body_buffer );
426426
427- #if (NGX_HTTP_HEADERS )
427+ #if (NGX_HTTP_HEADERS )
428428 if (request -> headers_in .accept == NULL )
429429 {
430430 ngx_int_t result ;
@@ -628,54 +628,68 @@ static ngx_int_t introspection_response_handler(ngx_http_request_t *request, voi
628628 return introspection_subrequest_status_code ;
629629 }
630630
631- // body parsing
632631 u_char * jwt_start = NULL ;
632+ ngx_str_t cache_data = ngx_null_string ;
633633
634+ #if (NGX_HTTP_CACHE )
634635 if (!request -> cache || !request -> cache -> buf )
635636 {
637+ // No cache; read JWT from response to sub-request
636638 jwt_start = request -> header_end + sizeof ("\r\n" ) - 1 ;
637639 }
638640
639641 if (jwt_start == NULL && request -> cache && request -> cache -> buf && request -> cache -> valid_sec > 0 )
640642 {
641- ngx_read_file ( & request -> cache -> file , request -> cache -> buf -> pos , request -> cache -> length , 0 );
643+ // Try to read JWT from cache
642644
643- jwt_start = request -> cache -> buf -> start + request -> cache -> body_start ;
645+ cache_data .len = request -> cache -> length ;
646+ cache_data .data = ngx_pnalloc (request -> pool , cache_data .len );
647+
648+ if (cache_data .data != NULL )
649+ {
650+ ngx_read_file (& request -> cache -> file , cache_data .data , cache_data .len , request -> cache -> body_start );
651+
652+ jwt_start = cache_data .data ;
653+ }
644654 }
645655
646656 if (jwt_start == NULL )
647657 {
648- ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , request -> connection -> log , 0 , "Failed to parse response" );
658+ ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , request -> connection -> log , 0 ,
659+ "Failed to obtain JWT from introspection response or, if applicable, cache" );
660+
649661 module_context -> done = 1 ;
650662 module_context -> status = NGX_HTTP_UNAUTHORIZED ;
651663
652664 return introspection_subrequest_status_code ;
653665 }
666+ #else
667+ jwt_start = request -> header_end + sizeof ("\r\n" ) - 1 ;
668+ #endif
669+
670+ size_t jwt_len = request -> headers_out .content_length_n ;
671+ size_t bearer_jwt_len = BEARER_SIZE + jwt_len ;
654672
655- u_char * jwt_end = jwt_start + request -> headers_out .content_length_n ;
673+ module_context -> jwt .len = bearer_jwt_len ;
674+ module_context -> jwt .data = ngx_pnalloc (request -> pool , bearer_jwt_len );
656675
657- if (jwt_end == NULL )
676+ if (module_context -> jwt . data == NULL )
658677 {
659- ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , request -> connection -> log , 0 , "Failed to parse response" );
660678 module_context -> done = 1 ;
661679 module_context -> status = NGX_HTTP_UNAUTHORIZED ;
662680
663681 return introspection_subrequest_status_code ;
664682 }
665683
666- module_context -> jwt .len = jwt_end - jwt_start + BEARER_SIZE ;
684+ u_char * p = ngx_copy ( module_context -> jwt .data , BEARER , BEARER_SIZE ) ;
667685
668- module_context -> jwt . data = ngx_pcalloc ( request -> pool , module_context -> jwt . len );
686+ ngx_memcpy ( p , jwt_start , jwt_len );
669687
670- if (module_context -> jwt . data == NULL )
688+ if (cache_data . len > 0 )
671689 {
672- return introspection_subrequest_status_code ;
690+ ngx_pfree ( request -> pool , cache_data . data ) ;
673691 }
674692
675- u_char * p = ngx_copy (module_context -> jwt .data , BEARER , BEARER_SIZE );
676-
677- ngx_memcpy (p , jwt_start , module_context -> jwt .len );
678-
679693 module_context -> done = 1 ;
680694
681695 return introspection_subrequest_status_code ;
0 commit comments