|
18 | 18 | #include "ngx_http_auth_jwt_binary_converters.h"
|
19 | 19 | #include "ngx_http_auth_jwt_string.h"
|
20 | 20 |
|
| 21 | +#include <stdio.h> |
| 22 | + |
| 23 | +const char* KEY_FILE_PATH = "/app/pub_key"; |
| 24 | + |
21 | 25 | typedef struct {
|
22 | 26 | ngx_str_t auth_jwt_loginurl;
|
23 | 27 | ngx_str_t auth_jwt_key;
|
24 | 28 | ngx_flag_t auth_jwt_enabled;
|
25 | 29 | ngx_flag_t auth_jwt_redirect;
|
26 | 30 | ngx_str_t auth_jwt_validation_type;
|
27 | 31 | ngx_str_t auth_jwt_algorithm;
|
| 32 | + ngx_flag_t auth_jwt_filekey; |
28 | 33 | ngx_flag_t auth_jwt_validate_email;
|
29 | 34 |
|
30 | 35 | } ngx_http_auth_jwt_loc_conf_t;
|
@@ -58,6 +63,13 @@ static ngx_command_t ngx_http_auth_jwt_commands[] = {
|
58 | 63 | offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_enabled),
|
59 | 64 | NULL },
|
60 | 65 |
|
| 66 | + { ngx_string("auth_jwt_filekey"), |
| 67 | + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
| 68 | + ngx_conf_set_flag_slot, |
| 69 | + NGX_HTTP_LOC_CONF_OFFSET, |
| 70 | + offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_filekey), |
| 71 | + NULL }, |
| 72 | + |
61 | 73 | { ngx_string("auth_jwt_redirect"),
|
62 | 74 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
63 | 75 | ngx_conf_set_flag_slot,
|
@@ -177,8 +189,45 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
|
177 | 189 | else if ( auth_jwt_algorithm.len == sizeof("RS256") - 1 && ngx_strncmp(auth_jwt_algorithm.data, "RS256", sizeof("RS256") - 1) == 0 )
|
178 | 190 | {
|
179 | 191 | // in this case, 'Binary' is a misnomer, as it is the public key string itself
|
180 |
| - keyBinary = jwtcf->auth_jwt_key.data; |
181 |
| - keylen = jwtcf->auth_jwt_key.len; |
| 192 | + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to find a jwt"); |
| 193 | + if (jwtcf->auth_jwt_filekey == 1) |
| 194 | + { |
| 195 | + FILE *file = fopen(KEY_FILE_PATH, "rb"); |
| 196 | + |
| 197 | + // Check if file exists or is correctly opened |
| 198 | + if (file == NULL) |
| 199 | + { |
| 200 | + char err[100]; |
| 201 | + strcpy(err, "failed to open pub key file: "); |
| 202 | + strcat(err, KEY_FILE_PATH); |
| 203 | + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, err); |
| 204 | + goto redirect; |
| 205 | + } |
| 206 | + |
| 207 | + // Read file length |
| 208 | + fseek(file, 0, SEEK_END); |
| 209 | + long key_size = ftell(file); |
| 210 | + fseek(file, 0, SEEK_SET); |
| 211 | + |
| 212 | + if (key_size == 0) |
| 213 | + { |
| 214 | + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "invalid key file size, check the key file"); |
| 215 | + goto redirect; |
| 216 | + } |
| 217 | + |
| 218 | + // Read pub key |
| 219 | + char *pub_key = malloc(key_size + 1); |
| 220 | + size_t bytes_read = fread(pub_key, 1, key_size, file); |
| 221 | + fclose(file); |
| 222 | + |
| 223 | + keyBinary = (u_char*)pub_key; |
| 224 | + keylen = (int)key_size; |
| 225 | + } |
| 226 | + else |
| 227 | + { |
| 228 | + keyBinary = jwtcf->auth_jwt_key.data; |
| 229 | + keylen = jwtcf->auth_jwt_key.len; |
| 230 | + } |
182 | 231 | }
|
183 | 232 | else
|
184 | 233 | {
|
@@ -374,6 +423,7 @@ ngx_http_auth_jwt_create_loc_conf(ngx_conf_t *cf)
|
374 | 423 | conf->auth_jwt_enabled = (ngx_flag_t) -1;
|
375 | 424 | conf->auth_jwt_redirect = (ngx_flag_t) -1;
|
376 | 425 | conf->auth_jwt_validate_email = (ngx_flag_t) -1;
|
| 426 | + conf->auth_jwt_filekey = (ngx_flag_t) -1; |
377 | 427 |
|
378 | 428 | ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, "Created Location Configuration");
|
379 | 429 |
|
@@ -403,6 +453,11 @@ ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
403 | 453 | conf->auth_jwt_redirect = (prev->auth_jwt_redirect == ((ngx_flag_t) -1)) ? 0 : prev->auth_jwt_redirect;
|
404 | 454 | }
|
405 | 455 |
|
| 456 | + if (conf->auth_jwt_filekey == ((ngx_flag_t) -1)) |
| 457 | + { |
| 458 | + conf->auth_jwt_filekey = (prev->auth_jwt_filekey == ((ngx_flag_t) -1)) ? 0 : prev->auth_jwt_filekey; |
| 459 | + } |
| 460 | + |
406 | 461 | return NGX_CONF_OK;
|
407 | 462 | }
|
408 | 463 |
|
|
0 commit comments