singleUserSecurity.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2018 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning( disable : 4786 )
  14. #include "basesecurity.hpp"
  15. #include "authmap.ipp"
  16. #include "singleUserSecurity.hpp"
  17. class CSingleUserSecurityManager : public CBaseSecurityManager
  18. {
  19. public:
  20. CSingleUserSecurityManager(const char * serviceName, IPropertyTree * secMgrCfg, IPropertyTree * bindConfig) : CBaseSecurityManager(serviceName, (IPropertyTree *)NULL)
  21. {
  22. if (secMgrCfg)
  23. {
  24. if (secMgrCfg->hasProp("@SingleUserName"))
  25. secMgrCfg->getProp("@SingleUserName", m_userName);
  26. else
  27. m_userName.set("admin");
  28. PROGLOG("SingleUserAuth: User set '%s'", m_userName.str());
  29. secMgrCfg->getProp("@SingleUserPass", m_userPass);
  30. if (m_userPass.isEmpty())
  31. throw MakeStringException(-1,"SingleUserAuth: Password not supplied and could not set up security manager!");
  32. }
  33. else
  34. throw MakeStringException(-1, "SingleUserAuth did not receive security manager configuration!");
  35. }
  36. ~CSingleUserSecurityManager() {}
  37. secManagerType querySecMgrType() override
  38. {
  39. return SMT_SingleUser;
  40. }
  41. inline virtual const char * querySecMgrTypeName() override { return "singleuser"; }
  42. IAuthMap * createAuthMap(IPropertyTree * authconfig) override
  43. {
  44. CAuthMap * authmap = new CAuthMap(this);
  45. Owned<IPropertyTreeIterator> loc_iter = authconfig->getElements(".//Location");
  46. ForEach(*loc_iter)
  47. {
  48. IPropertyTree & location = loc_iter->query();
  49. StringBuffer pathstr, rstr, required, description;
  50. location.getProp("@path", pathstr);
  51. location.getProp("@resource", rstr);
  52. location.getProp("@required", required);
  53. location.getProp("@description", description);
  54. if(pathstr.length() == 0)
  55. throw MakeStringException(-1, "path empty in Authenticate/Location");
  56. if(rstr.length() == 0)
  57. throw MakeStringException(-1, "resource empty in Authenticate/Location");
  58. ISecResourceList * rlist = authmap->queryResourceList(pathstr.str());
  59. if(rlist == NULL)
  60. {
  61. rlist = createResourceList("singleusersecurity");
  62. authmap->add(pathstr.str(), rlist);
  63. }
  64. ISecResource * rs = rlist->addResource(rstr.str());
  65. SecAccessFlags requiredaccess = str2perm(required.str());
  66. rs->setRequiredAccessFlags(requiredaccess);
  67. rs->setDescription(description.str());
  68. rs->setAccessFlags(SecAccess_Full);//grant full access to authenticated users
  69. }
  70. return authmap;
  71. }
  72. IAuthMap * createFeatureMap(IPropertyTree * authconfig) override
  73. {
  74. CAuthMap * feature_authmap = new CAuthMap(this);
  75. Owned<IPropertyTreeIterator> feature_iter = authconfig->getElements(".//Feature");
  76. ForEach(*feature_iter)
  77. {
  78. IPropertyTree * feature = &feature_iter->query();
  79. if (feature)
  80. {
  81. StringBuffer pathstr, rstr, required, description;
  82. feature->getProp("@path", pathstr);
  83. feature->getProp("@resource", rstr);
  84. feature->getProp("@required", required);
  85. feature->getProp("@description", description);
  86. ISecResourceList * rlist = feature_authmap->queryResourceList(pathstr.str());
  87. if(rlist == NULL)
  88. {
  89. rlist = createResourceList(pathstr.str());
  90. feature_authmap->add(pathstr.str(), rlist);
  91. }
  92. if (!rstr.isEmpty())
  93. {
  94. ISecResource * rs = rlist->addResource(rstr.str());
  95. SecAccessFlags requiredaccess = str2perm(required.str());
  96. rs->setRequiredAccessFlags(requiredaccess);
  97. rs->setDescription(description.str());
  98. rs->setAccessFlags(SecAccess_Full);//grant full access to authenticated users
  99. }
  100. }
  101. }
  102. return feature_authmap;
  103. }
  104. bool logoutUser(ISecUser & user) override { return true; }
  105. protected:
  106. //ISecManager
  107. bool IsPasswordValid(ISecUser& sec_user)
  108. {
  109. StringBuffer username;
  110. username.set(sec_user.getName());
  111. if (0 == username.length())
  112. throw MakeStringException(-1, "SingleUserAuth name is empty");
  113. if (sec_user.credentials().getSessionToken() != 0 || !isEmptyString(sec_user.credentials().getSignature()))//Already authenticated it token or signature exist
  114. return true;
  115. if (strcmp(username.str(), m_userName.str())!=0)
  116. {
  117. WARNLOG("SingleUserAuth: Invalid credentials provided!");
  118. return false;
  119. }
  120. if (m_userPass.isEmpty())
  121. throw MakeStringException(-1, "SingleUserAuth password was not set!");
  122. const char * userpass = sec_user.credentials().getPassword();
  123. if (!userpass || !*userpass)
  124. throw MakeStringException(-1, "SingleUserAuth encountered empty password!");
  125. StringBuffer encpass;
  126. encrypt(encpass, userpass);
  127. if (strcmp(m_userPass.str(), encpass.str())!=0)
  128. {
  129. WARNLOG("SingleUserAuth: Invalid credentials provided!");
  130. return false;
  131. }
  132. return true;
  133. }
  134. const char * getDescription() override
  135. {
  136. return "SingleUser Security Manager";
  137. }
  138. bool authorize(ISecUser & user, ISecResourceList * resources, IEspSecureContext * secureContext) override
  139. {
  140. return IsPasswordValid(user);
  141. }
  142. unsigned getPasswordExpirationWarningDays() override
  143. {
  144. return -2;//never expires
  145. }
  146. SecAccessFlags authorizeEx(SecResourceType rtype, ISecUser & user, const char * resourcename, IEspSecureContext * secureContext) override
  147. {
  148. return SecAccess_Full;//grant full access to authenticated users
  149. }
  150. SecAccessFlags getAccessFlagsEx(SecResourceType rtype, ISecUser& sec_user, const char * resourcename) override
  151. {
  152. return SecAccess_Full;//grant full access to authenticated users
  153. }
  154. SecAccessFlags authorizeFileScope(ISecUser & user, const char * filescope) override
  155. {
  156. return SecAccess_Full;//grant full access to authenticated users
  157. }
  158. SecAccessFlags authorizeWorkunitScope(ISecUser & user, const char * filescope) override
  159. {
  160. return SecAccess_Full;//grant full access to authenticated users
  161. }
  162. private:
  163. private:
  164. StringBuffer m_userPass;
  165. StringBuffer m_userName;
  166. };
  167. extern "C"
  168. {
  169. SINGLEUSERSECURITY_API ISecManager * createInstance(const char * serviceName, IPropertyTree &secMgrCfg, IPropertyTree &bndCfg)
  170. {
  171. return new CSingleUserSecurityManager(serviceName, &secMgrCfg, &bndCfg);
  172. }
  173. }