AuthenticationServiceBase.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 System;
  28. using OpenMetaverse;
  29. using log4net;
  30. using Nini.Config;
  31. using System.Reflection;
  32. using OpenSim.Data;
  33. using OpenSim.Framework;
  34. using OpenSim.Services.Base;
  35. using OpenSim.Services.Interfaces;
  36. namespace OpenSim.Services.AuthenticationService
  37. {
  38. // Generic Authentication service used for identifying
  39. // and authenticating principals.
  40. // Principals may be clients acting on users' behalf,
  41. // or any other components that need
  42. // verifiable identification.
  43. //
  44. public class AuthenticationServiceBase : ServiceBase
  45. {
  46. private static readonly ILog m_log =
  47. LogManager.GetLogger(
  48. MethodBase.GetCurrentMethod().DeclaringType);
  49. protected IAuthenticationData m_Database;
  50. public AuthenticationServiceBase(IConfigSource config) : base(config)
  51. {
  52. string dllName = String.Empty;
  53. string connString = String.Empty;
  54. string realm = "auth";
  55. //
  56. // Try reading the [AuthenticationService] section first, if it exists
  57. //
  58. IConfig authConfig = config.Configs["AuthenticationService"];
  59. if (authConfig != null)
  60. {
  61. dllName = authConfig.GetString("StorageProvider", dllName);
  62. connString = authConfig.GetString("ConnectionString", connString);
  63. realm = authConfig.GetString("Realm", realm);
  64. }
  65. //
  66. // Try reading the [DatabaseService] section, if it exists
  67. //
  68. IConfig dbConfig = config.Configs["DatabaseService"];
  69. if (dbConfig != null)
  70. {
  71. if (dllName == String.Empty)
  72. dllName = dbConfig.GetString("StorageProvider", String.Empty);
  73. if (connString == String.Empty)
  74. connString = dbConfig.GetString("ConnectionString", String.Empty);
  75. }
  76. //
  77. // We tried, but this doesn't exist. We can't proceed.
  78. //
  79. if (dllName == String.Empty || realm == String.Empty)
  80. throw new Exception("No StorageProvider configured");
  81. m_Database = LoadPlugin<IAuthenticationData>(dllName,
  82. new Object[] {connString, realm});
  83. if (m_Database == null)
  84. throw new Exception(string.Format("Could not find a storage interface in module {0}", dllName));
  85. }
  86. public bool Verify(UUID principalID, string token, int lifetime)
  87. {
  88. return m_Database.CheckToken(principalID, token, lifetime);
  89. }
  90. public virtual bool Release(UUID principalID, string token)
  91. {
  92. return m_Database.CheckToken(principalID, token, 0);
  93. }
  94. public virtual bool SetPassword(UUID principalID, string password)
  95. {
  96. string passwordSalt = Util.Md5Hash(UUID.Random().ToString());
  97. string md5PasswdHash = Util.Md5Hash(Util.Md5Hash(password) + ":" + passwordSalt);
  98. AuthenticationData auth = m_Database.Get(principalID);
  99. if (auth == null)
  100. {
  101. auth = new AuthenticationData();
  102. auth.PrincipalID = principalID;
  103. auth.Data = new System.Collections.Generic.Dictionary<string, object>();
  104. auth.Data["accountType"] = "UserAccount";
  105. auth.Data["webLoginKey"] = UUID.Zero.ToString();
  106. }
  107. auth.Data["passwordHash"] = md5PasswdHash;
  108. auth.Data["passwordSalt"] = passwordSalt;
  109. if (!m_Database.Store(auth))
  110. {
  111. m_log.DebugFormat("[AUTHENTICATION DB]: Failed to store authentication data");
  112. return false;
  113. }
  114. m_log.InfoFormat("[AUTHENTICATION DB]: Set password for principalID {0}", principalID);
  115. return true;
  116. }
  117. public virtual AuthInfo GetAuthInfo(UUID principalID)
  118. {
  119. AuthenticationData data = m_Database.Get(principalID);
  120. if (data == null)
  121. {
  122. return null;
  123. }
  124. else
  125. {
  126. AuthInfo info
  127. = new AuthInfo()
  128. {
  129. PrincipalID = data.PrincipalID,
  130. AccountType = data.Data["accountType"] as string,
  131. PasswordHash = data.Data["passwordHash"] as string,
  132. PasswordSalt = data.Data["passwordSalt"] as string,
  133. WebLoginKey = data.Data["webLoginKey"] as string
  134. };
  135. return info;
  136. }
  137. }
  138. public virtual bool SetAuthInfo(AuthInfo info)
  139. {
  140. AuthenticationData auth = new AuthenticationData();
  141. auth.PrincipalID = info.PrincipalID;
  142. auth.Data = new System.Collections.Generic.Dictionary<string, object>();
  143. auth.Data["accountType"] = info.AccountType;
  144. auth.Data["webLoginKey"] = info.WebLoginKey;
  145. auth.Data["passwordHash"] = info.PasswordHash;
  146. auth.Data["passwordSalt"] = info.PasswordSalt;
  147. if (!m_Database.Store(auth))
  148. {
  149. m_log.ErrorFormat("[AUTHENTICATION DB]: Failed to store authentication info.");
  150. return false;
  151. }
  152. m_log.DebugFormat("[AUTHENTICATION DB]: Set authentication info for principalID {0}", info.PrincipalID);
  153. return true;
  154. }
  155. protected string GetToken(UUID principalID, int lifetime)
  156. {
  157. UUID token = UUID.Random();
  158. if (m_Database.SetToken(principalID, token.ToString(), lifetime))
  159. return token.ToString();
  160. return String.Empty;
  161. }
  162. }
  163. }