1
0

UserAccountServiceConnector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Servers.HttpServer;
  36. using OpenSim.Server.Base;
  37. using OpenSim.Services.Interfaces;
  38. using OpenMetaverse;
  39. namespace OpenSim.Services.Connectors
  40. {
  41. public class UserAccountServicesConnector : IUserAccountService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private string m_ServerURI = String.Empty;
  47. public UserAccountServicesConnector()
  48. {
  49. }
  50. public UserAccountServicesConnector(string serverURI)
  51. {
  52. m_ServerURI = serverURI.TrimEnd('/');
  53. }
  54. public UserAccountServicesConnector(IConfigSource source)
  55. {
  56. Initialise(source);
  57. }
  58. public virtual void Initialise(IConfigSource source)
  59. {
  60. IConfig assetConfig = source.Configs["UserAccountService"];
  61. if (assetConfig == null)
  62. {
  63. m_log.Error("[ACCOUNT CONNECTOR]: UserAccountService missing from OpenSim.ini");
  64. throw new Exception("User account connector init error");
  65. }
  66. string serviceURI = assetConfig.GetString("UserAccountServerURI",
  67. String.Empty);
  68. if (serviceURI == String.Empty)
  69. {
  70. m_log.Error("[ACCOUNT CONNECTOR]: No Server URI named in section UserAccountService");
  71. throw new Exception("User account connector init error");
  72. }
  73. m_ServerURI = serviceURI;
  74. }
  75. public virtual UserAccount GetUserAccount(UUID scopeID, string firstName, string lastName)
  76. {
  77. Dictionary<string, object> sendData = new Dictionary<string, object>();
  78. //sendData["SCOPEID"] = scopeID.ToString();
  79. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  80. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  81. sendData["METHOD"] = "getaccount";
  82. sendData["ScopeID"] = scopeID;
  83. sendData["FirstName"] = firstName.ToString();
  84. sendData["LastName"] = lastName.ToString();
  85. return SendAndGetReply(sendData);
  86. }
  87. public virtual UserAccount GetUserAccount(UUID scopeID, string email)
  88. {
  89. Dictionary<string, object> sendData = new Dictionary<string, object>();
  90. //sendData["SCOPEID"] = scopeID.ToString();
  91. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  92. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  93. sendData["METHOD"] = "getaccount";
  94. sendData["ScopeID"] = scopeID;
  95. sendData["Email"] = email;
  96. return SendAndGetReply(sendData);
  97. }
  98. public virtual UserAccount GetUserAccount(UUID scopeID, UUID userID)
  99. {
  100. //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUserAccount {0}", userID);
  101. Dictionary<string, object> sendData = new Dictionary<string, object>();
  102. //sendData["SCOPEID"] = scopeID.ToString();
  103. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  104. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  105. sendData["METHOD"] = "getaccount";
  106. sendData["ScopeID"] = scopeID;
  107. sendData["UserID"] = userID.ToString();
  108. return SendAndGetReply(sendData);
  109. }
  110. public List<UserAccount> GetUserAccounts(UUID scopeID, string query)
  111. {
  112. Dictionary<string, object> sendData = new Dictionary<string, object>();
  113. //sendData["SCOPEID"] = scopeID.ToString();
  114. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  115. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  116. sendData["METHOD"] = "getaccounts";
  117. sendData["ScopeID"] = scopeID.ToString();
  118. sendData["query"] = query;
  119. string reply = string.Empty;
  120. string reqString = ServerUtils.BuildQueryString(sendData);
  121. // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  122. try
  123. {
  124. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  125. m_ServerURI + "/accounts",
  126. reqString);
  127. if (reply == null || (reply != null && reply == string.Empty))
  128. {
  129. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received null or empty reply");
  130. return null;
  131. }
  132. }
  133. catch (Exception e)
  134. {
  135. m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting accounts server: {0}", e.Message);
  136. }
  137. List<UserAccount> accounts = new List<UserAccount>();
  138. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  139. if (replyData != null)
  140. {
  141. if (replyData.ContainsKey("result") && replyData.ContainsKey("result").ToString() == "null")
  142. {
  143. return accounts;
  144. }
  145. Dictionary<string, object>.ValueCollection accountList = replyData.Values;
  146. //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count);
  147. foreach (object acc in accountList)
  148. {
  149. if (acc is Dictionary<string, object>)
  150. {
  151. UserAccount pinfo = new UserAccount((Dictionary<string, object>)acc);
  152. accounts.Add(pinfo);
  153. }
  154. else
  155. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received invalid response type {0}",
  156. acc.GetType());
  157. }
  158. }
  159. else
  160. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUserAccounts received null response");
  161. return accounts;
  162. }
  163. public virtual UserAccount CreateUserAccount(UserAccount data, string password)
  164. {
  165. return null;
  166. }
  167. public virtual bool StoreUserAccount(UserAccount data)
  168. {
  169. Dictionary<string, object> sendData = new Dictionary<string, object>();
  170. //sendData["SCOPEID"] = scopeID.ToString();
  171. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  172. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  173. sendData["METHOD"] = "setaccount";
  174. Dictionary<string, object> structData = data.ToKeyValuePairs();
  175. foreach (KeyValuePair<string,object> kvp in structData)
  176. sendData[kvp.Key] = kvp.Value.ToString();
  177. return SendAndGetBoolReply(sendData);
  178. }
  179. private UserAccount SendAndGetReply(Dictionary<string, object> sendData)
  180. {
  181. string reply = string.Empty;
  182. string reqString = ServerUtils.BuildQueryString(sendData);
  183. // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  184. try
  185. {
  186. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  187. m_ServerURI + "/accounts",
  188. reqString);
  189. if (reply == null || (reply != null && reply == string.Empty))
  190. {
  191. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccount received null or empty reply");
  192. return null;
  193. }
  194. }
  195. catch (Exception e)
  196. {
  197. m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user account server: {0}", e.Message);
  198. }
  199. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  200. UserAccount account = null;
  201. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  202. {
  203. if (replyData["result"] is Dictionary<string, object>)
  204. {
  205. account = new UserAccount((Dictionary<string, object>)replyData["result"]);
  206. }
  207. }
  208. return account;
  209. }
  210. private bool SendAndGetBoolReply(Dictionary<string, object> sendData)
  211. {
  212. string reqString = ServerUtils.BuildQueryString(sendData);
  213. // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  214. try
  215. {
  216. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  217. m_ServerURI + "/accounts",
  218. reqString);
  219. if (reply != string.Empty)
  220. {
  221. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  222. if (replyData.ContainsKey("result"))
  223. {
  224. if (replyData["result"].ToString().ToLower() == "success")
  225. return true;
  226. else
  227. return false;
  228. }
  229. else
  230. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount reply data does not contain result field");
  231. }
  232. else
  233. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount received empty reply");
  234. }
  235. catch (Exception e)
  236. {
  237. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Exception when contacting user account server: {0}", e.Message);
  238. }
  239. return false;
  240. }
  241. }
  242. }