-
Notifications
You must be signed in to change notification settings - Fork 0
/
phantom_token.c
998 lines (823 loc) · 38 KB
/
phantom_token.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
/*
* Copyright 2017 Curity AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_string.h>
#include <stdbool.h>
#include <assert.h>
#define UNENCODED_CLIENT_CREDENTIALS_BUF_LEN 255
typedef struct
{
ngx_str_t base64encoded_client_credential;
ngx_str_t introspection_endpoint;
ngx_str_t realm;
ngx_array_t *scopes;
ngx_str_t space_separated_scopes;
ngx_flag_t enable;
} phantom_token_configuration_t;
typedef struct
{
ngx_uint_t done;
ngx_uint_t status;
ngx_str_t jwt;
ngx_str_t original_accept_header;
ngx_str_t original_content_type_header;
} phantom_token_module_context_t;
static ngx_int_t post_configuration(ngx_conf_t *config);
static ngx_int_t handler(ngx_http_request_t *request);
static void *create_location_configuration(ngx_conf_t *config);
static char *merge_location_configuration(ngx_conf_t *main_config, void *parent, void *child);
static ngx_int_t introspection_response_handler(ngx_http_request_t *request, void *data,
ngx_int_t introspection_subrequest_status_code);
static ngx_int_t write_error_response(ngx_http_request_t *request, ngx_int_t status, phantom_token_configuration_t *module_location_config);
/**
* Adds a WWW-Authenticate header to the given request's output headers that conforms to <a href="https://tools.ietf.org/html/rfc6750">RFC 6750</>.
*
* After calling this method, a WWW-Authenticate header will be added that uses the Bearer scheme. If the realm and or
* scopes were also configured, then these too will be included. For instance, if scopes are configured, then the
* following output header will be added: <code> WWW-Authenticate: Bearer scope="scope1 scope2 scope3"</code>. If only
* realm is configured, then a response header like this one would be added:
* <code>WWW-Authenticate: Bearer realm="myGoodRealm"</code>. If both are configured, the two will be included and
* separated by a comma, like this: <code>WWW-Authenticate: Bearer realm="myGoodRealm", scope="scope1 scope2 scope3"</code>.
*
* @param request the current request
* @param realm the configured realm
* @param space_separated_scopes the space-separated list of configured scopes
* @param error an error code or NULL if none. Refer to
* <a href="https://tools.ietf.org/html/rfc6750#section-3.1">RFC 6750 § 3.1</a> for standard values.
*
* @return <code>NGX_HTTP_UNAUTHORIZED</code>
*
* @example <code>WWW-Authenticate: Bearer realm="myGoodRealm", scope="scope1 scope2 scope3"</code>
*
* @see <a href="https://tools.ietf.org/html/rfc6750">RFC 6750</a>
*/
static ngx_int_t set_www_authenticate_header(ngx_http_request_t *request, phantom_token_configuration_t *module_location_config, char *error_code);
/**
* Sets the base-64-encoded client ID and secret in the module's configuration setting structure.
*
* This method assumes the module's command where this setter function (<code>set</code>) is used has a
* configuration (<code>conf</code>) of <code>NGX_HTTP_LOC_CONF_OFFSET<code> and an <code>offset</code> of
* <code>base64encoded_client_credential</code>. If this is not the case, the result pointer <em>may</em> point to an
* unexpected location and the handler may not be able to use the configured values. Also, the command should have a
* type that includes <code>NGX_CONF_TAKE2</code>.
*
* @param config_setting the configuration setting that is being set
* @param command the module's command where this slot setter function is being used
* @param result a pointer to the location where the result will be stored; it should be a pointer to a
* <code>ngx_str_t</code>.
*
* @return <code>NGX_CONF_OK</code> upon success; some other character string on failure.
*/
static char* set_client_credential_configuration_slot(ngx_conf_t *config_setting, ngx_command_t *command, void *result);
static const char BEARER[] = "Bearer ";
static const size_t BEARER_SIZE = sizeof(BEARER) - 1;
static ngx_command_t phantom_token_module_directives[] =
{
{
ngx_string("phantom_token"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(phantom_token_configuration_t, enable),
NULL
},
{
ngx_string("phantom_token_client_credential"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
set_client_credential_configuration_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(phantom_token_configuration_t, base64encoded_client_credential),
NULL
},
{
ngx_string("phantom_token_introspection_endpoint"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(phantom_token_configuration_t, introspection_endpoint),
NULL
},
{
ngx_string("phantom_token_realm"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(phantom_token_configuration_t, realm),
NULL
},
{
ngx_string("phantom_token_scopes"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(phantom_token_configuration_t, space_separated_scopes),
NULL
},
{
ngx_string("phantom_token_scope"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_array_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(phantom_token_configuration_t, scopes),
NULL
},
ngx_null_command /* command termination */
};
/* The module context. */
static ngx_http_module_t phantom_token_module_context =
{
NULL, /* pre-configuration */
post_configuration,
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
create_location_configuration,
merge_location_configuration
};
/* Module definition. */
ngx_module_t ngx_curity_http_phantom_token_module =
{
NGX_MODULE_V1,
&phantom_token_module_context,
phantom_token_module_directives,
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
/**
* Sets the request's Accept header to the given value.
*
* @param request the request to which the header value will be set
* @param value the value to set
* @return NGX_OK if no error has occurred; NGX_ERROR if an error occurs.
*/
static ngx_int_t set_accept_header_value(ngx_http_request_t *request, const char* value)
{
ngx_table_elt_t *accept_header;
accept_header = ngx_list_push(&request->headers_in.headers);
if (accept_header == NULL)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to set Accept header value");
return NGX_ERROR;
}
accept_header->hash = 1;
ngx_str_set(&accept_header->key, "Accept");
accept_header->value.len = ngx_strlen(value);
accept_header->value.data = (u_char *)value;
accept_header->lowcase_key = (u_char *)"accept";
request->headers_in.accept = accept_header;
request->headers_in.headers.part.nelts = request->headers_in.headers.last->nelts;
return NGX_OK;
}
static ngx_int_t handler(ngx_http_request_t *request)
{
phantom_token_configuration_t *module_location_config = ngx_http_get_module_loc_conf(
request, ngx_curity_http_phantom_token_module);
// Return OK if the module is not active
if (!module_location_config->enable)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "Module disabled");
return NGX_DECLINED;
}
// OPTIONS requests from SPAs can never contain an authorization header so return a standard 204
if (request->method == NGX_HTTP_OPTIONS)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "Not processing OPTIONS request");
return NGX_OK;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "Handling request to convert token to JWT");
if (module_location_config->base64encoded_client_credential.len == 0)
{
ngx_log_error(NGX_LOG_WARN, request->connection->log, 0,
"Module not configured properly: missing client ID and secret");
return NGX_DECLINED;
}
ngx_str_t encoded_client_credentials = module_location_config->base64encoded_client_credential;
if (module_location_config->introspection_endpoint.len == 0)
{
ngx_log_error(NGX_LOG_WARN, request->connection->log, 0,
"Module not configured properly: missing introspection endpoint");
return NGX_DECLINED;
}
phantom_token_module_context_t *module_context = ngx_http_get_module_ctx(request, ngx_curity_http_phantom_token_module);
if (module_context != NULL)
{
if (module_context->done)
{
// return appropriate status
if (module_context->status == NGX_HTTP_OK)
{
// Introspection was successful. Replace the incoming Authorization header with one that has the JWT.
request->headers_in.authorization->value.len = module_context->jwt.len;
request->headers_in.authorization->value.data = module_context->jwt.data;
if (module_context->original_content_type_header.data == NULL)
{
request->headers_in.headers.part.nelts = request->headers_in.headers.last->nelts = request->headers_in.headers.last->nelts - 1;
}
else
{
request->headers_in.content_type->value = module_context->original_content_type_header;
}
if (request->headers_in.accept == NULL)
{
ngx_int_t result;
if ((result = set_accept_header_value(request, "*/*") != NGX_OK))
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to set Accept header value");
return result;
}
}
else
{
request->headers_in.accept->value = module_context->original_accept_header;
}
return NGX_OK;
}
else if (module_context->status == NGX_HTTP_NO_CONTENT)
{
ngx_log_error(
NGX_LOG_ERR, request->connection->log, 0,
"Introspection request from %V failed with no content: %d",
&request->connection->addr_text, module_context->status);
return set_www_authenticate_header(request, module_location_config, NULL);
}
else if (module_context->status == NGX_HTTP_SERVICE_UNAVAILABLE)
{
ngx_log_error(
NGX_LOG_ERR, request->connection->log, 0,
"Introspection request failed with service unavailable: %d",
module_context->status);
return write_error_response(request, NGX_HTTP_SERVICE_UNAVAILABLE, module_location_config);
}
else if (module_context->status >= NGX_HTTP_INTERNAL_SERVER_ERROR || module_context->status == NGX_HTTP_NOT_FOUND
|| module_context->status == NGX_HTTP_UNAUTHORIZED || module_context->status == NGX_HTTP_FORBIDDEN)
{
ngx_log_error(
NGX_LOG_ERR, request->connection->log, 0,
"Introspection request from %V failed with status code "
"(server responded): %d",
&request->connection->addr_text, module_context->status);
return write_error_response(request, NGX_HTTP_BAD_GATEWAY, module_location_config);
}
ngx_log_error(
NGX_LOG_ERR, request->connection->log, 0,
"Introspection request from %V failed with status code "
"(unknown error, see nginx error_logs): %d",
&request->connection->addr_text, module_context->status);
return write_error_response(request, NGX_HTTP_INTERNAL_SERVER_ERROR, module_location_config);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, request->connection->log, 0,
"Called again without having received the response from Curity");
return NGX_AGAIN;
}
// return unauthorized when no Authorization header is present
if (!request->headers_in.authorization || request->headers_in.authorization->value.len <= 0)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Authorization header not present");
return set_www_authenticate_header(request, module_location_config, NULL);
}
u_char *bearer_token_pos;
if ((bearer_token_pos = ngx_strcasestrn((u_char*)request->headers_in.authorization->value.data,
(char*)BEARER, BEARER_SIZE - 1)) == NULL)
{
// return unauthorized when Authorization header is not Bearer
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Authorization header does not contain a bearer token");
return set_www_authenticate_header(request, module_location_config, NULL);
}
bearer_token_pos += BEARER_SIZE;
// Remove any extra whitespace after the "Bearer " part of the authorization request header
while (isspace(*bearer_token_pos))
{
bearer_token_pos++;
}
module_context = ngx_pcalloc(request->pool, sizeof(phantom_token_module_context_t));
if (module_context == NULL)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for module context");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_http_post_subrequest_t *introspection_request_callback = ngx_pcalloc(request->pool, sizeof(ngx_http_post_subrequest_t));
if (introspection_request_callback == NULL)
{
ngx_log_error(
NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for introspection request callback");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
introspection_request_callback->handler = introspection_response_handler;
introspection_request_callback->data = module_context;
ngx_http_request_t *introspection_request;
if (ngx_http_subrequest(request, &module_location_config->introspection_endpoint, NULL, &introspection_request,
introspection_request_callback, NGX_HTTP_SUBREQUEST_WAITED) != NGX_OK)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to create subrequest to introspection endpoint");
write_error_response(request, NGX_HTTP_INTERNAL_SERVER_ERROR, module_location_config);
}
// extract access token from header
u_char *introspect_body_data = ngx_pcalloc(request->pool, request->headers_in.authorization->value.len);
if (introspect_body_data == NULL)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for introspection body data");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_str_t *introspection_body = ngx_pcalloc(request->pool, sizeof(ngx_str_t));
if (introspection_body == NULL)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for introspection body");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_snprintf(introspect_body_data, request->headers_in.authorization->value.len, "token=%s", bearer_token_pos);
introspection_body->data = introspect_body_data;
introspection_body->len = ngx_strlen(introspection_body->data);
introspection_request->request_body = ngx_pcalloc(request->pool, sizeof(ngx_http_request_body_t));
if (introspection_request->request_body == NULL)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for introspection request");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_http_request_body_t *introspection_request_body = ngx_pcalloc(request->pool, sizeof(ngx_http_request_body_t));
if (introspection_request_body == NULL)
{
ngx_log_error(
NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for introspection request body");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_buf_t *introspection_request_body_buffer = ngx_calloc_buf(request->pool);
if (introspection_request_body_buffer == NULL)
{
ngx_log_error(
NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for introspection request body buffer");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
introspection_request_body_buffer->start = introspection_request_body_buffer->pos = introspection_body->data;
introspection_request_body_buffer->end = introspection_request_body_buffer->last = introspection_body->data +
introspection_body->len;
introspection_request_body_buffer->temporary = true;
introspection_request_body->bufs = ngx_alloc_chain_link(request->pool);
if (introspection_request_body->bufs == NULL)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for introspection request "
"body chain link");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
introspection_request_body->bufs->buf = introspection_request_body_buffer;
introspection_request_body->bufs->next = NULL;
introspection_request_body->buf = introspection_request_body_buffer;
introspection_request->request_body = introspection_request_body;
introspection_request->headers_in.content_length_n = ngx_buf_size(introspection_request_body_buffer);
#if (NGX_HTTP_HEADERS)
if (request->headers_in.accept == NULL)
{
ngx_int_t result;
if ((result = set_accept_header_value(introspection_request, "application/jwt") != NGX_OK))
{
return result;
}
}
else
{
module_context->original_accept_header = request->headers_in.accept->value;
}
ngx_str_set(&introspection_request->headers_in.accept->value, "application/jwt");
#endif
if (request->headers_in.content_type == NULL)
{
ngx_table_elt_t *content_type_header;
content_type_header = ngx_list_push(&introspection_request->headers_in.headers);
if (content_type_header == NULL)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to set Content-type header value");
return NGX_ERROR;
}
content_type_header->hash = 1;
ngx_str_set(&content_type_header->key, "Content-type");
ngx_str_set(&content_type_header->value, "application/x-www-form-urlencoded");
content_type_header->lowcase_key = (u_char *)"content-type";
introspection_request->headers_in.content_type = content_type_header;
introspection_request->headers_in.headers.part.nelts = introspection_request->headers_in.headers.last->nelts;
}
else
{
module_context->original_content_type_header = request->headers_in.content_type->value;
ngx_str_set(&request->headers_in.content_type->value, "application/x-www-form-urlencoded");
}
introspection_request->header_only = true;
// Change subrequest method to POST
introspection_request->method = NGX_HTTP_POST;
ngx_str_set(&introspection_request->method_name, "POST");
// set authorization credentials header to Basic base64encoded_client_credential
size_t authorization_header_data_len = encoded_client_credentials.len + sizeof("Basic ") - 1;
u_char *authorization_header_data = ngx_pcalloc(request->pool, authorization_header_data_len);
if (authorization_header_data == NULL)
{
ngx_log_error(
NGX_LOG_ERR, request->connection->log, 0,
"Failed to allocate memory for authorization header data");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_snprintf(authorization_header_data, authorization_header_data_len, "Basic %V", &encoded_client_credentials);
introspection_request->headers_in.authorization->value.data = authorization_header_data;
introspection_request->headers_in.authorization->value.len = authorization_header_data_len;
ngx_http_set_ctx(request, module_context, ngx_curity_http_phantom_token_module)
return NGX_AGAIN;
}
static ngx_int_t introspection_response_handler(ngx_http_request_t *request, void *data,
ngx_int_t introspection_subrequest_status_code)
{
phantom_token_module_context_t *module_context = (phantom_token_module_context_t*)data;
ngx_str_t cache_data = ngx_null_string;
u_char *jwt_start = NULL;
size_t jwt_len = 0;
size_t bearer_jwt_len = 0;
u_char *p = NULL;
size_t body_buffer_size = 0;
bool read_response = false;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "auth request done status = %d",
request->headers_out.status);
module_context->status = request->headers_out.status;
// fail early for not 200 response
if (request->headers_out.status != NGX_HTTP_OK)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Subrequest to %V failed with response code: %d",
&request->uri, request->headers_out.status);
module_context->done = 1;
return introspection_subrequest_status_code;
}
#if (NGX_HTTP_CACHE)
// When caching is enabled, the introspection response is read from the cache, including the first request with a new opaque access token
if (request->cache && !request->cache->buf)
{
// We have a cache but it's not primed
ngx_http_file_cache_open(request);
}
if (request->cache && request->cache->buf && request->cache->valid_sec > 0)
{
cache_data.len = request->cache->length;
cache_data.data = ngx_pnalloc(request->pool, cache_data.len);
if (cache_data.data != NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, request->connection->log, 0,
"Reading from cache");
ngx_read_file(&request->cache->file, cache_data.data, cache_data.len, request->cache->body_start);
jwt_start = cache_data.data;
}
}
else
{
read_response = true;
}
#else
read_response = true;
#endif
jwt_len = request->headers_out.content_length_n;
bearer_jwt_len = BEARER_SIZE + jwt_len;
// When caching is not used, the introspection response is read directly.
// This method only receives a single response buffer per introspection request, which needs to contain the full JWT.
if (read_response)
{
// We use proxy_pass to call the introspection endpoint, so the ngx_http_proxy_module provides the response to the subrequest.
// The response is returned as an upstream buffer that NGINX prepares when it calls ngx_http_parse_header_line.
// This sets request->header_end and points request->upstream->buffer.pos past headers to the body content.
// - https://github.com/nginx/nginx/blob/master/src/http/modules/ngx_http_proxy_module.c#L1905
// - https://github.com/nginx/nginx/blob/master/src/http/ngx_http_parse.c#L816
jwt_start = request->upstream->buffer.pos;
// With default configuration, the total buffer memory size is 4KB and the response header size might be 332 bytes.
// The ngx_buf_size macro returns the body size only: the size of the JWT or a partial size of the JWT, like 3764 bytes.
// If the JWT content length is greater than the body buffer size, we must avoid reading past the end of the buffer.
body_buffer_size = ngx_buf_size((
&request->upstream->buffer)); // Double parenthesis because of macro
// expansion issue for nginx 1.18.0.
if (jwt_len > body_buffer_size)
{
// The standard solution to truncated responses, commonly used for long headers, is to configure an increased proxy_buffer_size.
// The customer needs to configure a larger value for the introspection location, such as with the following configuration.
// For a large JWT you might then get a body_buffer_size of 6535, even though the total buffer memory size is 16KB.
//
// location curity {
// proxy_pass "http://localhost:8443/oauth/v2/oauth-introspect";
// proxy_buffer_size 16k;
// proxy_buffers 4 16k;
// }
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0, "The introspection response buffer is too small to contain the JWT: increase the proxy_buffer_size configuration setting");
module_context->done = 1;
module_context->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
return introspection_subrequest_status_code;
}
}
if (jwt_start == NULL)
{
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Failed to obtain JWT from introspection response or, if "
"applicable, cache");
module_context->done = 1;
module_context->status = NGX_HTTP_UNAUTHORIZED;
return introspection_subrequest_status_code;
}
module_context->jwt.len = bearer_jwt_len;
module_context->jwt.data = ngx_pnalloc(request->pool, bearer_jwt_len);
if (module_context->jwt.data == NULL)
{
module_context->done = 1;
module_context->status = NGX_HTTP_UNAUTHORIZED;
return introspection_subrequest_status_code;
}
p = ngx_copy(module_context->jwt.data, BEARER, BEARER_SIZE);
ngx_memcpy(p, jwt_start, jwt_len);
if (cache_data.len > 0)
{
ngx_pfree(request->pool, cache_data.data);
}
module_context->done = 1;
return introspection_subrequest_status_code;
}
static ngx_int_t post_configuration(ngx_conf_t *config)
{
ngx_http_core_main_conf_t *main_config = ngx_http_conf_get_module_main_conf(config, ngx_http_core_module);
ngx_http_handler_pt *h = ngx_array_push(&main_config->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL)
{
return NGX_ERROR;
}
*h = handler;
return NGX_OK;
}
static void *create_location_configuration(ngx_conf_t *config)
{
phantom_token_configuration_t *location_config = ngx_pcalloc(config->pool, sizeof(phantom_token_configuration_t));
if (location_config == NULL)
{
return NGX_CONF_ERROR;
}
location_config->enable = NGX_CONF_UNSET_UINT;
location_config->scopes = NGX_CONF_UNSET_PTR;
return location_config;
}
static char *merge_location_configuration(ngx_conf_t *main_config, void *parent, void *child)
{
phantom_token_configuration_t *parent_config = parent, *child_config = child;
ngx_conf_merge_off_value(child_config->enable, parent_config->enable, 0)
ngx_conf_merge_str_value(child_config->introspection_endpoint, parent_config->introspection_endpoint, "")
ngx_conf_merge_str_value(child_config->realm, parent_config->realm, "api")
ngx_conf_merge_ptr_value(child_config->scopes, parent_config->scopes, NULL)
ngx_conf_merge_str_value(child_config->space_separated_scopes, parent_config->space_separated_scopes, "")
ngx_conf_merge_str_value(child_config->base64encoded_client_credential,
parent_config->base64encoded_client_credential, "")
if (child_config->scopes != NULL && child_config->space_separated_scopes.len == 0)
{
// Flatten scopes into a space-separated list
ngx_str_t *scope = child_config->scopes->elts;
size_t space_separated_scopes_data_size = child_config->scopes->nelts;
ngx_uint_t i;
for (i = 0; i < child_config->scopes->nelts; i++)
{
space_separated_scopes_data_size += scope[i].len;
}
u_char *space_separated_scopes_data = ngx_pcalloc(main_config->pool, space_separated_scopes_data_size);
if (space_separated_scopes_data == NULL)
{
return NGX_CONF_ERROR;
}
u_char *p = space_separated_scopes_data;
for (i = 0; i < child_config->scopes->nelts; i++)
{
p = ngx_cpymem(p, scope[i].data, scope[i].len);
*p = ' ';
p++;
}
*(p - 1) = '\0';
child_config->space_separated_scopes.data = space_separated_scopes_data;
child_config->space_separated_scopes.len = ngx_strlen(space_separated_scopes_data);
assert(child_config->space_separated_scopes.len <= space_separated_scopes_data_size);
}
return NGX_CONF_OK;
}
static char* set_client_credential_configuration_slot(ngx_conf_t *config_setting, ngx_command_t *command, void *result)
{
ngx_str_t *base64encoded_client_credential = result;
ngx_str_t *args = config_setting->args->elts;
ngx_str_t client_id = args[1], client_secret = args[2]; // sub 0 is the directive itself
if (client_id.len > 0 && client_secret.len > 0)
{
u_char unencoded_client_credentials_data[UNENCODED_CLIENT_CREDENTIALS_BUF_LEN];
u_char *p = ngx_snprintf(unencoded_client_credentials_data, sizeof(unencoded_client_credentials_data), "%V:%V",
&client_id, &client_secret);
ngx_str_t unencoded_client_credentials = { p - unencoded_client_credentials_data,
unencoded_client_credentials_data };
base64encoded_client_credential->data = ngx_palloc(
config_setting->pool, ngx_base64_encoded_length(unencoded_client_credentials.len));
if (base64encoded_client_credential->data == NULL)
{
return NGX_CONF_ERROR;
}
ngx_encode_base64(base64encoded_client_credential, &unencoded_client_credentials);
return NGX_CONF_OK;
}
ngx_conf_log_error(NGX_LOG_EMERG, config_setting, 0, "invalid client ID and/or secret");
return "invalid_client_credential";
}
static ngx_int_t set_www_authenticate_header(ngx_http_request_t *request, phantom_token_configuration_t *module_location_config, char *error_code)
{
request->headers_out.www_authenticate = ngx_list_push(&request->headers_out.headers);
if (request->headers_out.www_authenticate == NULL)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
static const char REALM_PREFIX[] = "realm=\"";
static const size_t REALM_PREFIX_SIZE = sizeof(REALM_PREFIX) - 1;
static const char TOKEN_SUFFIX[] = "\"";
static const size_t TOKEN_SUFFIX_SIZE = sizeof(TOKEN_SUFFIX) - 1;
static const char TOKEN_SEPARATER[] = ", ";
static const size_t TOKEN_SEPARATER_SIZE = sizeof(TOKEN_SEPARATER) - 1;
static const char SCOPE_PREFIX[] = "scope=\"";
static const size_t SCOPE_PREFIX_SIZE = sizeof(SCOPE_PREFIX) - 1;
static const u_char ERROR_CODE_PREFIX[] = "error=\"";
static const size_t ERROR_CODE_PREFIX_SIZE = sizeof(ERROR_CODE_PREFIX) - 1;
size_t bearer_data_size = BEARER_SIZE + sizeof('\0'); // Add one for the nul byte
bool realm_provided = module_location_config->realm.len > 0;
bool scopes_provided = module_location_config->space_separated_scopes.len > 0;
bool error_code_provided = error_code != NULL;
bool append_one_comma = false, append_two_commas = false;
size_t error_code_len = 0;
if (realm_provided)
{
bearer_data_size += REALM_PREFIX_SIZE + module_location_config->realm.len + TOKEN_SUFFIX_SIZE;
}
if (scopes_provided)
{
bearer_data_size += SCOPE_PREFIX_SIZE + module_location_config->space_separated_scopes.len + TOKEN_SUFFIX_SIZE;
}
if (error_code_provided)
{
error_code_len = ngx_strlen(error_code);
bearer_data_size += ERROR_CODE_PREFIX_SIZE + error_code_len + TOKEN_SUFFIX_SIZE;
}
if ((realm_provided && scopes_provided) || (realm_provided && error_code_provided) || (scopes_provided && error_code_provided))
{
bearer_data_size += TOKEN_SEPARATER_SIZE;
append_one_comma = true;
if (realm_provided && scopes_provided && error_code_provided)
{
bearer_data_size += TOKEN_SEPARATER_SIZE;
append_two_commas = true;
}
}
u_char *bearer_data = ngx_pnalloc(request->pool, bearer_data_size);
if (bearer_data == NULL)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
u_char *p = ngx_cpymem(bearer_data, BEARER, BEARER_SIZE);
if (realm_provided)
{
p = ngx_cpymem(p, REALM_PREFIX, REALM_PREFIX_SIZE);
p = ngx_cpymem(p, module_location_config->realm.data, module_location_config->realm.len);
p = ngx_cpymem(p, TOKEN_SUFFIX, TOKEN_SUFFIX_SIZE);
if (append_one_comma)
{
p = ngx_cpymem(p, TOKEN_SEPARATER, TOKEN_SEPARATER_SIZE);
}
}
if (scopes_provided)
{
p = ngx_cpymem(p, SCOPE_PREFIX, SCOPE_PREFIX_SIZE);
p = ngx_cpymem(p, module_location_config->space_separated_scopes.data, module_location_config->space_separated_scopes.len);
p = ngx_cpymem(p, TOKEN_SUFFIX, TOKEN_SUFFIX_SIZE);
if (append_one_comma || append_two_commas)
{
p = ngx_cpymem(p, TOKEN_SEPARATER, TOKEN_SEPARATER_SIZE);
}
}
if (error_code_provided)
{
p = ngx_cpymem(p, ERROR_CODE_PREFIX, ERROR_CODE_PREFIX_SIZE);
p = ngx_cpymem(p, error_code, error_code_len);
p = ngx_cpymem(p, TOKEN_SUFFIX, TOKEN_SUFFIX_SIZE);
}
if (!scopes_provided && !realm_provided && !error_code_provided)
{
// Only 'Bearer' is being sent. Replace the space at the end of BEARER with a null byte.
*(p - 1) = '\0';
}
else
{
*p = '\0';
}
request->headers_out.www_authenticate->hash = 1;
ngx_str_set(&request->headers_out.www_authenticate->key, "WWW-Authenticate");
request->headers_out.www_authenticate->value.data = bearer_data;
request->headers_out.www_authenticate->value.len = ngx_strlen(bearer_data);
assert(request->headers_out.www_authenticate->value.len <= bearer_data_size);
return write_error_response(request, NGX_HTTP_UNAUTHORIZED, module_location_config);
}
/*
* Add the error response as a JSON object that is easier to handle than the default HTML response that NGINX returns
* http://nginx.org/en/docs/dev/development_guide.html#http_response_body
*/
static ngx_int_t write_error_response(ngx_http_request_t *request, ngx_int_t status, phantom_token_configuration_t *module_location_config)
{
ngx_int_t rc;
ngx_str_t code;
ngx_str_t message;
u_char json_error_data[256];
ngx_chain_t output;
ngx_buf_t *body = NULL;
const char *error_format = NULL;
size_t error_len = 0;
if (request->method == NGX_HTTP_HEAD)
{
return status;
}
body = ngx_calloc_buf(request->pool);
if (body == NULL)
{
ngx_log_error(NGX_LOG_WARN, request->connection->log, 0, "Failed to allocate memory for error body");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
else
{
if (status == NGX_HTTP_UNAUTHORIZED)
{
ngx_str_set(&code, "unauthorized_request");
ngx_str_set(&message, "Access denied due to missing, invalid or "
"expired credentials.");
} else if (status >= 400 && status < 500) {
ngx_str_set(&code, "client_error");
ngx_str_set(&message,
"Problem encountered processing the request.");
} else if (status == NGX_HTTP_SERVICE_UNAVAILABLE) {
ngx_str_set(&code, "service_unavailable");
ngx_str_set(
&message,
"Service unavailable. Please contact the administrator.");
} else {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Internal server error encountered when processing "
"the request.");
ngx_str_set(&code, "server_error");
ngx_str_set(
&message,
"Internal server error encountered when "
"processing the request. Please contact the administrator.");
}
/* The string length calculation replaces the two '%V' markers with their actual values */
error_format = "{\"code\":\"%V\",\"message\":\"%V\"}";
error_len = ngx_strlen(error_format) + code.len + message.len - 4;
ngx_snprintf(json_error_data, sizeof(json_error_data) - 1, error_format, &code, &message);
json_error_data[error_len] = 0;
request->headers_out.status = status;
request->headers_out.content_length_n = error_len;
ngx_str_set(&request->headers_out.content_type, "application/json");
rc = ngx_http_send_header(request);
if (rc == NGX_ERROR || rc > NGX_OK || request->header_only) {
return rc;
}
body->pos = json_error_data;
body->last = json_error_data + error_len;
body->memory = 1;
body->last_buf = 1;
body->last_in_chain = 1;
output.buf = body;
output.next = NULL;
/* Return an error result, which also requires finalize_request to be called, to prevent a 'header already sent' warning in logs
https://forum.nginx.org/read.php?29,280514,280521#msg-280521 */
rc = ngx_http_output_filter(request, &output);
ngx_http_finalize_request(request, rc);
return NGX_DONE;
}
}