11
11
#include <jansson.h>
12
12
13
13
typedef struct {
14
- ngx_str_t auth_jwt_loginurl ;
15
- ngx_str_t auth_jwt_key ;
14
+ ngx_str_t auth_jwt_loginurl ;
15
+ ngx_str_t auth_jwt_key ;
16
16
ngx_flag_t auth_jwt_enabled ;
17
17
} ngx_http_auth_jwt_loc_conf_t ;
18
18
@@ -25,59 +25,59 @@ static int hex_to_binary( const char* str, u_char* buf, int len );
25
25
26
26
static ngx_command_t ngx_http_auth_jwt_commands [] = {
27
27
28
- { ngx_string ("auth_jwt_loginurl" ),
29
- NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
30
- ngx_conf_set_str_slot ,
31
- NGX_HTTP_LOC_CONF_OFFSET ,
32
- offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_loginurl ),
33
- NULL },
34
-
35
- { ngx_string ("auth_jwt_key" ),
36
- NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
37
- ngx_conf_set_str_slot ,
38
- NGX_HTTP_LOC_CONF_OFFSET ,
39
- offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_key ),
40
- NULL },
28
+ { ngx_string ("auth_jwt_loginurl" ),
29
+ NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
30
+ ngx_conf_set_str_slot ,
31
+ NGX_HTTP_LOC_CONF_OFFSET ,
32
+ offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_loginurl ),
33
+ NULL },
34
+
35
+ { ngx_string ("auth_jwt_key" ),
36
+ NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
37
+ ngx_conf_set_str_slot ,
38
+ NGX_HTTP_LOC_CONF_OFFSET ,
39
+ offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_key ),
40
+ NULL },
41
41
42
- { ngx_string ("auth_jwt_enabled" ),
43
- NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_FLAG ,
44
- ngx_conf_set_flag_slot ,
45
- NGX_HTTP_LOC_CONF_OFFSET ,
46
- offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_enabled ),
47
- NULL },
48
-
49
- ngx_null_command
42
+ { ngx_string ("auth_jwt_enabled" ),
43
+ NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_FLAG ,
44
+ ngx_conf_set_flag_slot ,
45
+ NGX_HTTP_LOC_CONF_OFFSET ,
46
+ offsetof(ngx_http_auth_jwt_loc_conf_t , auth_jwt_enabled ),
47
+ NULL },
48
+
49
+ ngx_null_command
50
50
};
51
51
52
52
53
53
static ngx_http_module_t ngx_http_auth_jwt_module_ctx = {
54
- NULL , /* preconfiguration */
55
- ngx_http_auth_jwt_init , /* postconfiguration */
54
+ NULL , /* preconfiguration */
55
+ ngx_http_auth_jwt_init , /* postconfiguration */
56
56
57
- NULL , /* create main configuration */
58
- NULL , /* init main configuration */
57
+ NULL , /* create main configuration */
58
+ NULL , /* init main configuration */
59
59
60
- NULL , /* create server configuration */
61
- NULL , /* merge server configuration */
60
+ NULL , /* create server configuration */
61
+ NULL , /* merge server configuration */
62
62
63
- ngx_http_auth_jwt_create_loc_conf , /* create ___location configuration */
64
- ngx_http_auth_jwt_merge_loc_conf /* merge ___location configuration */
63
+ ngx_http_auth_jwt_create_loc_conf , /* create ___location configuration */
64
+ ngx_http_auth_jwt_merge_loc_conf /* merge ___location configuration */
65
65
};
66
66
67
67
68
68
ngx_module_t ngx_http_auth_jwt_module = {
69
- NGX_MODULE_V1 ,
70
- & ngx_http_auth_jwt_module_ctx , /* module context */
71
- ngx_http_auth_jwt_commands , /* module directives */
72
- NGX_HTTP_MODULE , /* module type */
73
- NULL , /* init master */
74
- NULL , /* init module */
75
- NULL , /* init process */
76
- NULL , /* init thread */
77
- NULL , /* exit thread */
78
- NULL , /* exit process */
79
- NULL , /* exit master */
80
- NGX_MODULE_V1_PADDING
69
+ NGX_MODULE_V1 ,
70
+ & ngx_http_auth_jwt_module_ctx , /* module context */
71
+ ngx_http_auth_jwt_commands , /* module directives */
72
+ NGX_HTTP_MODULE , /* module type */
73
+ NULL , /* init master */
74
+ NULL , /* init module */
75
+ NULL , /* init process */
76
+ NULL , /* init thread */
77
+ NULL , /* exit thread */
78
+ NULL , /* exit process */
79
+ NULL , /* exit master */
80
+ NGX_MODULE_V1_PADDING
81
81
};
82
82
83
83
@@ -97,7 +97,6 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
97
97
time_t exp ;
98
98
time_t now ;
99
99
100
-
101
100
jwtcf = ngx_http_get_module_loc_conf (r , ngx_http_auth_jwt_module );
102
101
103
102
if (!jwtcf -> auth_jwt_enabled )
@@ -169,20 +168,48 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
169
168
170
169
redirect :
171
170
r -> headers_out .___location = ngx_list_push (& r -> headers_out .headers );
172
- if (r -> headers_out .___location == NULL ) {
171
+
172
+ if (r -> headers_out .___location == NULL )
173
+ {
173
174
ngx_http_finalize_request (r , NGX_HTTP_INTERNAL_SERVER_ERROR );
174
175
}
176
+
175
177
r -> headers_out .___location -> hash = 1 ;
176
178
r -> headers_out .___location -> key .len = sizeof ("Location" ) - 1 ;
177
179
r -> headers_out .___location -> key .data = (u_char * ) "Location" ;
178
180
179
181
if (r -> method == NGX_HTTP_GET )
180
182
{
181
- int loginlen = jwtcf -> auth_jwt_loginurl .len ;
182
-
183
- char * scheme = (r -> connection -> ssl ) ? "https" : "http" ;
184
- ngx_str_t server = r -> headers_in .server ;
185
- ngx_str_t uri = r -> uri ;
183
+ int loginlen ;
184
+ char * scheme ;
185
+ ngx_str_t server ;
186
+ ngx_str_t uri_variable_name = ngx_string ("request_uri" );;
187
+ ngx_int_t uri_variable_hash ;
188
+ ngx_http_variable_value_t * request_uri_var ;
189
+ ngx_str_t uri ;
190
+
191
+ loginlen = jwtcf -> auth_jwt_loginurl .len ;
192
+
193
+ scheme = (r -> connection -> ssl ) ? "https" : "http" ;
194
+ server = r -> headers_in .server ;
195
+
196
+ // get the URI
197
+ uri_variable_hash = ngx_hash_key (uri_variable_name .data , uri_variable_name .len );
198
+ request_uri_var = ngx_http_get_variable (r , & uri_variable_name , uri_variable_hash );
199
+
200
+ // get the uri
201
+ if (request_uri_var && !request_uri_var -> not_found && request_uri_var -> valid )
202
+ {
203
+ // ideally we would like the uri with the querystring parameters
204
+ uri .data = ngx_palloc (r -> pool , request_uri_var -> len );
205
+ uri .len = request_uri_var -> len ;
206
+ ngx_memcpy (uri .data , request_uri_var -> data , request_uri_var -> len );
207
+ }
208
+ else
209
+ {
210
+ // fallback to the querystring without params
211
+ uri = r -> uri ;
212
+ }
186
213
187
214
r -> headers_out .___location -> value .len = loginlen + sizeof ("?return_url=" ) - 1 + strlen (scheme ) + sizeof ("://" ) - 1 + server .len + uri .len ;
188
215
return_url = ngx_alloc (r -> headers_out .___location -> value .len , r -> connection -> log );
@@ -204,6 +231,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
204
231
}
205
232
else
206
233
{
234
+ // for non-get requests, redirect to the login page without a return URL
207
235
r -> headers_out .___location -> value .len = jwtcf -> auth_jwt_loginurl .len ;
208
236
r -> headers_out .___location -> value .data = jwtcf -> auth_jwt_loginurl .data ;
209
237
}
@@ -215,53 +243,56 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
215
243
static ngx_int_t ngx_http_auth_jwt_init (ngx_conf_t * cf )
216
244
{
217
245
ngx_http_handler_pt * h ;
218
- ngx_http_core_main_conf_t * cmcf ;
246
+ ngx_http_core_main_conf_t * cmcf ;
219
247
220
- cmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_core_module );
248
+ cmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_core_module );
221
249
222
- h = ngx_array_push (& cmcf -> phases [NGX_HTTP_ACCESS_PHASE ].handlers );
223
- if (h == NULL ) {
224
- return NGX_ERROR ;
225
- }
250
+ h = ngx_array_push (& cmcf -> phases [NGX_HTTP_ACCESS_PHASE ].handlers );
251
+ if (h == NULL )
252
+ {
253
+ return NGX_ERROR ;
254
+ }
226
255
227
- * h = ngx_http_auth_jwt_handler ;
256
+ * h = ngx_http_auth_jwt_handler ;
228
257
229
- return NGX_OK ;
258
+ return NGX_OK ;
230
259
}
231
260
232
261
233
262
static void *
234
263
ngx_http_auth_jwt_create_loc_conf (ngx_conf_t * cf )
235
264
{
236
- ngx_http_auth_jwt_loc_conf_t * conf ;
265
+ ngx_http_auth_jwt_loc_conf_t * conf ;
237
266
238
- conf = ngx_pcalloc (cf -> pool , sizeof (ngx_http_auth_jwt_loc_conf_t ));
239
- if (conf == NULL ) {
240
- return NULL ;
241
- }
267
+ conf = ngx_pcalloc (cf -> pool , sizeof (ngx_http_auth_jwt_loc_conf_t ));
268
+ if (conf == NULL )
269
+ {
270
+ return NULL ;
271
+ }
242
272
243
273
// set the flag to unset
244
274
conf -> auth_jwt_enabled = (ngx_flag_t ) - 1 ;
245
275
246
276
ngx_conf_log_error (NGX_LOG_DEBUG , cf , 0 , "Created Location Configuration" );
247
277
248
- return conf ;
278
+ return conf ;
249
279
}
250
280
251
281
252
282
static char *
253
283
ngx_http_auth_jwt_merge_loc_conf (ngx_conf_t * cf , void * parent , void * child )
254
284
{
255
- ngx_http_auth_jwt_loc_conf_t * prev = parent ;
256
- ngx_http_auth_jwt_loc_conf_t * conf = child ;
285
+ ngx_http_auth_jwt_loc_conf_t * prev = parent ;
286
+ ngx_http_auth_jwt_loc_conf_t * conf = child ;
257
287
258
288
ngx_conf_merge_str_value (conf -> auth_jwt_loginurl , prev -> auth_jwt_loginurl , "" );
259
289
ngx_conf_merge_str_value (conf -> auth_jwt_key , prev -> auth_jwt_key , "" );
260
290
261
291
262
- if (conf -> auth_jwt_enabled == ((ngx_flag_t ) - 1 )) {
263
- conf -> auth_jwt_enabled = (prev -> auth_jwt_enabled == ((ngx_flag_t ) - 1 )) ? 0 : prev -> auth_jwt_enabled ;
264
- }
292
+ if (conf -> auth_jwt_enabled == ((ngx_flag_t ) - 1 ))
293
+ {
294
+ conf -> auth_jwt_enabled = (prev -> auth_jwt_enabled == ((ngx_flag_t ) - 1 )) ? 0 : prev -> auth_jwt_enabled ;
295
+ }
265
296
266
297
ngx_conf_log_error (NGX_LOG_DEBUG , cf , 0 , "Merged Location Configuration" );
267
298
@@ -274,16 +305,16 @@ ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
274
305
static int
275
306
hex_char_to_binary ( char ch , char * ret )
276
307
{
277
- ch = tolower ( ch );
278
- if ( isdigit ( ch ) )
279
- * ret = ch - '0' ;
280
- else if ( ch >= 'a' && ch <= 'f' )
281
- * ret = ( ch - 'a' ) + 10 ;
282
- else if ( ch >= 'A' && ch <= 'F' )
283
- * ret = ( ch - 'A' ) + 10 ;
284
- else
285
- return * ret = 0 ;
286
- return 1 ;
308
+ ch = tolower ( ch );
309
+ if ( isdigit ( ch ) )
310
+ * ret = ch - '0' ;
311
+ else if ( ch >= 'a' && ch <= 'f' )
312
+ * ret = ( ch - 'a' ) + 10 ;
313
+ else if ( ch >= 'A' && ch <= 'F' )
314
+ * ret = ( ch - 'A' ) + 10 ;
315
+ else
316
+ return * ret = 0 ;
317
+ return 1 ;
287
318
}
288
319
289
320
static int
0 commit comments