1 /*------------------------------------------------------------------------------
  2 Name:      SecurityQos.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   The qos for the security (a subelement of connect qos)
  6 ------------------------------------------------------------------------------*/
  7 #include <authentication/SecurityQos.h>
  8 #include <string>
  9 #include <util/StringStripper.h>
 10 #include <util/Global.h>
 11 
 12 namespace org { namespace xmlBlaster { namespace authentication {
 13 
 14 using namespace std;
 15 using namespace org::xmlBlaster::util;
 16 
 17 SecurityQos::SecurityQos(Global& global,
 18                          const string& loginName,
 19                          const string& password,
 20                          const string& pluginTypeVersion)
 21    : ME("SecurityQos"), global_(global), log_(global.getLog("org.xmlBlaster.authentication"))
 22 {
 23 
 24    string tv = (pluginTypeVersion == "") ? "htpasswd,1.0" : pluginTypeVersion;
 25    string help = global_.getProperty().getStringProperty("Security.Client.DefaultPlugin", tv);
 26 
 27    StringStripper stripper(",");
 28    vector<std::string> help1 = stripper.strip(help);
 29    if (help1.size() == 2) {
 30       type_    = help1[0];
 31       version_ = help1[1];
 32    }
 33    else {
 34       type_    = "htpasswd";
 35       version_ = "1.0";
 36    }
 37 
 38    if (loginName != "") {
 39       user_ = loginName;
 40    }
 41    else {
 42       SessionName sessionName(global_);
 43       user_ = sessionName.getSubjectId();
 44    }
 45 
 46    passwd_ =  global_.getProperty().getStringProperty("passwd", "");
 47    if (password != "") passwd_ = password;
 48 
 49    if (log_.trace())  log_.trace(ME, string("constructor: type=" + type_ + " and version=" + version_ + " userId=") + user_);
 50 }
 51 
 52 SecurityQos::SecurityQos(const SecurityQos& securityQos)
 53    : ME("SecurityQos"), global_(securityQos.global_), log_(securityQos.log_)
 54 {
 55    copy(securityQos);
 56 }
 57 
 58 SecurityQos& SecurityQos::operator =(const SecurityQos& securityQos)
 59 {
 60    copy(securityQos);
 61    return *this;
 62 }
 63 
 64 string SecurityQos::getPluginVersion() const
 65 {
 66    return version_;
 67 }
 68 
 69 
 70 string SecurityQos::getPluginType() const
 71 {
 72    return type_;
 73 }
 74 
 75 
 76 void SecurityQos::setUserId(const string& userId)
 77 {
 78    user_ = userId;
 79 }
 80 
 81 
 82 string SecurityQos::getUserId() const
 83 {
 84    return user_;
 85 }
 86 
 87 
 88 /**
 89  * @param cred The password
 90  */
 91 void SecurityQos::setCredential(const string& cred)
 92 {
 93    passwd_ = cred;
 94 }
 95 
 96 
 97 /**
 98  * @return "" (empty string) (no password is delivered)
 99  */
100 string SecurityQos::getCredential() const
101 {
102    return "";
103 }
104 
105 /**
106  * Dump state of this object into a XML ASCII string.
107  * <br>
108  * @param extraOffset indenting of tags for nice output
109  * @return The xml representation
110  */
111 string SecurityQos::toXml(const string& extraOffset)
112 {
113    string ret;
114    string offset = Constants::OFFSET + extraOffset;
115    string offset2 = offset + Constants::INDENT;
116 
117    ret += offset + "<securityService type=\"";
118    ret += getPluginType() + "\" version=\"" + getPluginVersion()  + "\">";
119    ret += offset2 + "<![CDATA[";
120    ret += offset2 + "<user>" + user_ + "</user>";
121    ret += offset2 + "<passwd>" + passwd_ + "</passwd>";
122    ret += offset2 + "]]>";
123    ret += offset + "</securityService>";
124    return ret;
125 }
126 
127 }}} // namespaces
128 
129 
130 #ifdef _XMLBLASTER_CLASSTEST
131 
132 using namespace std;
133 using namespace org::xmlBlaster::authentication;
134 
135 int main(int args, char* argv[])
136 {
137     try {
138        Global& glob = Global::getInstance();
139        glob.initialize(args, argv);
140 
141        SecurityQos qos(glob);
142        cout << "the default: " << endl << qos.toXml() << endl;
143 
144        SecurityQos qos1(glob, "this_is_user", "this_is_passwd");
145        cout << "the default: " << endl << qos1.toXml() << endl;
146 
147     }
148 
149     catch(...)
150     {
151        cout << "Error during execution" << endl;
152        return 1;
153     }
154 
155    return 0;
156 }
157 
158 #endif


syntax highlighted by Code2HTML, v. 0.9.1