Auth members have optional strings now.

This commit is contained in:
2026-01-10 17:05:15 -05:00
parent 99c95b9f0b
commit 7eeeb61ae2
3 changed files with 26 additions and 41 deletions
+14 -29
View File
@@ -357,8 +357,7 @@ static AuthRealm_t *Auth_realm_by_name(const AuthHost_t *host,
int i;
for (i = 0; (realm = reinterpret_cast< AuthRealm_t * >( dList_nth_data(host->realms, i) )); i++)
if (strcmp(realm->name, name) == 0)
return realm;
if (realm->name == name) return realm;
return NULL;
}
@@ -394,17 +393,10 @@ static void Auth_realm_delete(AuthRealm_t *realm)
{
int i;
MSG("Auth_realm_delete: \"%s\"\n", realm->name);
MSG("Auth_realm_delete: \"%s\"\n", realm->name.value().c_str());
for (i = dList_length(realm->paths) - 1; i >= 0; i--)
dFree(dList_nth_data(realm->paths, i));
dList_free(realm->paths);
dFree(realm->name);
dFree(realm->username);
dFree(realm->authorization);
dFree(realm->cnonce);
dFree(realm->nonce);
dFree(realm->opaque);
dFree(realm->domain);
delete realm;
}
@@ -458,7 +450,7 @@ char *a_Auth_get_auth_str(const DilloUrl *url, const char *request_uri)
if ((host = Auth_host_by_url(url)) &&
(realm = Auth_realm_by_path(host, URL_PATH(url)))) {
if (realm->type == BASIC)
ret = dStrdup(realm->authorization);
ret = dStrdup(realm->authorization.value().c_str());
else if (realm->type == DIGEST)
ret = a_Digest_authorization_hdr(realm, url, request_uri);
else
@@ -510,8 +502,7 @@ static int Auth_do_auth_required(const AuthParse_t *auth_parse,
if (auth_parse->type == DIGEST && auth_parse->stale) {
/* we do have valid credentials but our nonce is old */
dFree((void *)realm->nonce);
realm->nonce = dStrdup(auth_parse->nonce.value().c_str());
realm->nonce = auth_parse->nonce;
return 0;
}
}
@@ -545,39 +536,33 @@ static void Auth_do_auth_dialog_cb(const char *user, const char *password,
/* find or create the realm */
if (!(realm = Auth_realm_by_name(host, data->auth_parse->realm.value().c_str()))) {
realm = std::make_unique< AuthRealm_t >().release();
realm->name = dStrdup(data->auth_parse->realm.value().c_str());
realm->name = data->auth_parse->realm;
realm->paths = dList_new(1);
dList_append(host->realms, realm);
}
realm->type = data->auth_parse->type;
dFree(realm->authorization);
realm->authorization = NULL;
realm->authorization = std::nullopt;
Auth_realm_add_path(realm, URL_PATH(data->url));
if (realm->type == BASIC) {
char *user_password = dStrconcat(user, ":", password, NULL);
char *response = a_Misc_encode_base64(user_password);
char *authorization =
dStrconcat("Authorization: Basic ", response, "\r\n", NULL);
dFree(realm->authorization);
realm->authorization = authorization;
realm->authorization = "Authorization: Basic ";
realm->authorization.value()+= response;
realm->authorization.value()+= "\r\n";
dFree(response);
dStrshred(user_password);
dFree(user_password);
} else if (realm->type == DIGEST) {
dFree(realm->username);
realm->username = dStrdup(user);
realm->username = user;
realm->nonce_count = 0;
dFree(realm->nonce);
realm->nonce = dStrdup(data->auth_parse->nonce.value().c_str());
dFree(realm->opaque);
realm->opaque = dStrdup(data->auth_parse->opaque.value().c_str());
realm->nonce = data->auth_parse->nonce;
realm->opaque = data->auth_parse->opaque;
realm->algorithm = data->auth_parse->algorithm;
dFree(realm->domain);
realm->domain = dStrdup(data->auth_parse->domain.value().c_str());
realm->domain = data->auth_parse->domain;
realm->qop = data->auth_parse->qop;
dFree(realm->cnonce);
realm->cnonce= std::nullopt;
if (realm->qop != QOPNOTSET)
realm->cnonce = a_Digest_create_cnonce();
if (!a_Digest_compute_digest(realm, user, password)) {
+7 -7
View File
@@ -8,18 +8,18 @@ enum AuthParseDigestQOP_t { QOPNOTSET, AUTH, AUTHINT };
struct AuthRealm_t{
enum AuthParseHTTPAuthType_t type;
char *name;
std::optional< std::string > name;
Dlist *paths; /* stripped of any trailing '/', so the root path is "" */
char *authorization; /* BASIC: the authorization request header */
std::optional< std::string > authorization; /* BASIC: the authorization request header */
/* DIGEST: the hexdigest of A1 */
/* digest state ahead */
char *username;
char *cnonce;
std::optional< std::string > username;
std::optional< std::string > cnonce;
unsigned int nonce_count;
char *nonce;
char *opaque;
std::optional< std::string > nonce;
std::optional< std::string > opaque;
enum AuthParseDigestAlgorithm_t algorithm;
char *domain; /* NOT USED */
std::optional< std::string > domain; /* NOT USED */
enum AuthParseDigestQOP_t qop;
};
+5 -5
View File
@@ -180,12 +180,12 @@ char *a_Digest_authorization_hdr(AuthRealm_t *realm, const DilloUrl *url,
if (!response)
return NULL;
result = dStr_new("Authorization: Digest ");
Digest_Dstr_append_token_value(result, 0, "username", realm->username, 1);
Digest_Dstr_append_token_value(result, 1, "realm", realm->name, 1);
Digest_Dstr_append_token_value(result, 1, "nonce", realm->nonce, 1);
Digest_Dstr_append_token_value(result, 0, "username", realm->username.value().c_str(), 1);
Digest_Dstr_append_token_value(result, 1, "realm", realm->name.value().c_str(), 1);
Digest_Dstr_append_token_value(result, 1, "nonce", realm->nonce.value().c_str(), 1);
Digest_Dstr_append_token_value(result, 1, "uri", digest_uri, 1);
if (realm->qop != QOPNOTSET) {
Digest_Dstr_append_token_value(result, 1, "cnonce", realm->cnonce, 1);
Digest_Dstr_append_token_value(result, 1, "cnonce", realm->cnonce.value().c_str(), 1);
dStr_sprintfa(result, ", nc=%08x", realm->nonce_count);
}
if (realm->algorithm != ALGORITHMNOTSET) {
@@ -194,7 +194,7 @@ char *a_Digest_authorization_hdr(AuthRealm_t *realm, const DilloUrl *url,
}
Digest_Dstr_append_token_value(result, 1, "response", response->str, 1);
if (realm->opaque)
Digest_Dstr_append_token_value(result, 1, "opaque", realm->opaque, 1);
Digest_Dstr_append_token_value(result, 1, "opaque", realm->opaque.value().c_str(), 1);
if (realm->qop != QOPNOTSET)
Digest_Dstr_append_token_value(result, 1, "qop", QOP2STR[realm->qop], 1);
dStr_sprintfa(result, "\r\n");