AuthenticationServiceBase.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. namespace OpenSim.Services.AuthenticationService
  35. {
  36. // Generic Authentication service used for identifying
  37. // and authenticating principals.
  38. // Principals may be clients acting on users' behalf,
  39. // or any other components that need
  40. // verifiable identification.
  41. //
  42. public class AuthenticationServiceBase : ServiceBase
  43. {
  44. // private static readonly ILog m_log =
  45. // LogManager.GetLogger(
  46. // MethodBase.GetCurrentMethod().DeclaringType);
  47. protected IAuthenticationData m_Database;
  48. public AuthenticationServiceBase(IConfigSource config) : base(config)
  49. {
  50. string dllName = String.Empty;
  51. string connString = String.Empty;
  52. string realm = "auth";
  53. //
  54. // Try reading the [AuthenticationService] section first, if it exists
  55. //
  56. IConfig authConfig = config.Configs["AuthenticationService"];
  57. if (authConfig != null)
  58. {
  59. dllName = authConfig.GetString("StorageProvider", dllName);
  60. connString = authConfig.GetString("ConnectionString", connString);
  61. realm = authConfig.GetString("Realm", realm);
  62. }
  63. //
  64. // Try reading the [DatabaseService] section, if it exists
  65. //
  66. IConfig dbConfig = config.Configs["DatabaseService"];
  67. if (dbConfig != null)
  68. {
  69. if (dllName == String.Empty)
  70. dllName = dbConfig.GetString("StorageProvider", String.Empty);
  71. if (connString == String.Empty)
  72. connString = dbConfig.GetString("ConnectionString", String.Empty);
  73. }
  74. //
  75. // We tried, but this doesn't exist. We can't proceed.
  76. //
  77. if (dllName == String.Empty || realm == String.Empty)
  78. throw new Exception("No StorageProvider configured");
  79. m_Database = LoadPlugin<IAuthenticationData>(dllName,
  80. new Object[] {connString, realm});
  81. if (m_Database == null)
  82. throw new Exception("Could not find a storage interface in the given module");
  83. }
  84. public bool Verify(UUID principalID, string token, int lifetime)
  85. {
  86. return m_Database.CheckToken(principalID, token, lifetime);
  87. }
  88. public virtual bool Release(UUID principalID, string token)
  89. {
  90. return m_Database.CheckToken(principalID, token, 0);
  91. }
  92. protected string GetToken(UUID principalID, int lifetime)
  93. {
  94. UUID token = UUID.Random();
  95. if (m_Database.SetToken(principalID, token.ToString(), lifetime))
  96. return token.ToString();
  97. return String.Empty;
  98. }
  99. }
  100. }