Commit 06b2254d authored by Timothée Jaussoin's avatar Timothée Jaussoin
Browse files

Move the account creation in AccountManager to FlexiAPI with a specific admin account

parent 0666fba9
......@@ -1383,7 +1383,7 @@ static void sound_config_read(LinphoneCore *lc) {
// Wait to have restored previous sound cards to notify list has been updated
// Otherwise app won't be able to change audio device in callback
linphone_core_notify_audio_devices_list_updated(lc);
/*
tmp=linphone_config_get_int(lc->config,"sound","play_lev",80);
linphone_core_set_play_level(lc,tmp);
......@@ -2692,7 +2692,7 @@ static void _linphone_core_init_account_creator_service(LinphoneCore *lc) {
// FlexiAPI specific endpoints
linphone_account_creator_service_set_login_linphone_account_cb(service, linphone_account_creator_send_token_flexiapi);
linphone_account_creator_service_set_create_account_with_token_cb(service, linphone_account_creator_create_account_with_token);
linphone_account_creator_service_set_create_account_with_token_cb(service, linphone_account_creator_create_account_with_token_flexiapi);
}
#endif
......@@ -5143,7 +5143,7 @@ LinphoneStatus linphone_core_set_ringer_device(LinphoneCore *lc, const char * de
LinphoneStatus linphone_core_set_playback_device(LinphoneCore *lc, const char * devid) {
MSSndCard *card = get_card_from_string_id(devid, MS_SND_CARD_CAP_PLAYBACK, lc->factory);
if (lc->sound_conf.play_sndcard) {
if (lc->sound_conf.play_sndcard) {
ms_snd_card_unref(lc->sound_conf.play_sndcard);
lc->sound_conf.play_sndcard = NULL;
}
......
......@@ -40,6 +40,7 @@ using namespace std;
FlexiAPIClient::FlexiAPIClient(LinphoneCore *lc) {
mCore = lc;
mApiKey = nullptr;
mTest = false;
// Assign the core there as well to keep it in the callback contexts
mRequestCallbacks.core = lc;
......@@ -238,6 +239,11 @@ FlexiAPIClient *FlexiAPIClient::setApiKey(const char *key) {
return this;
}
FlexiAPIClient *FlexiAPIClient::setTest(bool test) {
mTest = test;
return this;
}
/**
* Callback requests
*/
......@@ -280,6 +286,9 @@ void FlexiAPIClient::prepareRequest(string path, string type, JsonParams params)
belle_sip_message_add_header(BELLE_SIP_MESSAGE(req), belle_http_header_create("From", addr));
ms_free(addr);
} else if (mTest) {
belle_sip_message_add_header(BELLE_SIP_MESSAGE(req), belle_http_header_create("From", "sip:admin_test@sip.example.org"));
belle_sip_message_add_header(BELLE_SIP_MESSAGE(req), belle_http_header_create("x-api-key", "no_secret_at_all"));
}
if (!params.empty()) {
......
......@@ -115,6 +115,7 @@ class LINPHONE_PUBLIC FlexiAPIClient : public enable_shared_from_this<FlexiAPICl
// Authentication
FlexiAPIClient *setApiKey(const char *key);
FlexiAPIClient *setTest(bool test);
// Callbacks handlers
FlexiAPIClient *then(function<void(Response)> success);
......@@ -124,6 +125,7 @@ class LINPHONE_PUBLIC FlexiAPIClient : public enable_shared_from_this<FlexiAPICl
LinphoneCore *mCore;
Callbacks mRequestCallbacks;
const char *mApiKey;
bool mTest;
void prepareRequest(string path);
void prepareRequest(string path, string type);
......
......@@ -415,7 +415,7 @@ LinphoneAccountCreatorStatus linphone_account_creator_send_token_flexiapi(Linpho
return LinphoneAccountCreatorStatusRequestOk;
}
LinphoneAccountCreatorStatus linphone_account_creator_create_account_with_token(LinphoneAccountCreator *creator) {
LinphoneAccountCreatorStatus linphone_account_creator_create_account_with_token_flexiapi(LinphoneAccountCreator *creator) {
fill_domain_and_algorithm_if_needed(creator);
auto flexiAPIClient = make_shared<FlexiAPIClient>(creator->core);
......@@ -438,5 +438,36 @@ LinphoneAccountCreatorStatus linphone_account_creator_create_account_with_token(
return LinphoneAccountCreatorStatusRequestFailed;
});
return LinphoneAccountCreatorStatusRequestOk;
}
/**
* The following method is only available if APP_EVERYONE_IS_ADMIN is enabled on FlexiAPI
*/
LinphoneAccountCreatorStatus linphone_account_creator_admin_create_account_flexiapi(LinphoneAccountCreator *creator) {
fill_domain_and_algorithm_if_needed(creator);
auto flexiAPIClient = make_shared<FlexiAPIClient>(creator->core);
flexiAPIClient->setTest(TRUE)
->adminAccountCreate(creator->username, creator->password, "MD5", creator->domain, true)
->then([creator](FlexiAPIClient::Response response) {
NOTIFY_IF_EXIST_ACCOUNT_CREATOR(create_account, creator, LinphoneAccountCreatorStatusAccountCreated,
response.body.c_str());
return LinphoneAccountCreatorStatusRequestOk;
})
->error([creator](FlexiAPIClient::Response response) {
if (response.code == 422) {
NOTIFY_IF_EXIST_ACCOUNT_CREATOR(create_account, creator, LinphoneAccountCreatorStatusMissingArguments,
response.body.c_str())
} else {
NOTIFY_IF_EXIST_ACCOUNT_CREATOR(create_account, creator, LinphoneAccountCreatorStatusUnexpectedError,
response.body.c_str())
}
return LinphoneAccountCreatorStatusRequestFailed;
});
return LinphoneAccountCreatorStatusRequestOk;
}
\ No newline at end of file
......@@ -73,7 +73,6 @@ LinphoneProxyConfig * linphone_account_creator_configure(const LinphoneAccountCr
return linphone_account_creator_create_proxy_config(creator);
}
LinphoneAccountCreatorUsernameStatus linphone_account_creator_set_username(LinphoneAccountCreator *creator, const char *username) {
int min_length = linphone_config_get_int(linphone_core_get_config(creator->core), "assistant", "username_min_length", -1);
int max_length = linphone_config_get_int(linphone_core_get_config(creator->core), "assistant", "username_max_length", -1);
......@@ -86,7 +85,7 @@ LinphoneAccountCreatorUsernameStatus linphone_account_creator_set_username(Linph
return LinphoneAccountCreatorUsernameStatusTooShort;
} else if (max_length > 0 && strlen(username) > (size_t)max_length) {
return LinphoneAccountCreatorUsernameStatusTooLong;
} else if (use_phone_number && !linphone_proxy_config_is_phone_number(NULL, username)) {
} else if (use_phone_number && !linphone_account_is_phone_number(NULL, username)) {
return LinphoneAccountCreatorUsernameStatusInvalid;
} else if (regex && !bctbx_is_matching_regex(username, regex)) {
return LinphoneAccountCreatorUsernameStatusInvalidCharacters;
......
......@@ -273,7 +273,14 @@ LINPHONE_PUBLIC LinphoneAccountCreatorStatus linphone_account_creator_send_token
* @param[in] creator LinphoneAccountCreator object
* @return LinphoneAccountCreatorStatusRequestOk if everything is OK, or a specific error otherwise.
**/
LINPHONE_PUBLIC LinphoneAccountCreatorStatus linphone_account_creator_create_account_with_token(LinphoneAccountCreator *creator);
LINPHONE_PUBLIC LinphoneAccountCreatorStatus linphone_account_creator_create_account_with_token_flexiapi(LinphoneAccountCreator *creator);
/**
* Send an FlexiAPI request to create an account as an admin
* @param[in] creator LinphoneAccountCreator object
* @return LinphoneAccountCreatorStatusRequestOk if everything is OK, or a specific error otherwise.
**/
LINPHONE_PUBLIC LinphoneAccountCreatorStatus linphone_account_creator_admin_create_account_flexiapi(LinphoneAccountCreator *creator);
#ifdef __cplusplus
}
......
......@@ -268,7 +268,7 @@ static void server_account_create_account_with_token(void) {
linphone_account_creator_cbs_set_create_account(cbs, account_creator_cb);
BC_ASSERT_EQUAL(
linphone_account_creator_create_account_with_token(creator),
linphone_account_creator_create_account_with_token_flexiapi(creator),
LinphoneAccountCreatorStatusRequestOk,
LinphoneAccountCreatorStatus,
"%i");
......
......@@ -222,34 +222,7 @@ static void account_created_in_db_cb(LinphoneAccountCreator *creator, LinphoneAc
}
}
static void get_confirmation_key_cb(LinphoneAccountCreator *creator, LinphoneAccountCreatorStatus status, const char *resp){
AccountCreatorState *state = linphone_account_creator_get_user_data(creator);
switch(status){
case LinphoneAccountCreatorStatusRequestOk:
state->confirmation_key_received = TRUE;
break;
default:
ms_warning("Confirmation key not received for %s.", linphone_account_creator_get_username(creator));
state->confirmation_key_received = FALSE;
break;
}
}
static void account_activated_cb(LinphoneAccountCreator *creator, LinphoneAccountCreatorStatus status, const char *resp){
AccountCreatorState *state = linphone_account_creator_get_user_data(creator);
switch(status){
case LinphoneAccountCreatorStatusAccountActivated:
case LinphoneAccountCreatorStatusAccountAlreadyActivated:
state->account_activated = TRUE;
break;
default:
ms_message("Account not activated for %s.", linphone_account_creator_get_username(creator));
state->account_activated = FALSE;
break;
}
}
void account_create_in_db(Account *account, LinphoneProxyConfig *cfg, const char *xmlrpc_url){
void account_create_in_server_db(Account *account, LinphoneProxyConfig *cfg){
LinphoneCoreCbs *cbs = linphone_factory_create_core_cbs(linphone_factory_get());
linphone_core_cbs_set_registration_state_changed(cbs, account_created_on_server_cb);
LinphoneCore *lc = configure_lc_from(cbs, bc_tester_get_resource_dir_prefix(), NULL, account);
......@@ -260,10 +233,18 @@ void account_create_in_db(Account *account, LinphoneProxyConfig *cfg, const char
tr.tls_port = LC_SIP_TRANSPORT_RANDOM;
linphone_core_set_sip_transports(lc, &tr);
LinphoneAccountCreator *creator = linphone_account_creator_new(lc, xmlrpc_url);
LinphoneAccountCreator *creator = linphone_account_creator_new(lc, "");
LinphoneAccountCreatorCbs *creator_cbs = linphone_account_creator_get_callbacks(creator);
LinphoneProxyConfig *default_cfg = linphone_core_get_default_proxy_config(lc);
linphone_config_set_string(linphone_core_get_config(lc), "sip", "flexiapi_url",
linphone_config_get_string(
linphone_core_get_config(lc),
"sip",
"flexiapi_url",
"http://fs-test.linphone.org/flexiapi/api/"
)
);
linphone_account_creator_set_proxy_config(creator, cfg);
linphone_account_creator_service_set_user_data(linphone_account_creator_get_service(creator), (void*)LinphoneAccountCreatorStatusAccountCreated);
......@@ -278,7 +259,11 @@ void account_create_in_db(Account *account, LinphoneProxyConfig *cfg, const char
AccountCreatorState state = {0};
// create account
// Attache the event to FlexiAPI and add the callbacks
linphone_account_creator_service_set_create_account_cb(linphone_account_creator_get_service(creator), linphone_account_creator_admin_create_account_flexiapi);
linphone_account_creator_add_callbacks(creator, creator_cbs);
// Create account
linphone_account_creator_cbs_set_create_account(creator_cbs, account_created_in_db_cb);
linphone_account_creator_set_username(creator, username);
linphone_account_creator_set_password(creator, password);
......@@ -301,7 +286,7 @@ void account_create_in_db(Account *account, LinphoneProxyConfig *cfg, const char
};
}
if (linphone_account_creator_create_account(creator) == LinphoneAccountCreatorStatusMissingCallbacks) {
if (linphone_account_creator_create_account(creator) == LinphoneAccountCreatorStatusMissingCallbacks) {
ms_fatal("Could not create account %s on db (missing callbacks)", linphone_proxy_config_get_identity(cfg));
}
......@@ -312,23 +297,6 @@ void account_create_in_db(Account *account, LinphoneProxyConfig *cfg, const char
linphone_core_add_auth_info(lc, ai);
linphone_auth_info_unref(ai);
// get confirmation key
linphone_account_creator_cbs_set_confirmation_key(creator_cbs, get_confirmation_key_cb);
linphone_account_creator_get_confirmation_key(creator);
if (wait_for_until(lc, NULL, &state.confirmation_key_received, TRUE, 15000) == FALSE)
ms_fatal("Could not get confirmation key for account %s", linphone_proxy_config_get_identity(cfg));
// activate account
linphone_account_creator_cbs_set_activate_account(creator_cbs, account_activated_cb);
if (linphone_account_creator_get_phone_number(creator))
linphone_account_creator_activate_phone_account_linphone_xmlrpc(creator);
else
linphone_account_creator_activate_email_account_linphone_xmlrpc(creator);
if (wait_for_until(lc, NULL, &state.account_activated, TRUE, 15000) == FALSE)
ms_fatal("Could not activate account %s", linphone_proxy_config_get_identity(cfg));
linphone_account_creator_set_proxy_config(creator, default_cfg);
bctbx_free(email);
......@@ -374,13 +342,7 @@ static LinphoneAddress *account_manager_check_account(AccountManager *m, Linphon
if (liblinphonetester_no_account_creator) {
account_create_on_server(account, cfg, phone_alias);
} else {
const char *xmlrpc_url = linphone_config_get_string(
linphone_core_get_config(lc),
"misc",
"xmlrpc_server_url",
"http://subscribe.example.org:8082/flexisip-account-manager/xmlrpc.php"
);
account_create_in_db(account, cfg, xmlrpc_url);
account_create_in_server_db(account, cfg);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment