+void yf::Zoom::Frontend::auth(mp::Package &package, Z_InitRequest *req,
+ int *error, char **addinfo, ODR odr)
+{
+ if (m_p->torus_auth_url.length() == 0)
+ return;
+
+ std::string user;
+ std::string password;
+ if (req->idAuthentication)
+ {
+ Z_IdAuthentication *auth = req->idAuthentication;
+ switch (auth->which)
+ {
+ case Z_IdAuthentication_open:
+ if (auth->u.open)
+ {
+ const char *cp = strchr(auth->u.open, '/');
+ if (cp)
+ {
+ user.assign(auth->u.open, cp - auth->u.open);
+ password.assign(cp + 1);
+ }
+ }
+ break;
+ case Z_IdAuthentication_idPass:
+ if (auth->u.idPass->userId)
+ user.assign(auth->u.idPass->userId);
+ if (auth->u.idPass->password)
+ password.assign(auth->u.idPass->password);
+ break;
+ }
+ }
+ // need to dig out IP!!
+
+ if (user.length() == 0 || password.length() == 0)
+ {
+ *error = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
+ *addinfo = odr_strdup(odr, "User and password required");
+ return;
+ }
+
+ std::string dummy_db;
+ std::string dummy_realm;
+ std::string torus_query = "userName==" + user +
+ " and password==" + password;
+ xmlDoc *doc = mp::get_searchable(package, m_p->torus_auth_url, dummy_db,
+ torus_query, dummy_realm, m_p->proxy);
+ if (!doc)
+ {
+ *error = YAZ_BIB1_UNSPECIFIED_ERROR;
+ *addinfo = odr_strdup(odr, "Torus server unavailable or "
+ "incorrectly configured");
+ return;
+ }
+ const xmlNode *ptr = xmlDocGetRootElement(doc);
+ if (ptr && ptr->type == XML_ELEMENT_NODE)
+ {
+ if (strcmp((const char *) ptr->name, "records") == 0)
+ {
+ ptr = ptr->children;
+ while (ptr && ptr->type != XML_ELEMENT_NODE)
+ ptr = ptr->next;
+ }
+ if (ptr && strcmp((const char *) ptr->name, "record") == 0)
+ {
+ ptr = ptr->children;
+ while (ptr && ptr->type != XML_ELEMENT_NODE)
+ ptr = ptr->next;
+ }
+ if (ptr && strcmp((const char *) ptr->name, "layer") == 0)
+ {
+ ptr = ptr->children;
+ while (ptr && ptr->type != XML_ELEMENT_NODE)
+ ptr = ptr->next;
+ }
+ while (ptr)
+ {
+ if (ptr && ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "identityId"))
+ break;
+ ptr = ptr->next;
+ }
+ }
+ if (!ptr)
+ {
+ *error = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
+ return;
+ }
+ session_realm = mp::xml::get_text(ptr);
+}
+