libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] Basic Auth Example patch


From: Sven Geggus
Subject: [libmicrohttpd] Basic Auth Example patch
Date: Thu, 01 Dec 2011 13:32:03 -0000
User-agent: tin/2.0.0-20110823 ("Ardenistiel") (UNIX) (Linux/2.6.27.52-ztdummy (x86_64))

Hi there,

am I right in the assumption, that the documentation at
http://www.gnu.org/s/libmicrohttpd/tutorial.html#Supporting-basic-authentication
is newer than authorization_example.c in the src/examples/ subdirectory?

If so here is a patch to make authorization_example.c use the functions from
the tutorial instead of raw base64 strings.

Regards

Sven

--cut--
--- authorization_example.c     2011-12-01 14:24:48.686994967 +0100
+++ authorization_example_new.c 2011-12-01 14:23:40.699587802 +0100
@@ -45,8 +45,9 @@
   const char *me = cls;
   struct MHD_Response *response;
   int ret;
-  int code;
-  const char *auth;
+  char *user;
+  char *pass;
+  int fail;
 
   if (0 != strcmp (method, "GET"))
     return MHD_NO;              /* unexpected method */
@@ -57,28 +58,26 @@
       return MHD_YES;
     }
   *ptr = NULL;                  /* reset when done */
-  auth = MHD_lookup_connection_value (connection,
-                                      MHD_HEADER_KIND,
-                                      MHD_HTTP_HEADER_AUTHORIZATION);
-  if ((auth == NULL) ||
-      (0 != strcmp (auth, "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==")))
-    {
-      /* require: "Aladdin" with password "open sesame" */
+
+  /* require: "Aladdin" with password "open sesame" */
+  pass = NULL;
+  user = MHD_basic_auth_get_username_password (connection, &pass);
+  fail = ( (user == NULL) || (0 != strcmp (user, "Aladdin")) || (0 != strcmp 
(pass, "open sesame") ) );
+  if (fail)
+  {
       response = MHD_create_response_from_buffer (strlen (DENIED),
                                                  (void *) DENIED, 
                                                  MHD_RESPMEM_PERSISTENT);
-      MHD_add_response_header (response, MHD_HTTP_HEADER_WWW_AUTHENTICATE,
-                               "Basic realm=\"TestRealm\"");
-      code = MHD_HTTP_UNAUTHORIZED;
+      ret = MHD_queue_basic_auth_fail_response 
(connection,"TestRealm",response);
     }
   else
     {
       response = MHD_create_response_from_buffer (strlen (me),
                                                  (void *) me, 
                                                  MHD_RESPMEM_PERSISTENT);
-      code = MHD_HTTP_OK;
+      ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
     }
-  ret = MHD_queue_response (connection, code, response);
+
   MHD_destroy_response (response);
   return ret;
 }
--cut--

-- 
The source code is not comprehensible
                 (found in bug section of man 8 telnetd on Redhat Linux)

/me is address@hidden, http://sven.gegg.us/ on the Web



reply via email to

[Prev in Thread] Current Thread [Next in Thread]