lasso_saml20_login_process_federation(LassoLogin *login, gboolean is_consent_obtained)
{
	LassoProfile *profile = LASSO_PROFILE(login);
	LassoSamlp2NameIDPolicy *name_id_policy;
	char *name_id_policy_format = NULL;
	LassoFederation *federation;
	const char *name_id_sp_name_qualifier = NULL;
	LassoProvider *remote_provider;
	int rc = 0;

	/* verify if identity already exists else create it */
	if (profile->identity == NULL) {
		profile->identity = lasso_identity_new();
	}

	remote_provider = lasso_server_get_provider(profile->server, profile->remote_providerID);
	if (! LASSO_IS_PROVIDER(remote_provider)) {
		goto_cleanup_with_rc (LASSO_PROFILE_ERROR_UNKNOWN_PROVIDER);
	}

	if (! LASSO_IS_SAMLP2_AUTHN_REQUEST(profile->request)) {
		goto_cleanup_with_rc(critical_error(LASSO_PROFILE_ERROR_INVALID_REQUEST));
	}

	name_id_policy = ((LassoSamlp2AuthnRequest*)profile->request)->NameIDPolicy;

	if (name_id_policy) {
		name_id_policy_format = name_id_policy->Format;
	}

	if (! name_id_policy_format) {
		name_id_policy_format = lasso_provider_get_default_name_id_format(remote_provider);
	}

	lasso_assign_string(login->nameIDPolicy, name_id_policy_format);

	if (lasso_saml20_login_must_ask_for_consent_private(login) && !is_consent_obtained) {
		goto_cleanup_with_rc (LASSO_LOGIN_ERROR_CONSENT_NOT_OBTAINED)
	}
	if (lasso_strisnotequal(name_id_policy_format,LASSO_SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT)) {
		/* non persistent case, TRANSIENT is handled by lasso_login_build_assertion() and
		 * other format are the sole responsibility of the caller */
		goto_cleanup_with_rc (0)
	}

	/* PERSISTENT case, try to federation or find an existing federation */
	name_id_sp_name_qualifier = lasso_provider_get_sp_name_qualifier(remote_provider);
	if (name_id_sp_name_qualifier == NULL) {
		goto_cleanup_with_rc (LASSO_PROFILE_ERROR_UNKNOWN_PROVIDER);
	}

	/* search a federation in the identity */
	federation = lasso_identity_get_federation(profile->identity, name_id_sp_name_qualifier);
	if (! federation && ( ! name_id_policy || name_id_policy->AllowCreate == FALSE)) {
		goto_cleanup_with_rc (LASSO_LOGIN_ERROR_FEDERATION_NOT_FOUND)
	}
	if (! federation && name_id_policy && name_id_policy->AllowCreate) {
		federation = lasso_federation_new(name_id_sp_name_qualifier);
		lasso_saml20_federation_build_local_name_identifier(federation,
				LASSO_PROVIDER(profile->server)->ProviderID,
				LASSO_SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT,
				NULL);
		lasso_assign_string(LASSO_SAML2_NAME_ID(federation->local_nameIdentifier)->SPNameQualifier,
				name_id_sp_name_qualifier);
		lasso_identity_add_federation(profile->identity, federation);
	}

	lasso_assign_gobject(profile->nameIdentifier, federation->local_nameIdentifier);

cleanup:
	return rc;
}
lasso_saml20_login_process_federation(LassoLogin *login, gboolean is_consent_obtained)
{
	LassoProfile *profile = LASSO_PROFILE(login);
	LassoSamlp2NameIDPolicy *name_id_policy;
	char *name_id_policy_format = NULL;
	LassoFederation *federation;
	const char *name_id_sp_name_qualifier = NULL;
	LassoProvider *remote_provider;
	int rc = 0;

	 
	if (profile->identity == NULL) {
		profile->identity = lasso_identity_new();
	}

	remote_provider = lasso_server_get_provider(profile->server, profile->remote_providerID);
	if (! LASSO_IS_PROVIDER(remote_provider)) {
		goto_cleanup_with_rc (LASSO_PROFILE_ERROR_UNKNOWN_PROVIDER);
	}

	if (! LASSO_IS_SAMLP2_AUTHN_REQUEST(profile->request)) {
		goto_cleanup_with_rc(critical_error(LASSO_PROFILE_ERROR_INVALID_REQUEST));
	}

	name_id_policy = ((LassoSamlp2AuthnRequest*)profile->request)->NameIDPolicy;

	if (name_id_policy) {
		name_id_policy_format = name_id_policy->Format;
	}

	if (! name_id_policy_format) {
		name_id_policy_format = lasso_provider_get_default_name_id_format(remote_provider);
	}

	lasso_assign_string(login->nameIDPolicy, name_id_policy_format);

	if (lasso_saml20_login_must_ask_for_consent_private(login) && !is_consent_obtained) {
		goto_cleanup_with_rc (LASSO_LOGIN_ERROR_CONSENT_NOT_OBTAINED)
	}
	if (lasso_strisnotequal(name_id_policy_format,LASSO_SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT)) {
		 
		goto_cleanup_with_rc (0)
	}

	 
	name_id_sp_name_qualifier = lasso_provider_get_sp_name_qualifier(remote_provider);
	if (name_id_sp_name_qualifier == NULL) {
		goto_cleanup_with_rc (LASSO_PROFILE_ERROR_UNKNOWN_PROVIDER);
	}

	 
	federation = lasso_identity_get_federation(profile->identity, name_id_sp_name_qualifier);
	if (! federation && ( ! name_id_policy || name_id_policy->AllowCreate == FALSE)) {
		goto_cleanup_with_rc (LASSO_LOGIN_ERROR_FEDERATION_NOT_FOUND)
	}
	if (! federation && name_id_policy && name_id_policy->AllowCreate) {
		federation = lasso_federation_new(name_id_sp_name_qualifier);
		lasso_saml20_federation_build_local_name_identifier(federation,
				LASSO_PROVIDER(profile->server)->ProviderID,
				LASSO_SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT,
				NULL);
		lasso_assign_string(LASSO_SAML2_NAME_ID(federation->local_nameIdentifier)->SPNameQualifier,
				name_id_sp_name_qualifier);
		lasso_identity_add_federation(profile->identity, federation);
	}

	lasso_assign_gobject(profile->nameIdentifier, federation->local_nameIdentifier);

cleanup:
	return rc;
}