[svn-commits] dlee: branch dlee/ari-authn r393205 - in /team/dlee/ari-authn: main/ res/ res...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Jun 28 14:30:14 CDT 2013


Author: dlee
Date: Fri Jun 28 14:30:12 2013
New Revision: 393205

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=393205
Log:
Addressed review feedback.

* Consolidated the allocation of the ast_http_auth object
* Cleaned up the parsing of username:password using strsep
* Allocated config objects using AO2_ALLOC_OPT_LOCK_NOLOCK
* Logging message cleanups

Modified:
    team/dlee/ari-authn/main/http.c
    team/dlee/ari-authn/res/res_stasis_http.c
    team/dlee/ari-authn/res/stasis_http/config.c

Modified: team/dlee/ari-authn/main/http.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-authn/main/http.c?view=diff&rev=393205&r1=393204&r2=393205
==============================================================================
--- team/dlee/ari-authn/main/http.c (original)
+++ team/dlee/ari-authn/main/http.c Fri Jun 28 14:30:12 2013
@@ -879,22 +879,30 @@
 	const char *password)
 {
 	RAII_VAR(struct ast_http_auth *, auth, NULL, ao2_cleanup);
+	size_t userid_len;
+	size_t password_len;
 
 	if (!userid || !password) {
 		ast_log(LOG_ERROR, "Invalid userid/password\n");
 		return NULL;
 	}
 
-	auth = ao2_alloc(sizeof(*auth), auth_dtor);
+	userid_len = strlen(userid) + 1;
+	password_len = strlen(password) + 1;
+
+	/* Allocate enough room to store everything in one memory block */
+	auth = ao2_alloc(sizeof(*auth) + userid_len + password_len, auth_dtor);
 	if (!auth) {
 		return NULL;
 	}
 
-	auth->userid = ast_strdup(userid);
-	auth->password = ast_strdup(password);
-	if (!auth->userid || !auth->password) {
-		return NULL;
-	}
+	/* Put the userid right after the struct */
+	auth->userid = (char *)(auth + 1);
+	strcpy(auth->userid, userid);
+
+	/* Put the password right after the userid */
+	auth->password = auth->userid + userid_len;
+	strcpy(auth->password, password);
 
 	ao2_ref(auth, +1);
 	return auth;
@@ -910,8 +918,9 @@
 	for (v = headers; v; v = v->next) {
 		const char *base64;
 		char decoded[256] = {};
+		char *username;
+		char *password;
 		int cnt;
-		char *colon;
 
 		if (strcasecmp("Authorization", v->name) != 0) {
 			continue;
@@ -941,14 +950,14 @@
 		ast_assert(cnt < sizeof(decoded));
 
 		/* Split the string at the colon */
-		colon = strchr(decoded, ':');
-		if (!colon) {
+		password = decoded;
+		username = strsep(&password, ":");
+		if (!password) {
 			ast_log(LOG_WARNING, "Invalid Authorization header\n");
 			return NULL;
 		}
-		*colon = '\0';
-
-		return auth_create(decoded, colon + 1);
+
+		return auth_create(username, password);
 	}
 
 	return NULL;

Modified: team/dlee/ari-authn/res/res_stasis_http.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-authn/res/res_stasis_http.c?view=diff&rev=393205&r1=393204&r2=393205
==============================================================================
--- team/dlee/ari-authn/res/res_stasis_http.c (original)
+++ team/dlee/ari-authn/res/res_stasis_http.c Fri Jun 28 14:30:12 2013
@@ -713,29 +713,22 @@
  */
 static struct ari_conf_user *authenticate_api_key(const char *api_key)
 {
-	RAII_VAR(char *, username, NULL, ast_free);
-	RAII_VAR(struct ari_conf_user *, user, NULL, ao2_cleanup);
-	char *colon;
-
-	username = ast_strdup(api_key);
-	if (!username) {
+	RAII_VAR(char *, copy, NULL, ast_free);
+	char *username;
+	char *password;
+
+	password = copy = ast_strdup(api_key);
+	if (!copy) {
 		return NULL;
 	}
 
-	/* Split the string */
-	colon = strchr(username, ':');
-	if (!colon) {
+	username = strsep(&password, ":");
+	if (!password) {
+		ast_log(LOG_WARNING, "Invalid api_key\n");
 		return NULL;
 	}
-	*colon = '\0';
-
-	user = ari_config_validate_user(username, colon + 1);
-	if (!user) {
-		return NULL;
-	}
-
-	ao2_ref(user, +1);
-	return user;
+
+	return ari_config_validate_user(username, password);
 }
 
 /*!
@@ -874,7 +867,7 @@
 	if (response.message && !ast_json_is_null(response.message)) {
 		ast_str_append(&response_headers, 0,
 			       "Content-type: application/json\r\n");
-		if (ast_json_dump_str_format(response.message, &response_body, 
+		if (ast_json_dump_str_format(response.message, &response_body,
 				conf->general->format) != 0) {
 			/* Error encoding response */
 			response.response_code = 500;

Modified: team/dlee/ari-authn/res/stasis_http/config.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-authn/res/stasis_http/config.c?view=diff&rev=393205&r1=393204&r2=393205
==============================================================================
--- team/dlee/ari-authn/res/stasis_http/config.c (original)
+++ team/dlee/ari-authn/res/stasis_http/config.c Fri Jun 28 14:30:12 2013
@@ -104,7 +104,8 @@
 
 	ast_debug(3, "Allocating user %s\n", cat);
 
-	user = ao2_alloc(sizeof(*user), user_dtor);
+	user = ao2_alloc_options(sizeof(*user), user_dtor,
+		AO2_ALLOC_OPT_LOCK_NOLOCK);
 	if (!user) {
 		return NULL;
 	}
@@ -175,12 +176,14 @@
 {
 	RAII_VAR(struct ari_conf *, cfg, NULL, ao2_cleanup);
 
-	cfg = ao2_alloc(sizeof(*cfg), conf_destructor);
+	cfg = ao2_alloc_options(sizeof(*cfg), conf_destructor,
+		AO2_ALLOC_OPT_LOCK_NOLOCK);
 	if (!cfg) {
 		return NULL;
 	}
 
-	cfg->general = ao2_alloc(sizeof(*cfg->general), NULL);
+	cfg->general = ao2_alloc_options(sizeof(*cfg->general), NULL,
+		AO2_ALLOC_OPT_LOCK_NOLOCK);
 	if (!cfg->general) {
 		return NULL;
 	}
@@ -191,11 +194,13 @@
 	ao2_ref(cfg, +1);
 	return cfg;
 }
+
+#define CONF_FILENAME "stasis_http.conf"
 
 /*! \brief The conf file that's processed for the module. */
 static struct aco_file conf_file = {
 	/*! The config file name. */
-	.filename = "stasis_http.conf",
+	.filename = CONF_FILENAME,
 	/*! The mapping object types to be processed. */
 	.types = ACO_TYPES(&general_option, &user_option),
 };
@@ -207,7 +212,8 @@
 {
 	struct ari_conf *res = ao2_global_obj_ref(confs);
 	if (!res) {
-		ast_log(LOG_ERROR, "Error accessing configuration\n");
+		ast_log(LOG_ERROR,
+			"Error obtaining config from " CONF_FILENAME "\n");
 	}
 	return res;
 }
@@ -230,7 +236,7 @@
 	}
 
 	if (ast_strlen_zero(user->password)) {
-		ast_log(LOG_ERROR,
+		ast_log(LOG_WARNING,
 			"User '%s' missing password; authentication failed\n",
 			user->username);
 		return NULL;
@@ -259,7 +265,7 @@
 	struct ari_conf_user *user = obj;
 
 	if (ast_strlen_zero(user->password)) {
-		ast_log(LOG_ERROR, "User '%s' missing password\n",
+		ast_log(LOG_WARNING, "User '%s' missing password\n",
 			user->username);
 	}
 




More information about the svn-commits mailing list