aboutsummaryrefslogtreecommitdiff
path: root/src/examples/digest_auth_example.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-08-22 14:49:48 +0000
committerChristian Grothoff <christian@grothoff.org>2010-08-22 14:49:48 +0000
commit7b221e3a6a2f6ed8f34dbb04cf145c4795b9f930 (patch)
tree483cda153e75cb0b94052156ae487e1f8939463b /src/examples/digest_auth_example.c
parent53d412023bd9cdd665c2f327fac2d90a03bc24c0 (diff)
downloadlibmicrohttpd-7b221e3a6a2f6ed8f34dbb04cf145c4795b9f930.tar.gz
libmicrohttpd-7b221e3a6a2f6ed8f34dbb04cf145c4795b9f930.zip
initial draft for digest authentication (based on patch R3)
Diffstat (limited to 'src/examples/digest_auth_example.c')
-rw-r--r--src/examples/digest_auth_example.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/examples/digest_auth_example.c b/src/examples/digest_auth_example.c
new file mode 100644
index 00000000..b83be227
--- /dev/null
+++ b/src/examples/digest_auth_example.c
@@ -0,0 +1,105 @@
1/*
2 This file is part of libmicrohttpd
3 (C) 2010 Christian Grothoff (and other contributing authors)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19/**
20 * @file digest_auth_example.c
21 * @brief minimal example for how to use digest auth with libmicrohttpd
22 * @author Amr Ali
23 */
24
25#include "platform.h"
26#include <microhttpd.h>
27#include <stdlib.h>
28
29#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
30
31#define OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4"
32
33static int
34ahc_echo (void *cls,
35 struct MHD_Connection *connection,
36 const char *url,
37 const char *method,
38 const char *version,
39 const char *upload_data, size_t *upload_data_size, void **ptr)
40{
41 struct MHD_Response *response;
42 char *username;
43 const char *password = "testpass";
44 const char *realm = "test@example.com";
45 int ret;
46
47 username = MHD_digest_auth_get_username(connection);
48
49 if (username == NULL) {
50 ret = MHD_queue_auth_fail_response(connection, realm,
51 password,
52 OPAQUE,
53 MHD_NO);
54
55 return ret;
56 }
57
58 ret = MHD_digest_auth_check(connection, realm,
59 username, password, 300);
60
61 free(username);
62
63 if (ret == MHD_INVALID_NONCE) {
64 ret = MHD_queue_auth_fail_response(connection, realm,
65 password,
66 OPAQUE, MHD_YES);
67
68 return ret;
69 }
70
71 if (ret == MHD_NO) {
72 ret = MHD_queue_auth_fail_response(connection, realm,
73 password, OPAQUE, MHD_NO);
74
75 return ret;
76 }
77
78 response = MHD_create_response_from_data(strlen(PAGE), PAGE,
79 MHD_NO, MHD_NO);
80
81 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
82
83 MHD_destroy_response(response);
84 return ret;
85}
86
87int
88main (int argc, char *const *argv)
89{
90 struct MHD_Daemon *d;
91
92 if (argc != 2)
93 {
94 printf ("%s PORT\n", argv[0]);
95 return 1;
96 }
97 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
98 atoi (argv[1]),
99 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
100 if (d == NULL)
101 return 1;
102 (void) getc (stdin);
103 MHD_stop_daemon (d);
104 return 0;
105}