ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
basic_auth.c File Reference

Go to the source code of this file.

Functions

void http_basic_credentials_clear (http_basic_credentials_t *credentials)
asciichat_error_t http_basic_auth_parse (const char *authorization, http_basic_credentials_t *out)
bool http_basic_auth_matches (const http_basic_credentials_t *provided, const char *username, const char *password)
bool http_basic_auth_authorize (const http_basic_auth_config_t *config, const char *authorization)
bool http_server_authorize (const http_basic_auth_config_t *config, const struct http_request_info *request, struct http_response *response)

Function Documentation

◆ http_basic_auth_authorize()

bool http_basic_auth_authorize ( const http_basic_auth_config_t * config,
const char * authorization )

Definition at line 97 of file basic_auth.c.

97 {
98 if (!config) {
99 (void)SET_ERRNO(ERROR_INVALID_PARAM, "Missing Basic authentication configuration");
100 return false;
101 }
102 if (!config->enabled)
103 return true;
104 if (!config->username || !config->password || !config->username[0] || !config->password[0]) {
105 (void)SET_ERRNO(ERROR_INVALID_PARAM, "Basic authentication requires non-empty credentials");
106 return false;
107 }
108 http_basic_credentials_t credentials;
109 bool authorized = authorization && http_basic_auth_parse(authorization, &credentials) == ASCIICHAT_OK && http_basic_auth_matches(&credentials, config->username, config->password);
110 http_basic_credentials_clear(&credentials);
111 return authorized;
112}
#define SET_ERRNO(code,...)
void http_basic_credentials_clear(http_basic_credentials_t *credentials)
Definition basic_auth.c:9
bool http_basic_auth_matches(const http_basic_credentials_t *provided, const char *username, const char *password)
Definition basic_auth.c:60
asciichat_error_t http_basic_auth_parse(const char *authorization, http_basic_credentials_t *out)
Definition basic_auth.c:14
struct http_basic_credentials http_basic_credentials_t
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
const char * password
Definition basic_auth.h:18
const char * username
Definition basic_auth.h:17

References ASCIICHAT_OK, http_basic_auth_config::enabled, ERROR_INVALID_PARAM, http_basic_auth_matches(), http_basic_auth_parse(), http_basic_credentials_clear(), http_basic_auth_config::password, SET_ERRNO, and http_basic_auth_config::username.

Referenced by http_server_authorize().

◆ http_basic_auth_matches()

bool http_basic_auth_matches ( const http_basic_credentials_t * provided,
const char * username,
const char * password )

Definition at line 60 of file basic_auth.c.

60 {
61 if (!provided || !username || !password) {
62 (void)SET_ERRNO(ERROR_INVALID_PARAM, "Basic credential comparison arguments are required");
63 return false;
64 }
65
66 uint8_t expected_username[HTTP_BASIC_AUTH_VALUE_CAPACITY] = {0};
67 uint8_t expected_password[HTTP_BASIC_AUTH_VALUE_CAPACITY] = {0};
68 bool expected_username_terminated = false, expected_password_terminated = false;
69 for (size_t i = 0; i < HTTP_BASIC_AUTH_VALUE_CAPACITY; ++i) {
70 if (!expected_username_terminated) {
71 expected_username[i] = (uint8_t)username[i];
72 if (!username[i])
73 expected_username_terminated = true;
74 }
75 if (!expected_password_terminated) {
76 expected_password[i] = (uint8_t)password[i];
77 if (!password[i])
78 expected_password_terminated = true;
79 }
80 }
81 bool provided_username_terminated = false, provided_password_terminated = false;
82 for (size_t i = 0; i < HTTP_BASIC_AUTH_VALUE_CAPACITY; ++i) {
83 if (!provided_username_terminated && !provided->username[i])
84 provided_username_terminated = true;
85 if (!provided_password_terminated && !provided->password[i])
86 provided_password_terminated = true;
87 }
88 int username_comparison = sodium_memcmp(provided->username, expected_username, HTTP_BASIC_AUTH_VALUE_CAPACITY);
89 int password_comparison = sodium_memcmp(provided->password, expected_password, HTTP_BASIC_AUTH_VALUE_CAPACITY);
90 bool username_match = expected_username_terminated && provided_username_terminated && username_comparison == 0;
91 bool password_match = expected_password_terminated && provided_password_terminated && password_comparison == 0;
92 sodium_memzero(expected_username, sizeof(expected_username));
93 sodium_memzero(expected_password, sizeof(expected_password));
94 return username_match && password_match;
95}
#define HTTP_BASIC_AUTH_VALUE_CAPACITY
Definition basic_auth.h:13
unsigned char uint8_t

References ERROR_INVALID_PARAM, HTTP_BASIC_AUTH_VALUE_CAPACITY, http_basic_credentials::password, SET_ERRNO, and http_basic_credentials::username.

Referenced by http_basic_auth_authorize().

◆ http_basic_auth_parse()

asciichat_error_t http_basic_auth_parse ( const char * authorization,
http_basic_credentials_t * out )

Definition at line 14 of file basic_auth.c.

