authentication/SecurityQos.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      SecurityQos.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   The qos for the security (a subelement of connect qos)
00006 ------------------------------------------------------------------------------*/
00007 #include <authentication/SecurityQos.h>
00008 #include <string>
00009 #include <util/StringStripper.h>
00010 #include <util/Global.h>
00011 
00012 namespace org { namespace xmlBlaster { namespace authentication {
00013 
00014 using namespace std;
00015 using namespace org::xmlBlaster::util;
00016 
00017 SecurityQos::SecurityQos(Global& global,
00018                          const string& loginName,
00019                          const string& password,
00020                          const string& pluginTypeVersion)
00021    : ME("SecurityQos"), global_(global), log_(global.getLog("org.xmlBlaster.authentication"))
00022 {
00023 
00024    string tv = (pluginTypeVersion == "") ? "htpasswd,1.0" : pluginTypeVersion;
00025    string help = global_.getProperty().getStringProperty("Security.Client.DefaultPlugin", tv);
00026 
00027    StringStripper stripper(",");
00028    vector<std::string> help1 = stripper.strip(help);
00029    if (help1.size() == 2) {
00030       type_    = help1[0];
00031       version_ = help1[1];
00032    }
00033    else {
00034       type_    = "htpasswd";
00035       version_ = "1.0";
00036    }
00037 
00038    if (loginName != "") {
00039       user_ = loginName;
00040    }
00041    else {
00042       SessionName sessionName(global_);
00043       user_ = sessionName.getSubjectId();
00044    }
00045 
00046    passwd_ =  global_.getProperty().getStringProperty("passwd", "");
00047    if (password != "") passwd_ = password;
00048 
00049    if (log_.trace())  log_.trace(ME, string("constructor: type=" + type_ + " and version=" + version_ + " userId=") + user_);
00050 }
00051 
00052 SecurityQos::SecurityQos(const SecurityQos& securityQos)
00053    : ME("SecurityQos"), global_(securityQos.global_), log_(securityQos.log_)
00054 {
00055    copy(securityQos);
00056 }
00057 
00058 SecurityQos& SecurityQos::operator =(const SecurityQos& securityQos)
00059 {
00060    copy(securityQos);
00061    return *this;
00062 }
00063 
00064 string SecurityQos::getPluginVersion() const
00065 {
00066    return version_;
00067 }
00068 
00069 
00070 string SecurityQos::getPluginType() const
00071 {
00072    return type_;
00073 }
00074 
00075 
00076 void SecurityQos::setUserId(const string& userId)
00077 {
00078    user_ = userId;
00079 }
00080 
00081 
00082 string SecurityQos::getUserId() const
00083 {
00084    return user_;
00085 }
00086 
00087 
00091 void SecurityQos::setCredential(const string& cred)
00092 {
00093    passwd_ = cred;
00094 }
00095 
00096 
00100 string SecurityQos::getCredential() const
00101 {
00102    return "";
00103 }
00104 
00111 string SecurityQos::toXml(const string& extraOffset)
00112 {
00113    string ret;
00114    string offset = Constants::OFFSET + extraOffset;
00115    string offset2 = offset + Constants::INDENT;
00116 
00117    ret += offset + "<securityService type=\"";
00118    ret += getPluginType() + "\" version=\"" + getPluginVersion()  + "\">";
00119    ret += offset2 + "<![CDATA[";
00120    ret += offset2 + "<user>" + user_ + "</user>";
00121    ret += offset2 + "<passwd>" + passwd_ + "</passwd>";
00122    ret += offset2 + "]]>";
00123    ret += offset + "</securityService>";
00124    return ret;
00125 }
00126 
00127 }}} // namespaces
00128 
00129 
00130 #ifdef _XMLBLASTER_CLASSTEST
00131 
00132 using namespace std;
00133 using namespace org::xmlBlaster::authentication;
00134 
00135 int main(int args, char* argv[])
00136 {
00137     try {
00138        Global& glob = Global::getInstance();
00139        glob.initialize(args, argv);
00140 
00141        SecurityQos qos(glob);
00142        cout << "the default: " << endl << qos.toXml() << endl;
00143 
00144        SecurityQos qos1(glob, "this_is_user", "this_is_passwd");
00145        cout << "the default: " << endl << qos1.toXml() << endl;
00146 
00147     }
00148 
00149     catch(...)
00150     {
00151        cout << "Error during execution" << endl;
00152        return 1;
00153     }
00154 
00155    return 0;
00156 }
00157 
00158 #endif