library/src/network/httpd/BP-HTTPdSessions.cc
// attempts to add an api key to a user session within the hash registry
attempts to add an api key to a user session within the hash registry
attempts to add an api key to a user session within the hash registry
BP_ERROR_T BP_HTTPdSessionAddAPIKey
(
	P_BP_HTTPD_SESSION           httpd,
	P_BP_HTTPD_USER_SESSION      user_session,
	char *                       key_identifier,
	P_BP_HTTPD_API_KEY_PRIVS privs
)
{
	if(!httpd)
		return ERR_FAILURE;
	if(!user_session)
		return ERR_FAILURE;
	if(!key_identifier)
		return ERR_FAILURE;
	if(!privs)
		return ERR_FAILURE;
	if(!BP_StringIsReasonablePrintableString(key_identifier, bpstrlen(key_identifier), BP_FALSE, BP_FALSE))
		return ERR_FAILURE;
	BP_HTTPD_ENTER_CRITICAL_SECTION;
	if(BP_HTTPdSessionLookupAPIKeyByIdentifier(httpd, key_identifier))
		BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	char * table_addition_path[]
	{
		"api_keys",
		NULL
	};
	BP_ERROR_T err_ret = BP_HashRegAddDataToTableByPath
	(
		httpd->system_hreg,
		(char **) &table_addition_path,
		key_identifier,
		BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB,
		NULL,
		sizeof(BP_HTTPD_API_KEY)
	);
	if(!err_ret)
	{
		BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	}
	P_BP_HTTPD_API_KEY new_key = BP_HTTPdSessionLookupAPIKeyByIdentifier
	(
		httpd,
		key_identifier
	);
	new_key->key_identifier = (char *) ht_strndup(httpd->system_hreg, key_identifier, bpstrlen(key_identifier));
	new_key->user_session = (P_BP_HTTPD_USER_SESSION)  user_session;
	memcpy(&new_key->privs, privs, sizeof(BP_HTTPD_API_KEY_PRIVS));
	if(!new_key)
		BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	user_session->api_key = new_key;
	BP_HTTPD_LEAVE_CRITICAL_SECTION;
	return ERR_SUCCESS;
}
BP_ERROR_T BP_HTTPdSessionAddAPIKey ( P_BP_HTTPD_SESSION httpd , P_BP_HTTPD_USER_SESSION user_session , char * key_identifier , P_BP_HTTPD_API_KEY_PRIVS privs ) { if ( ! httpd ) return ERR_FAILURE ; if ( ! user_session ) return ERR_FAILURE ; if ( ! key_identifier ) return ERR_FAILURE ; if ( ! privs ) return ERR_FAILURE ; if ( ! BP_StringIsReasonablePrintableString ( key_identifier , bpstrlen ( key_identifier ) , BP_FALSE , BP_FALSE ) ) return ERR_FAILURE ; BP_HTTPD_ENTER_CRITICAL_SECTION ; if ( BP_HTTPdSessionLookupAPIKeyByIdentifier ( httpd , key_identifier ) ) BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; char * table_addition_path [ ] { " " , NULL } ; BP_ERROR_T err_ret = BP_HashRegAddDataToTableByPath ( httpd -> system_hreg , ( char * * ) & table_addition_path , key_identifier , BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB , NULL , sizeof ( BP_HTTPD_API_KEY ) ) ; if ( ! err_ret ) { BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; } P_BP_HTTPD_API_KEY new_key = BP_HTTPdSessionLookupAPIKeyByIdentifier ( httpd , key_identifier ) ; new_key -> key_identifier = ( char * ) ht_strndup ( httpd -> system_hreg , key_identifier , bpstrlen ( key_identifier ) ) ; new_key -> user_session = ( P_BP_HTTPD_USER_SESSION ) user_session ; memcpy ( & new_key -> privs , privs , sizeof ( BP_HTTPD_API_KEY_PRIVS ) ) ; if ( ! new_key ) BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; user_session -> api_key = new_key ; BP_HTTPD_LEAVE_CRITICAL_SECTION ; return ERR_SUCCESS ; }
attempts to add an api key to a user session within the hash registry
attempts to add an api key to a user session within the hash registry
// ensure we have basic parameters // run reasonable check on identifier // enter critical section // if the api key already exists, exit immediately // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Attempt API Key Addition %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set table addition path // add api key to table // if api key addition failed, return immediately // ensure we can lookup the key before returning // api key identifier // set user session // copy in privileges // ensure we have looked up our key ok // set apikey in session here // leave critical section // return indicating success