AuthParse RAII...
This commit is contained in:
+21
-30
@@ -29,12 +29,12 @@ struct AuthParse_t
|
|||||||
{
|
{
|
||||||
int ok;
|
int ok;
|
||||||
enum AuthParseHTTPAuthType_t type;
|
enum AuthParseHTTPAuthType_t type;
|
||||||
const char *realm;
|
std::optional< std::string > realm;
|
||||||
const char *nonce;
|
std::optional< std::string > nonce;
|
||||||
const char *opaque;
|
std::optional< std::string > opaque;
|
||||||
int stale;
|
int stale;
|
||||||
enum AuthParseDigestAlgorithm_t algorithm;
|
enum AuthParseDigestAlgorithm_t algorithm;
|
||||||
const char *domain;
|
std::optional< std::string > domain;
|
||||||
enum AuthParseDigestQOP_t qop;
|
enum AuthParseDigestQOP_t qop;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -45,10 +45,11 @@ struct AuthHost_t
|
|||||||
Dlist *realms;
|
Dlist *realms;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
struct AuthDialogData_t
|
||||||
|
{
|
||||||
const AuthParse_t *auth_parse;
|
const AuthParse_t *auth_parse;
|
||||||
const DilloUrl *url;
|
const DilloUrl *url;
|
||||||
} AuthDialogData_t;
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local data
|
* Local data
|
||||||
@@ -60,25 +61,15 @@ static AuthParse_t *Auth_parse_new(void)
|
|||||||
AuthParse_t *auth_parse = std::make_unique< AuthParse_t >().release();
|
AuthParse_t *auth_parse = std::make_unique< AuthParse_t >().release();
|
||||||
auth_parse->ok = 0;
|
auth_parse->ok = 0;
|
||||||
auth_parse->type = TYPENOTSET;
|
auth_parse->type = TYPENOTSET;
|
||||||
auth_parse->realm = NULL;
|
|
||||||
auth_parse->nonce = NULL;
|
|
||||||
auth_parse->opaque = NULL;
|
|
||||||
auth_parse->stale = 0;
|
auth_parse->stale = 0;
|
||||||
auth_parse->algorithm = ALGORITHMNOTSET;
|
auth_parse->algorithm = ALGORITHMNOTSET;
|
||||||
auth_parse->domain = NULL;
|
|
||||||
auth_parse->qop = QOPNOTSET;
|
auth_parse->qop = QOPNOTSET;
|
||||||
return auth_parse;
|
return auth_parse;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Auth_parse_free(AuthParse_t *auth_parse)
|
static void Auth_parse_free(AuthParse_t *auth_parse)
|
||||||
{
|
{
|
||||||
if (auth_parse) {
|
|
||||||
dFree((void *)auth_parse->realm);
|
|
||||||
dFree((void *)auth_parse->nonce);
|
|
||||||
dFree((void *)auth_parse->opaque);
|
|
||||||
dFree((void *)auth_parse->domain);
|
|
||||||
delete auth_parse;
|
delete auth_parse;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int Auth_path_is_inside(const char *path1, const char *path2, int len)
|
static int Auth_path_is_inside(const char *path1, const char *path2, int len)
|
||||||
@@ -225,7 +216,7 @@ static int Auth_parse_basic_challenge_cb(AuthParse_t *auth_parse, char *token,
|
|||||||
{
|
{
|
||||||
if (dStrAsciiCasecmp("realm", token) == 0) {
|
if (dStrAsciiCasecmp("realm", token) == 0) {
|
||||||
if (!auth_parse->realm)
|
if (!auth_parse->realm)
|
||||||
auth_parse->realm = dStrdup(value);
|
auth_parse->realm = value;
|
||||||
return 0; /* end parsing */
|
return 0; /* end parsing */
|
||||||
} else
|
} else
|
||||||
MSG("Auth_parse_basic_challenge_cb: Ignoring unknown parameter: %s = "
|
MSG("Auth_parse_basic_challenge_cb: Ignoring unknown parameter: %s = "
|
||||||
@@ -239,13 +230,13 @@ static int Auth_parse_digest_challenge_cb(AuthParse_t *auth_parse, char *token,
|
|||||||
const char *const fn = "Auth_parse_digest_challenge_cb";
|
const char *const fn = "Auth_parse_digest_challenge_cb";
|
||||||
|
|
||||||
if (!dStrAsciiCasecmp("realm", token) && !auth_parse->realm)
|
if (!dStrAsciiCasecmp("realm", token) && !auth_parse->realm)
|
||||||
auth_parse->realm = dStrdup(value);
|
auth_parse->realm = value;
|
||||||
else if (!strcmp("domain", token) && !auth_parse->domain)
|
else if (!strcmp("domain", token) && !auth_parse->domain)
|
||||||
auth_parse->domain = dStrdup(value);
|
auth_parse->domain = value;
|
||||||
else if (!strcmp("nonce", token) && !auth_parse->nonce)
|
else if (!strcmp("nonce", token) && !auth_parse->nonce)
|
||||||
auth_parse->nonce = dStrdup(value);
|
auth_parse->nonce = value;
|
||||||
else if (!strcmp("opaque", token) && !auth_parse->opaque)
|
else if (!strcmp("opaque", token) && !auth_parse->opaque)
|
||||||
auth_parse->opaque = dStrdup(value);
|
auth_parse->opaque = value;
|
||||||
else if (strcmp("stale", token) == 0) {
|
else if (strcmp("stale", token) == 0) {
|
||||||
if (dStrAsciiCasecmp("true", value) == 0)
|
if (dStrAsciiCasecmp("true", value) == 0)
|
||||||
auth_parse->stale = 1;
|
auth_parse->stale = 1;
|
||||||
@@ -514,7 +505,7 @@ static int Auth_do_auth_required(const AuthParse_t *auth_parse,
|
|||||||
* we will re-authenticate.
|
* we will re-authenticate.
|
||||||
*/
|
*/
|
||||||
if ((host = Auth_host_by_url(url)) &&
|
if ((host = Auth_host_by_url(url)) &&
|
||||||
(realm = Auth_realm_by_name(host, auth_parse->realm))) {
|
(realm = Auth_realm_by_name(host, auth_parse->realm.value().c_str()))) {
|
||||||
if (!Auth_realm_includes_path(realm, URL_PATH(url))) {
|
if (!Auth_realm_includes_path(realm, URL_PATH(url))) {
|
||||||
_MSG("Auth_do_auth_required: updating realm '%s' with URL '%s'\n",
|
_MSG("Auth_do_auth_required: updating realm '%s' with URL '%s'\n",
|
||||||
auth_parse->realm, URL_STR(url));
|
auth_parse->realm, URL_STR(url));
|
||||||
@@ -525,7 +516,7 @@ static int Auth_do_auth_required(const AuthParse_t *auth_parse,
|
|||||||
if (auth_parse->type == DIGEST && auth_parse->stale) {
|
if (auth_parse->type == DIGEST && auth_parse->stale) {
|
||||||
/* we do have valid credentials but our nonce is old */
|
/* we do have valid credentials but our nonce is old */
|
||||||
dFree((void *)realm->nonce);
|
dFree((void *)realm->nonce);
|
||||||
realm->nonce = dStrdup(auth_parse->nonce);
|
realm->nonce = dStrdup(auth_parse->nonce.value().c_str());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -557,9 +548,9 @@ static void Auth_do_auth_dialog_cb(const char *user, const char *password,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* find or create the realm */
|
/* find or create the realm */
|
||||||
if (!(realm = Auth_realm_by_name(host, data->auth_parse->realm))) {
|
if (!(realm = Auth_realm_by_name(host, data->auth_parse->realm.value().c_str()))) {
|
||||||
realm = dNew0(AuthRealm_t, 1);
|
realm = dNew0(AuthRealm_t, 1);
|
||||||
realm->name = dStrdup(data->auth_parse->realm);
|
realm->name = dStrdup(data->auth_parse->realm.value().c_str());
|
||||||
realm->paths = dList_new(1);
|
realm->paths = dList_new(1);
|
||||||
dList_append(host->realms, realm);
|
dList_append(host->realms, realm);
|
||||||
}
|
}
|
||||||
@@ -584,12 +575,12 @@ static void Auth_do_auth_dialog_cb(const char *user, const char *password,
|
|||||||
realm->username = dStrdup(user);
|
realm->username = dStrdup(user);
|
||||||
realm->nonce_count = 0;
|
realm->nonce_count = 0;
|
||||||
dFree(realm->nonce);
|
dFree(realm->nonce);
|
||||||
realm->nonce = dStrdup(data->auth_parse->nonce);
|
realm->nonce = dStrdup(data->auth_parse->nonce.value().c_str());
|
||||||
dFree(realm->opaque);
|
dFree(realm->opaque);
|
||||||
realm->opaque = dStrdup(data->auth_parse->opaque);
|
realm->opaque = dStrdup(data->auth_parse->opaque.value().c_str());
|
||||||
realm->algorithm = data->auth_parse->algorithm;
|
realm->algorithm = data->auth_parse->algorithm;
|
||||||
dFree(realm->domain);
|
dFree(realm->domain);
|
||||||
realm->domain = dStrdup(data->auth_parse->domain);
|
realm->domain = dStrdup(data->auth_parse->domain.value().c_str());
|
||||||
realm->qop = data->auth_parse->qop;
|
realm->qop = data->auth_parse->qop;
|
||||||
dFree(realm->cnonce);
|
dFree(realm->cnonce);
|
||||||
if (realm->qop != QOPNOTSET)
|
if (realm->qop != QOPNOTSET)
|
||||||
@@ -624,13 +615,13 @@ static int Auth_do_auth_dialog(const AuthParse_t *auth_parse,
|
|||||||
msg = dStrconcat("The server at ", URL_HOST(url), " requires a username"
|
msg = dStrconcat("The server at ", URL_HOST(url), " requires a username"
|
||||||
" and password for \"", auth_parse->realm, "\".\n\n"
|
" and password for \"", auth_parse->realm, "\".\n\n"
|
||||||
"Authentication scheme: ", typestr, NULL);
|
"Authentication scheme: ", typestr, NULL);
|
||||||
data = dNew(AuthDialogData_t, 1);
|
data = std::make_unique< AuthDialogData_t >().release();
|
||||||
data->auth_parse = auth_parse;
|
data->auth_parse = auth_parse;
|
||||||
data->url = a_Url_dup(url).release();
|
data->url = a_Url_dup(url).release();
|
||||||
ret = a_Dialog_user_password(title, msg, Auth_do_auth_dialog_cb, data);
|
ret = a_Dialog_user_password(title, msg, Auth_do_auth_dialog_cb, data);
|
||||||
dFree(title); dFree(msg);
|
dFree(title); dFree(msg);
|
||||||
delete const_cast< DilloUrl * >( data->url );
|
delete const_cast< DilloUrl * >( data->url );
|
||||||
dFree(data);
|
delete data;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user