AuthenticationServiceBase.cs 5.6 KB

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