-
Notifications
You must be signed in to change notification settings - Fork 17
/
ngx_http_auth_token_module.c
307 lines (248 loc) · 7.83 KB
/
ngx_http_auth_token_module.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
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <string.h>
#include "hiredis/hiredis.h"
typedef struct {
ngx_str_t redis_host;
ngx_int_t redis_port;
} auth_token_main_conf_t;
typedef struct {
ngx_flag_t enabled;
ngx_str_t redirect_location;
ngx_str_t cookie_name;
ngx_str_t header_name;
} auth_token_loc_conf_t;
ngx_module_t ngx_http_auth_token_module;
static ngx_int_t
lookup_user(auth_token_main_conf_t *conf, ngx_str_t *auth_token, ngx_str_t *user_id)
{
redisContext *context = redisConnect((const char*)conf->redis_host.data, conf->redis_port);
redisReply *reply = redisCommand(context, "GET %s", auth_token->data);
if (reply->type == REDIS_REPLY_NIL) {
return NGX_DECLINED;
} else {
user_id->len = strlen(reply->str);
user_id->data = (u_char *) reply->str;
return NGX_OK;
}
}
static ngx_int_t
redirect(ngx_http_request_t *r, ngx_str_t *location)
{
ngx_table_elt_t *h;
h = ngx_list_push(&r->headers_out.headers);
h->hash = 1;
ngx_str_set(&h->key, "Location");
h->value = *location;
return NGX_HTTP_MOVED_TEMPORARILY;
}
static void
append_user_id(ngx_http_request_t *r, ngx_str_t *user_id)
{
ngx_table_elt_t *h;
h = ngx_list_push(&r->headers_in.headers);
h->hash = 1;
ngx_str_set(&h->key, "X-User-Id");
h->value = *user_id;
}
static ngx_int_t
search_headers(ngx_http_request_t *r, auth_token_loc_conf_t *location_conf, ngx_str_t *token)
{
ngx_list_part_t *part;
ngx_table_elt_t *h;
ngx_uint_t i;
part = &r->headers_in.headers.part;
h = part->elts;
for (i = 0; /**/; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
h = part->elts;
i = 0;
}
if (ngx_strncmp(h[i].key.data, location_conf->header_name.data, h[i].key.len) == 0) {
token->data = h[i].value.data;
token->len = h[i].value.len;
return NGX_OK;
}
}
return NGX_DECLINED;
}
static ngx_int_t
header_lookup(ngx_http_request_t *r, auth_token_loc_conf_t *location_conf, ngx_str_t *token)
{
ngx_int_t result = search_headers(r, location_conf, token);
if (result == NGX_DECLINED) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Could not locate header %V", &location_conf->header_name);
return NGX_DECLINED;
} else {
return NGX_OK;
}
}
static ngx_int_t
cookie_lookup(ngx_http_request_t *r, auth_token_loc_conf_t *location_conf, ngx_str_t *token)
{
ngx_int_t cookie_location;
cookie_location = ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &location_conf->cookie_name, token);
if (cookie_location == NGX_DECLINED) {
return NGX_DECLINED;
} else {
return NGX_OK;
}
}
static ngx_int_t
ngx_http_auth_token_handler(ngx_http_request_t *r)
{
if (r->main->internal) {
return NGX_DECLINED;
}
auth_token_loc_conf_t *location_conf = ngx_http_get_module_loc_conf(r, ngx_http_auth_token_module);
if (!location_conf->enabled || location_conf->enabled == NGX_CONF_UNSET) {
return NGX_DECLINED;
}
auth_token_main_conf_t *main_conf = ngx_http_get_module_main_conf(r, ngx_http_auth_token_module);
ngx_str_t auth_token;
ngx_int_t search_result;
if (location_conf->header_name.len != 0) {
search_result = header_lookup(r, location_conf, &auth_token);
} else {
search_result = cookie_lookup(r, location_conf, &auth_token);
}
if (search_result == NGX_DECLINED) {
return redirect(r, &location_conf->redirect_location);
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Looking up user by auth token %V", &auth_token);
ngx_str_t user_id;
ngx_int_t lookup_result = lookup_user(main_conf, &auth_token, &user_id);
if (lookup_result == NGX_DECLINED) {
return redirect(r, &location_conf->redirect_location);
} else {
append_user_id(r, &user_id);
return NGX_DECLINED;
}
}
static ngx_int_t
ngx_http_auth_token_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_auth_token_handler;
return NGX_OK;
}
static void*
ngx_http_auth_token_create_main_conf(ngx_conf_t *cf)
{
auth_token_main_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(auth_token_main_conf_t));
if (conf == NULL) {
return NULL;
}
conf->redis_port = NGX_CONF_UNSET_UINT;
return conf;
}
static void*
ngx_http_auth_token_create_loc_conf(ngx_conf_t *cf)
{
auth_token_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(auth_token_loc_conf_t));
if (conf == NULL) {
return NULL;
}
conf->enabled = NGX_CONF_UNSET;
return conf;
}
static char*
ngx_http_auth_token_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
auth_token_loc_conf_t *prev = (auth_token_loc_conf_t*)parent;
auth_token_loc_conf_t *conf = (auth_token_loc_conf_t*)child;
ngx_conf_merge_value(conf->enabled, prev->enabled, 0);
ngx_conf_merge_str_value(conf->redirect_location, prev->redirect_location, "");
ngx_conf_merge_str_value(conf->cookie_name, prev->cookie_name, "");
ngx_conf_merge_str_value(conf->header_name, prev->header_name, "");
return NGX_CONF_OK;
}
static ngx_command_t ngx_http_auth_token_commands[] = {
{
ngx_string("auth_token_redis_host"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(auth_token_main_conf_t, redis_host),
NULL
},
{
ngx_string("auth_token_redis_port"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(auth_token_main_conf_t, redis_port),
NULL
},
{
ngx_string("auth_token_cookie_name"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(auth_token_loc_conf_t, cookie_name),
NULL
},
{
ngx_string("auth_token_header_name"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(auth_token_loc_conf_t, header_name),
NULL
},
{
ngx_string("auth_token_redirect_location"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(auth_token_loc_conf_t, redirect_location),
NULL
},
{
ngx_string("auth_token_enabled"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(auth_token_loc_conf_t, enabled),
NULL
},
ngx_null_command
};
static ngx_http_module_t ngx_http_auth_token_module_ctx = {
NULL, /* preconfiguration */
ngx_http_auth_token_init, /* postconfiguration */
ngx_http_auth_token_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_auth_token_create_loc_conf, /* create location configuration */
ngx_http_auth_token_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_auth_token_module = {
NGX_MODULE_V1,
&ngx_http_auth_token_module_ctx, /* module context */
ngx_http_auth_token_commands, /* 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
};