Skip to content

Commit c01c6d3

Browse files
marisnjmckenna
authored andcommitted
Add http basic authorization option to http requests (related to #283) (#328)
1 parent 4c935c7 commit c01c6d3

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

lib/http.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ struct _header_struct {
4040
mapcache_context *ctx;
4141
};
4242

43+
char *base64_encode(apr_pool_t *pool, const unsigned char *data, size_t input_length);
44+
4345
size_t _mapcache_curl_memory_callback(void *ptr, size_t size, size_t nmemb, void *data)
4446
{
4547
mapcache_buffer *buffer = (mapcache_buffer*)data;
@@ -414,8 +416,43 @@ mapcache_http* mapcache_http_configuration_parse_xml(mapcache_context *ctx, ezxm
414416
apr_table_set(req->headers, header_node->name, header_node->txt);
415417
}
416418
}
419+
420+
/* Parse auth and append to headers for simplicity */
421+
if ((http_node = ezxml_child(node, "auth")) != NULL) {
422+
if (ezxml_attr(http_node, "scheme") &&
423+
strcmp(ezxml_attr(http_node, "scheme"), "basic") == 0) {
424+
ezxml_t user_node, pass_node;
425+
char *credentials, *str2enc;
426+
user_node = ezxml_child(http_node, "user");
427+
pass_node = ezxml_child(http_node, "pass");
428+
if (!user_node || !pass_node) {
429+
ctx->set_error(ctx, 400,
430+
"both <http> <auth> username (<user>) and password "
431+
"(<pass>) elements must be provided");
432+
return NULL;
433+
}
434+
str2enc =
435+
apr_pstrcat(ctx->pool, user_node->txt, ":", pass_node->txt, NULL);
436+
credentials = base64_encode(ctx->pool, (unsigned char *)str2enc,
437+
sizeof(unsigned char) * strlen(str2enc));
438+
memset(str2enc, '\0', sizeof(char) * strlen(str2enc));
439+
if (credentials == NULL) {
440+
ctx->set_error(ctx, 400, "error encoding <http> <auth> credentials");
441+
return NULL;
442+
}
443+
apr_table_set(req->headers, "Authorization",
444+
apr_pstrcat(ctx->pool, "Basic ", credentials, NULL));
445+
memset(credentials, '\0', sizeof(char) * strlen(credentials));
446+
} else {
447+
ctx->set_error(ctx, 400,
448+
"invalid or missing <http> <auth> scheme (only 'basic' "
449+
"scheme supported)");
450+
return NULL;
451+
}
452+
}
453+
417454
return req;
418-
/* TODO: parse <proxy> and <auth> elements */
455+
/* TODO: parse <proxy> element */
419456
}
420457

421458

0 commit comments

Comments
 (0)