From e66e830db19502f2e6d02e91a6f427930e374590 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Tue, 21 Dec 2010 01:37:42 -0600 Subject: [PATCH] Begin work on authentication --- http_blerg.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/http_blerg.c b/http_blerg.c index 8abebaa..8543fb8 100644 --- a/http_blerg.c +++ b/http_blerg.c @@ -96,6 +96,51 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c } } } else if (strncmp(url, "/put", 4) == 0) { + char *username; + const char *password = "testpass"; + const char *realm = "Blerg Post"; + +#define OPAQUE "d29fb6db8f21a6e99903651a9f87470e" +#define DENIED "DENIED, MOTHERFUCKER" +#define PAGE "DERP DERP AUTHENTICATED DERP" + + if (*ptr == NULL) { + *ptr = (void *) 1; + + username = MHD_digest_auth_get_username(connection); + if (username == NULL) { + response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO); + ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO); + MHD_destroy_response(response); + return ret; + } + + printf("username: %s\n", username); + + ret = MHD_digest_auth_check(connection, realm, username, password, 300); + free(username); + + if (ret == MHD_INVALID_NONCE || ret == MHD_NO) { + response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO); + ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, + (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO); + MHD_destroy_response(response); + return ret; + } + } + + *ptr = NULL; + + if (url[4] != '/') + return respond_404(connection); + ret = parse_url_info(url + 5, author, &record); + if ((ret & URL_INFO_AUTHOR) == 0) + return respond_404(connection); + + response = MHD_create_response_from_data(strlen(PAGE), PAGE, MHD_NO, MHD_NO); + ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + MHD_destroy_response(response); + return ret; } else { return respond_404(connection); } -- 2.25.1