diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-06-13 12:15:50 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-06-13 12:15:50 +0300 |
commit | c2414ffc9adc80beb62b65a36349bb8de9741476 (patch) | |
tree | be967e1d02f83e2e12b82adb23673be6f3d1bcdf /doc/examples | |
parent | bb0addb5872b2fbfbcb8722305afb7c64f8cc501 (diff) | |
download | libmicrohttpd-c2414ffc9adc80beb62b65a36349bb8de9741476.tar.gz libmicrohttpd-c2414ffc9adc80beb62b65a36349bb8de9741476.zip |
Updated examples to use new API for Basic Authorization
Diffstat (limited to 'doc/examples')
-rw-r--r-- | doc/examples/basicauthentication.c | 49 | ||||
-rw-r--r-- | doc/examples/tlsauthentication.c | 110 |
2 files changed, 47 insertions, 112 deletions
diff --git a/doc/examples/basicauthentication.c b/doc/examples/basicauthentication.c index d75ba636..6e1493a3 100644 --- a/doc/examples/basicauthentication.c +++ b/doc/examples/basicauthentication.c | |||
@@ -23,9 +23,7 @@ answer_to_connection (void *cls, struct MHD_Connection *connection, | |||
23 | const char *version, const char *upload_data, | 23 | const char *version, const char *upload_data, |
24 | size_t *upload_data_size, void **req_cls) | 24 | size_t *upload_data_size, void **req_cls) |
25 | { | 25 | { |
26 | char *user; | 26 | struct MHD_BasicAuthInfo *auth_info; |
27 | char *pass; | ||
28 | int fail; | ||
29 | enum MHD_Result ret; | 27 | enum MHD_Result ret; |
30 | struct MHD_Response *response; | 28 | struct MHD_Response *response; |
31 | (void) cls; /* Unused. Silent compiler warning. */ | 29 | (void) cls; /* Unused. Silent compiler warning. */ |
@@ -41,30 +39,43 @@ answer_to_connection (void *cls, struct MHD_Connection *connection, | |||
41 | *req_cls = connection; | 39 | *req_cls = connection; |
42 | return MHD_YES; | 40 | return MHD_YES; |
43 | } | 41 | } |
44 | pass = NULL; | 42 | auth_info = MHD_basic_auth_get_username_password3 (connection); |
45 | user = MHD_basic_auth_get_username_password (connection, | 43 | if (NULL == auth_info) |
46 | &pass); | ||
47 | fail = ( (NULL == user) || | ||
48 | (0 != strcmp (user, "root")) || | ||
49 | (0 != strcmp (pass, "pa$$w0rd") ) ); | ||
50 | if (NULL != user) | ||
51 | MHD_free (user); | ||
52 | if (NULL != pass) | ||
53 | MHD_free (pass); | ||
54 | if (fail) | ||
55 | { | 44 | { |
56 | const char *page = "<html><body>Go away.</body></html>"; | 45 | static const char *page = |
46 | "<html><body>Authorization required</body></html>"; | ||
57 | response = MHD_create_response_from_buffer_static (strlen (page), page); | 47 | response = MHD_create_response_from_buffer_static (strlen (page), page); |
58 | ret = MHD_queue_basic_auth_fail_response (connection, | 48 | ret = MHD_queue_basic_auth_fail_response3 (connection, |
59 | "my realm", | 49 | "admins", |
60 | response); | 50 | MHD_YES, |
51 | response); | ||
52 | } | ||
53 | else if ((strlen ("root") != auth_info->username_len) || | ||
54 | (0 != memcmp (auth_info->username, "root", | ||
55 | auth_info->username_len)) || | ||
56 | /* The next check against NULL is optional, | ||
57 | * if 'password' is NULL then 'password_len' is always zero. */ | ||
58 | (NULL == auth_info->password) || | ||
59 | (strlen ("pa$$w0rd") != auth_info->password_len) || | ||
60 | (0 != memcmp (auth_info->password, "pa$$w0rd", | ||
61 | auth_info->password_len))) | ||
62 | { | ||
63 | static const char *page = | ||
64 | "<html><body>Wrong username or password</body></html>"; | ||
65 | response = MHD_create_response_from_buffer_static (strlen (page), page); | ||
66 | ret = MHD_queue_basic_auth_fail_response3 (connection, | ||
67 | "admins", | ||
68 | MHD_YES, | ||
69 | response); | ||
61 | } | 70 | } |
62 | else | 71 | else |
63 | { | 72 | { |
64 | const char *page = "<html><body>A secret.</body></html>"; | 73 | static const char *page = "<html><body>A secret.</body></html>"; |
65 | response = MHD_create_response_from_buffer_static (strlen (page), page); | 74 | response = MHD_create_response_from_buffer_static (strlen (page), page); |
66 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 75 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
67 | } | 76 | } |
77 | if (NULL != auth_info) | ||
78 | MHD_free (auth_info); | ||
68 | MHD_destroy_response (response); | 79 | MHD_destroy_response (response); |
69 | return ret; | 80 | return ret; |
70 | } | 81 | } |
diff --git a/doc/examples/tlsauthentication.c b/doc/examples/tlsauthentication.c index 65d9d8db..0cd6c4e6 100644 --- a/doc/examples/tlsauthentication.c +++ b/doc/examples/tlsauthentication.c | |||
@@ -15,7 +15,7 @@ | |||
15 | 15 | ||
16 | #define PORT 8888 | 16 | #define PORT 8888 |
17 | 17 | ||
18 | #define REALM "\"Maintenance\"" | 18 | #define REALM "Maintenance" |
19 | #define USER "a legitimate user" | 19 | #define USER "a legitimate user" |
20 | #define PASSWORD "and his password" | 20 | #define PASSWORD "and his password" |
21 | 21 | ||
@@ -23,48 +23,6 @@ | |||
23 | #define SERVERCERTFILE "server.pem" | 23 | #define SERVERCERTFILE "server.pem" |
24 | 24 | ||
25 | 25 | ||
26 | static char * | ||
27 | string_to_base64 (const char *message) | ||
28 | { | ||
29 | const char *lookup = | ||
30 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
31 | unsigned long l; | ||
32 | size_t i; | ||
33 | size_t j; | ||
34 | char *tmp; | ||
35 | size_t length = strlen (message); | ||
36 | |||
37 | tmp = malloc (length * 2 + 1); | ||
38 | if (NULL == tmp) | ||
39 | return NULL; | ||
40 | j = 0; | ||
41 | for (i = 0; i < length; i += 3) | ||
42 | { | ||
43 | l = (((unsigned long) message[i]) << 16) | ||
44 | | (((i + 1) < length) ? (((unsigned long) message[i + 1]) << 8) : 0) | ||
45 | | (((i + 2) < length) ? ((unsigned long) message[i + 2]) : 0); | ||
46 | |||
47 | |||
48 | tmp [j++] = lookup[(l >> 18) & 0x3F]; | ||
49 | tmp [j++] = lookup[(l >> 12) & 0x3F]; | ||
50 | |||
51 | if (i + 1 < length) | ||
52 | tmp [j++] = lookup[(l >> 6) & 0x3F]; | ||
53 | if (i + 2 < length) | ||
54 | tmp [j++] = lookup[l & 0x3F]; | ||
55 | } | ||
56 | |||
57 | if (0 != length % 3) | ||
58 | tmp [j++] = '='; | ||
59 | if (1 == length % 3) | ||
60 | tmp [j++] = '='; | ||
61 | |||
62 | tmp [j] = 0; | ||
63 | |||
64 | return tmp; | ||
65 | } | ||
66 | |||
67 | |||
68 | static size_t | 26 | static size_t |
69 | get_file_size (const char *filename) | 27 | get_file_size (const char *filename) |
70 | { | 28 | { |
@@ -126,35 +84,15 @@ ask_for_authentication (struct MHD_Connection *connection, const char *realm) | |||
126 | { | 84 | { |
127 | enum MHD_Result ret; | 85 | enum MHD_Result ret; |
128 | struct MHD_Response *response; | 86 | struct MHD_Response *response; |
129 | char *headervalue; | ||
130 | size_t slen; | ||
131 | const char *strbase = "Basic realm="; | ||
132 | 87 | ||
133 | response = MHD_create_response_empty (MHD_RF_NONE); | 88 | response = MHD_create_response_empty (MHD_RF_NONE); |
134 | if (! response) | 89 | if (! response) |
135 | return MHD_NO; | 90 | return MHD_NO; |
136 | 91 | ||
137 | slen = strlen (strbase) + strlen (realm) + 1; | 92 | ret = MHD_queue_basic_auth_fail_response3 (connection, |
138 | if (NULL == (headervalue = malloc (slen))) | 93 | realm, |
139 | return MHD_NO; | 94 | MHD_YES, |
140 | snprintf (headervalue, | 95 | response); |
141 | slen, | ||
142 | "%s%s", | ||
143 | strbase, | ||
144 | realm); | ||
145 | ret = MHD_add_response_header (response, | ||
146 | "WWW-Authenticate", | ||
147 | headervalue); | ||
148 | free (headervalue); | ||
149 | if (! ret) | ||
150 | { | ||
151 | MHD_destroy_response (response); | ||
152 | return MHD_NO; | ||
153 | } | ||
154 | |||
155 | ret = MHD_queue_response (connection, | ||
156 | MHD_HTTP_UNAUTHORIZED, | ||
157 | response); | ||
158 | MHD_destroy_response (response); | 96 | MHD_destroy_response (response); |
159 | return ret; | 97 | return ret; |
160 | } | 98 | } |
@@ -165,37 +103,23 @@ is_authenticated (struct MHD_Connection *connection, | |||
165 | const char *username, | 103 | const char *username, |
166 | const char *password) | 104 | const char *password) |
167 | { | 105 | { |
168 | const char *headervalue; | 106 | struct MHD_BasicAuthInfo *auth_info; |
169 | char *expected_b64; | ||
170 | char *expected; | ||
171 | const char *strbase = "Basic "; | ||
172 | int authenticated; | 107 | int authenticated; |
173 | size_t slen; | ||
174 | 108 | ||
175 | headervalue = | 109 | auth_info = MHD_basic_auth_get_username_password3 (connection); |
176 | MHD_lookup_connection_value (connection, MHD_HEADER_KIND, | 110 | if (NULL == auth_info) |
177 | "Authorization"); | ||
178 | if (NULL == headervalue) | ||
179 | return 0; | ||
180 | if (0 != strncmp (headervalue, strbase, strlen (strbase))) | ||
181 | return 0; | 111 | return 0; |
112 | authenticated = | ||
113 | ( (strlen (username) == auth_info->username_len) && | ||
114 | (0 == memcmp (auth_info->username, username, auth_info->username_len)) && | ||
115 | /* The next check against NULL is optional, | ||
116 | * if 'password' is NULL then 'password_len' is always zero. */ | ||
117 | (NULL != auth_info->password) && | ||
118 | (strlen (password) == auth_info->password_len) && | ||
119 | (0 == memcmp (auth_info->password, password, auth_info->password_len)) ); | ||
182 | 120 | ||
183 | slen = strlen (username) + 1 + strlen (password) + 1; | 121 | MHD_free (auth_info); |
184 | if (NULL == (expected = malloc (slen))) | ||
185 | return 0; | ||
186 | snprintf (expected, | ||
187 | slen, | ||
188 | "%s:%s", | ||
189 | username, | ||
190 | password); | ||
191 | expected_b64 = string_to_base64 (expected); | ||
192 | free (expected); | ||
193 | if (NULL == expected_b64) | ||
194 | return 0; | ||
195 | 122 | ||
196 | authenticated = | ||
197 | (strcmp (headervalue + strlen (strbase), expected_b64) == 0); | ||
198 | free (expected_b64); | ||
199 | return authenticated; | 123 | return authenticated; |
200 | } | 124 | } |
201 | 125 | ||