Debian Codegraph @

src/testlib/mock-auth.c

raw
1 /*
2 * Copyright (C) 2013 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "mock-auth.h"
9
10 #include "common/cockpitauthorize.h"
11
12 #include "ws/websocket.h"
13
14 #include <string.h>
15
16 GHashTable *
17 mock_auth_basic_header (const gchar *user,
18 const gchar *password)
19 {
20 GHashTable *headers;
21 gchar *userpass;
22 gchar *encoded;
23 gchar *header;
24
25 userpass = g_strdup_printf ("%s:%s", user, password);
26 encoded = g_base64_encode ((guchar *)userpass, strlen (userpass));
27 header = g_strdup_printf ("Basic %s", encoded);
28
29 g_free (userpass);
30 g_free (encoded);
31
32 headers = web_socket_util_new_headers ();
33 g_hash_table_insert (headers, g_strdup ("Authorization"), header);
34 return headers;
35 }
36
37 void
38 mock_auth_include_cookie_as_if_client (GHashTable *resp_headers,
39 GHashTable *req_headers,
40 const gchar *cookie_name)
41 {
42 gchar *cookie;
43 gchar *end;
44 gchar *expected = g_strdup_printf ("%s=", cookie_name);
45
46 cookie = g_strdup (g_hash_table_lookup (resp_headers, "Set-Cookie"));
47 g_assert (cookie != NULL);
48 end = strchr (cookie, ';');
49 g_assert (end != NULL);
50 end[0] = '\0';
51
52 g_assert (strncmp (cookie, expected, strlen(expected)) == 0);
53
54 g_hash_table_insert (req_headers, g_strdup ("Cookie"), cookie);
55 g_free (expected);
56 }