14 {
15 if (!authorization || !out)
16 return SET_ERRNO(ERROR_INVALID_PARAM, "Basic authorization arguments are required");
17
18 memset(out, 0, sizeof(*out));
19 while (*authorization == ' ' || *authorization == '\t')
20 ++authorization;
21
22 static const char scheme[] = "Basic";
23 if (platform_strncasecmp(authorization, scheme, sizeof(scheme) - 1) != 0 || (authorization[sizeof(scheme) - 1] != ' ' && authorization[sizeof(scheme) - 1] != '\t'))
24 return SET_ERRNO(ERROR_INVALID_PARAM, "Authorization is not Basic authentication");
25
26 const char *encoded = authorization + sizeof(scheme) - 1;
27 while (*encoded == ' ' || *encoded == '\t')
28 ++encoded;
29 size_t encoded_length = strlen(encoded);
30 if (!encoded_length || encoded_length > sizeof(*out) * 4 || encoded_length % 4 != 0)
31 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Basic authorization value");
32
33 uint8_t decoded[sizeof(*out) + 1];
34 size_t decoded_length = 0;
35 if (sodium_base642bin(decoded, sizeof(decoded) - 1, encoded, encoded_length, NULL, &decoded_length, NULL, sodium_base64_VARIANT_ORIGINAL) != 0 || decoded_length == 0) {
36 sodium_memzero(decoded, sizeof(decoded));
37 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Basic authorization encoding");
38 }
39 for (size_t i = 0; i < decoded_length; ++i) {
40 if (decoded[i] == '\0') {
41 sodium_memzero(decoded, sizeof(decoded));
42 return SET_ERRNO(ERROR_INVALID_PARAM, "Basic authorization contains an embedded NUL");
43 }
44 }
45
46 char *separator = memchr(decoded, ':', decoded_length);
47 if (!separator || separator == (char *)decoded || (size_t)(separator - (char *)decoded) >= sizeof(out->username) || decoded_length - (size_t)(separator - (char *)decoded) - 1 == 0 ||
48 decoded_length - (size_t)(separator - (char *)decoded) - 1 >= sizeof(out->password)) {
49 sodium_memzero(decoded, sizeof(decoded));
50 return SET_ERRNO(ERROR_INVALID_PARAM, "Basic authorization must contain username and password");
51 }
52 size_t username_length = (size_t)(separator - (char *)decoded);
53 size_t password_length = decoded_length - username_length - 1;
54 memcpy(out->username, decoded, username_length);
55 memcpy(out->password, separator + 1, password_length);
56 sodium_memzero(decoded, sizeof(decoded));
57 return ASCIICHAT_OK;
58}
static bool size_t gotel_query_t * out
Definition queries.h:10
int platform_strncasecmp(const char *s1, const char *s2, size_t n)

References ASCIICHAT_OK, ERROR_INVALID_PARAM, out, platform_strncasecmp(), and SET_ERRNO.

Referenced by http_basic_auth_authorize().

◆ http_basic_credentials_clear()

void http_basic_credentials_clear ( http_basic_credentials_t * credentials)

Definition at line 9 of file basic_auth.c.

9 {
10 if (credentials)
11 sodium_memzero(credentials, sizeof(*credentials));
12}

Referenced by http_basic_auth_authorize().

◆ http_server_authorize()

bool http_server_authorize ( const http_basic_auth_config_t * config,
const struct http_request_info * request,
struct http_response * response )

Definition at line 114 of file basic_auth.c.

114 {
115 if (!config || !request || !response) {
116 (void)SET_ERRNO(ERROR_INVALID_PARAM, "Basic authorization request arguments are required");
117 return false;
118 }
119 if (!config->enabled)
120 return true;
121 const char *authorization = NULL;
122 for (size_t i = 0; i < request->header_count; ++i) {
123 if (!platform_strcasecmp(request->headers[i].name, "authorization")) {
124 authorization = request->headers[i].value;
125 break;
126 }
127 }
128 if (http_basic_auth_authorize(config, authorization))
129 return true;
131 if (http_response_set_status(response, 401) != ASCIICHAT_OK)
132 return false;
133 const char *realm = config->realm && config->realm[0] ? config->realm : "ascii-chat";
134 char challenge[256];
135 if (safe_snprintf(challenge, sizeof(challenge), "Basic realm=\"%s\"", realm) < 0 || http_response_set_header(response, "WWW-Authenticate", challenge) != ASCIICHAT_OK)
136 return false;
137 (void)http_response_finish(response);
138 return false;
139}
bool http_basic_auth_authorize(const http_basic_auth_config_t *config, const char *authorization)
Definition basic_auth.c:97
asciichat_error_t http_response_set_status(http_response_t *r, unsigned s)
asciichat_error_t http_response_set_header(http_response_t *r, const char *n, const char *v)
asciichat_error_t http_response_finish(http_response_t *r)
void stats_counter_add(stats_scope_t *scope, stats_counter_id_t id, uint64_t amount)
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:88
int platform_strcasecmp(const char *s1, const char *s2)

References ASCIICHAT_OK, http_basic_auth_config::enabled, ERROR_INVALID_PARAM, http_request_info::header_count, http_request_info::headers, http_basic_auth_authorize(), http_response_finish(), http_response_set_header(), http_response_set_status(), http_header_t::name, platform_strcasecmp(), http_basic_auth_config::realm, SET_ERRNO, stats_counter_add(), STATS_COUNTER_HTTP_REJECTED, stats_runtime_scope(), and http_header_t::value.

Referenced by http_server_dispatch_http3().