Beispiel #1
0
int CSoapService::processHeader(CHeader* header, IEspContext* ctx)
{
    int num = header->getNumBlocks();

    if(ctx == NULL)
        return 0;

    int returnValue = 0;
    bool authenticated = !ctx->toBeAuthenticated();
    for (int i = 0; i < num; i++)
    {
        IRpcMessage* oneblock = header->getHeaderBlock(i);
        if(oneblock == NULL)
            continue;
        if(strcmp(oneblock->get_name(), "Security") == 0)
        {
            bool encodeXML = oneblock->getEncodeXml();
            oneblock->setEncodeXml(false);
            StringBuffer username, password,realm;
            oneblock->get_value("UsernameToken/Username", username);
            oneblock->get_value("UsernameToken/Password", password);
            oneblock->get_value("RealmToken/Realm", realm);
            oneblock->setEncodeXml(encodeXML);
            //DBGLOG("username=%s, password=%s", username.str(), password.str());
            if(username.length() > 0)
            {
                ctx->setUserID(username.str());
                ctx->setPassword(password.str());
                if(realm.length()>0)
                    ctx->setRealm(realm.str());
                
                ISecManager* secmgr = ctx->querySecManager();
                if(secmgr != NULL)
                {
                    ISecUser *user = ctx->queryUser();
                    if(user==NULL)
                    {
                        user = secmgr->createUser(username.str());
                        ctx->setUser(user);
                    }
                    if(user == NULL)
                    {
                        WARNLOG("Couldn't create ISecUser object for %s", username.str());
                    }
                    user->setName(username.str());
                    user->credentials().setPassword(password.str());
                    if(realm.length()>0)
                        user->setRealm(realm.str());
                }

                if(ctx->toBeAuthenticated())
                {
                    if(stricmp(m_soapbinding->getTransportType(), "http") == 0)
                    {
                        EspHttpBinding* httpbinding = dynamic_cast<EspHttpBinding*>(m_soapbinding.get());
                        authenticated = httpbinding->doAuth(ctx);
                    }
                    else
                    {
                        authenticated = false;
                    }
                    if(!authenticated)
                        returnValue = SOAP_AUTHENTICATION_ERROR;
                    break;
                }
            }
        }
    }

    if (returnValue == 0)
    {
        if (authenticated)
            return 0;
        returnValue = SOAP_AUTHENTICATION_REQUIRED;
    }

    StringBuffer peerStr;
    ctx->getPeer(peerStr);
    const char* userId = ctx->queryUserId();
    VStringBuffer msg("SOAP request from %s@%s.", (userId&&*userId)?userId:"unknown", (peerStr.length()>0)?peerStr.str():"unknown");
    if (returnValue == SOAP_AUTHENTICATION_ERROR)
        msg.append(" User authentication failed");
    else
        msg.append(" User authentication required");
    DBGLOG("%s", msg.str());

    return returnValue;
}