Skip to content

Commit c2a7db2

Browse files
committed
nginx: add nginx_oauth2_set_require to be used with OAuth2Require etc.
see OpenIDC/ngx_oauth2_module#7; thanks @smanolache and @pladen Signed-off-by: Hans Zandbelt <[email protected]>
1 parent a64a58e commit c2a7db2

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

ChangeLog

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
06/20/2024
2+
- nginx: add nginx_oauth2_set_require to be used with OAuth2Require etc.
3+
see OpenIDC/ngx_oauth2_module#7; thanks @smanolache and @pladen
4+
15
06/19/2024
26
- add NGINX macros/functions for setting claim variables in the request context
3-
see OpenIDC/ngx_oauth2_module#7; thanks @@smanolache and @pladen
7+
see OpenIDC/ngx_oauth2_module#7; thanks @smanolache and @pladen
48
- allow NGINX primitives in an if block within a location block in the http block
59
- bump to 1.6.3dev
610
.

include/oauth2/nginx.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
return rv ? NGX_CONF_ERROR : NGX_CONF_OK; \
6464
}
6565

66+
#define OAUTH2_NGINX_CFG_FUNC_RET1(module, type, primitive, func, member) \
67+
OAUTH2_NGINX_CFG_FUNC_START(module, type, primitive) \
68+
(void)value; \
69+
rv = func(cf, &cfg->member); \
70+
OAUTH2_NGINX_CFG_FUNC_END(cf, rv)
71+
6672
#define OAUTH2_NGINX_CFG_FUNC_ARGS1(module, type, primitive, func, member) \
6773
OAUTH2_NGINX_CFG_FUNC_START(module, type, primitive) \
6874
char *v1 = cf->args->nelts > 1 \
@@ -113,7 +119,8 @@
113119
#define OAUTH2_NGINX_CMD(take, module, directive, primitive) \
114120
{ \
115121
ngx_string(directive), \
116-
NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE##take, \
122+
NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | \
123+
NGX_CONF_TAKE##take, \
117124
ngx_##module##_set_##primitive, NGX_HTTP_LOC_CONF_OFFSET, \
118125
0, NULL \
119126
}
@@ -168,9 +175,9 @@ ngx_int_t oauth2_nginx_claim_variable(ngx_module_t module,
168175
char *oauth2_nginx_set_claim(ngx_module_t module,
169176
ngx_http_get_variable_pt handler, ngx_conf_t *cf,
170177
ngx_command_t *cmd, void *conf);
171-
172178
ngx_int_t oauth2_nginx_set_target_variables(ngx_module_t module,
173179
oauth2_nginx_request_context_t *ctx,
174180
json_t *json_token);
181+
char *nginx_oauth2_set_require(ngx_conf_t *cf, ngx_array_t **requirements);
175182

176183
#endif /* _OAUTH2_NGINX_H_ */

src/server/nginx.c

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,14 @@ ngx_int_t oauth2_nginx_claim_variable(ngx_module_t module,
410410
return NGX_OK;
411411
}
412412

413-
static const size_t MAX_BUF = 128;
413+
static const size_t OAUTH2_NGINX_MAX_BUF = 128;
414414

415415
char *oauth2_nginx_set_claim(ngx_module_t module,
416416
ngx_http_get_variable_pt handler, ngx_conf_t *cf,
417417
ngx_command_t *cmd, void *conf)
418418
{
419419
ngx_http_variable_t *v;
420-
char buf[MAX_BUF];
420+
char buf[OAUTH2_NGINX_MAX_BUF];
421421
int n = 0;
422422
char *s = NULL;
423423
ngx_str_t *value = cf->args->elts;
@@ -558,3 +558,53 @@ ngx_int_t oauth2_nginx_set_target_variables(ngx_module_t module,
558558

559559
return NGX_OK;
560560
}
561+
562+
char *nginx_oauth2_set_require(ngx_conf_t *cf, ngx_array_t **requirements)
563+
{
564+
ngx_http_complex_value_t *val = NULL;
565+
ngx_http_compile_complex_value_t ccv;
566+
ngx_str_t *var = NULL;
567+
int rc = NGX_OK;
568+
char *s = NULL;
569+
char buf[OAUTH2_NGINX_MAX_BUF];
570+
571+
if (cf->args == NULL)
572+
return NGX_CONF_ERROR;
573+
574+
if (*requirements == NULL) {
575+
*requirements =
576+
ngx_array_create(cf->pool, cf->args->nelts,
577+
sizeof(ngx_http_complex_value_t));
578+
if (*requirements == NULL) {
579+
ngx_str_t msg = ngx_string("Out of memory");
580+
s = oauth2_nginx_str2chr(cf->pool, &msg);
581+
return s ? s : NGX_CONF_ERROR;
582+
}
583+
}
584+
585+
for (unsigned int i = 1; i < cf->args->nelts; ++i) {
586+
587+
var = (ngx_str_t *)cf->args->elts + i;
588+
/* no allocation here because we've already dimensioned the
589+
* array upon its creation */
590+
val = (ngx_http_complex_value_t *)ngx_array_push(*requirements);
591+
592+
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
593+
ccv.cf = cf;
594+
ccv.value = var;
595+
ccv.complex_value = val;
596+
597+
rc = ngx_http_compile_complex_value(&ccv);
598+
if (rc != NGX_OK) {
599+
int n = snprintf(buf, sizeof(buf),
600+
"Error %d compiling "
601+
"expression %.*s",
602+
rc, (int)var->len, var->data);
603+
ngx_str_t msg = {n, (u_char *)&buf[0]};
604+
s = oauth2_nginx_str2chr(cf->pool, &msg);
605+
return s ? s : NGX_CONF_ERROR;
606+
}
607+
}
608+
609+
return NGX_CONF_OK;
610+
}

test/server_stubs.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,19 @@ ngx_http_variable_t *ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name,
248248
return NULL;
249249
}
250250

251+
ngx_array_t *ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
252+
{
253+
return NULL;
254+
}
255+
256+
void *ngx_array_push(ngx_array_t *a)
257+
{
258+
return NULL;
259+
}
260+
261+
ngx_int_t ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv)
262+
{
263+
return 0;
264+
}
265+
251266
#endif

0 commit comments

Comments
 (0)