library/src/network/httpd/BP-HTTPdSessions.cc
// Add a session for a user.
Add a session for a user.
Add a session for a user .
P_BP_HTTPD_USER_SESSION BP_HTTPdSessionAdd
(
		P_BP_HTTPD_SESSION           httpd,
		BP_HTTPD_USER_SESSION_TYPE   session_type,
		P_BP_HTTPD_USER              user,
		size_t                       session_lifetime_in_seconds,
		size_t                       xsrf_token_count
)
{
	if(!httpd)
		return NULL;
	if(!httpd->system_hreg)
		return NULL;
	if(!session_lifetime_in_seconds)
		return NULL;
	if(!user)
		return NULL;
	if(!xsrf_token_count)
		return NULL;
	char * table_path[10];
	BP_ZERO_STACK(table_path);
	P_BP_HASH_TABLE_KEYED_ENTRY tmp_lookup = NULL;
	BP_HTTPD_ENTER_CRITICAL_SECTION;
	table_path[0] = "sessions";
	table_path[1] = NULL;
	char * new_session_uuid = BP_GenRandomUUID();
	BP_ERROR_T err_status = BP_HashRegAddDataToTableByPath
	(
		httpd->system_hreg,
		table_path,
		new_session_uuid,
		BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB,
		NULL,
		sizeof(BP_HTTPD_USER_SESSION)
	);
	P_BP_HTTPD_USER_SESSION user_session = BP_HTTPdSessionLookup(httpd, new_session_uuid);
	if(!user_session)
	{
		bpfree(new_session_uuid);
		BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	user_session->identifier = new_session_uuid;
	user_session->user = user;
	user_session->type = session_type;
	user_session->life_in_seconds = session_lifetime_in_seconds;
	user_session->issued_time = (size_t) BP_TimeGetSecondsSinceUnixEpoch();
	user->session = (void *) user_session;
	user_session->xsrf_reg = BP_CreateHashTableRegistry();
	if(!user_session->xsrf_reg)
	{
		BP_HTTPdSessionDestroy(	httpd, user_session);
		BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	char *xsrf_table_add_path[10];
	BP_ZERO_STACK(xsrf_table_add_path);
	xsrf_table_add_path[0] = "tokens";
	BP_HashRegAddTableByPath(user_session->xsrf_reg, (char **) &xsrf_table_add_path);
	if(!BP_HTTPdSessionXSRFTokensAdd(user_session, xsrf_token_count))
	{
		BP_HTTPdSessionDestroy(	httpd, user_session);
		BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	printf("\n HERE HERE ADDED TOKENS DISPLAYING REGISTRY!!");
	BP_HashRegDisplay(user_session->xsrf_reg, BP_FALSE, BP_TRUE);
	%%%%%%%%%%%
	%%%%%%%%%%%
	user_session->session_data_hreg = BP_CreateHashTableRegistry();
	if(!user_session->session_data_hreg)
	{
		BP_HTTPdSessionDestroy(	httpd, user_session );
		BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	%%%%%%%%%%
	%%%%%%%%%%
	 for api key
	char * api_key = BP_GenRandomUUID();
	BP_HTTPD_API_KEY_PRIVS api_key_privs;
	BP_ZERO_STACK(api_key_privs);
	printf("\n [APIKEY] Adding new API key!: %s", api_key);
	printf("\n");
	printf("\n");
	if(!BP_HTTPdSessionAddAPIKey(httpd, user_session, api_key, &api_key_privs))
	{
		bpfree(api_key);
		BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	bpfree(api_key);
	BP_HTTPD_LEAVE_CRITICAL_SECTION;
	return user_session;
}
P_BP_HTTPD_USER_SESSION BP_HTTPdSessionAdd ( P_BP_HTTPD_SESSION httpd , BP_HTTPD_USER_SESSION_TYPE session_type , P_BP_HTTPD_USER user , size_t session_lifetime_in_seconds , size_t xsrf_token_count ) { if ( ! httpd ) return NULL ; if ( ! httpd -> system_hreg ) return NULL ; if ( ! session_lifetime_in_seconds ) return NULL ; if ( ! user ) return NULL ; if ( ! xsrf_token_count ) return NULL ; char * table_path [ 10 ] ; BP_ZERO_STACK ( table_path ) ; P_BP_HASH_TABLE_KEYED_ENTRY tmp_lookup = NULL ; BP_HTTPD_ENTER_CRITICAL_SECTION ; table_path [ 0 ] = " " ; table_path [ 1 ] = NULL ; char * new_session_uuid = BP_GenRandomUUID ( ) ; BP_ERROR_T err_status = BP_HashRegAddDataToTableByPath ( httpd -> system_hreg , table_path , new_session_uuid , BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB , NULL , sizeof ( BP_HTTPD_USER_SESSION ) ) ; P_BP_HTTPD_USER_SESSION user_session = BP_HTTPdSessionLookup ( httpd , new_session_uuid ) ; if ( ! user_session ) { bpfree ( new_session_uuid ) ; BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } user_session -> identifier = new_session_uuid ; user_session -> user = user ; user_session -> type = session_type ; user_session -> life_in_seconds = session_lifetime_in_seconds ; user_session -> issued_time = ( size_t ) BP_TimeGetSecondsSinceUnixEpoch ( ) ; user -> session = ( void * ) user_session ; user_session -> xsrf_reg = BP_CreateHashTableRegistry ( ) ; if ( ! user_session -> xsrf_reg ) { BP_HTTPdSessionDestroy ( httpd , user_session ) ; BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } char * xsrf_table_add_path [ 10 ] ; BP_ZERO_STACK ( xsrf_table_add_path ) ; xsrf_table_add_path [ 0 ] = " " ; BP_HashRegAddTableByPath ( user_session -> xsrf_reg , ( char * * ) & xsrf_table_add_path ) ; if ( ! BP_HTTPdSessionXSRFTokensAdd ( user_session , xsrf_token_count ) ) { BP_HTTPdSessionDestroy ( httpd , user_session ) ; BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } printf ( " \n " ) ; BP_HashRegDisplay ( user_session -> xsrf_reg , BP_FALSE , BP_TRUE ) ; user_session -> session_data_hreg = BP_CreateHashTableRegistry ( ) ; if ( ! user_session -> session_data_hreg ) { BP_HTTPdSessionDestroy ( httpd , user_session ) ; BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } char * api_key = BP_GenRandomUUID ( ) ; BP_HTTPD_API_KEY_PRIVS api_key_privs ; BP_ZERO_STACK ( api_key_privs ) ; printf ( " \n " , api_key ) ; printf ( " \n " ) ; printf ( " \n " ) ; if ( ! BP_HTTPdSessionAddAPIKey ( httpd , user_session , api_key , & api_key_privs ) ) { bpfree ( api_key ) ; BP_HTTPD_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } bpfree ( api_key ) ; BP_HTTPD_LEAVE_CRITICAL_SECTION ; return user_session ; }
Add a session for a user.
Add a session for a user .
// ensure we have a daemon // ensure we have a system registry // ensure we have a session length (in seconds) // ensure we have a user // xsrf tokens are required whether you use them or not // declare dynamic table path // declare the lookup pointer // enter critical section // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Attempt to Create New User Structure %%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set the initial path // generate random uuid // attempt to add data to the table // attempt to lookup the user session after adding to registry // set the identifier here (don't ht_strdup, it's freed directly using bpfree in the session destructor) // set the session user here // set the session type // set default session life to 12 hours // user_session->life_in_seconds = BP_HOUR * 12; // set issued time // set cyclical lookup for the session (cast as void pointer) // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Create XSRF Registry %%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create xsrf registry // destroy session before exit // exit here // add the table path // set tokens path // add table // add xsrf tokens to new session // destroy session before exit // exit here // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Create User Session Persistent Data Registry %%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create persistent data storage registry // destroy session before exit // exit here // dbg optional // printf("\n New user session created ok?: %p", user_session); // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Add API Key %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // generate random uuid for api key // api key privileges set here (they are copied during addition) // debug apikey // __asm("int3"); // attempt to add random api key // free api key buffer // leave the critical section // return the new user session