1
0

MySQLAuthenticationData.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 System.Collections;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using MySql.Data.MySqlClient;
  34. namespace OpenSim.Data.MySQL
  35. {
  36. public class MySqlAuthenticationData : MySqlFramework, IAuthenticationData
  37. {
  38. private string m_Realm;
  39. private List<string> m_ColumnNames = null;
  40. private int m_LastExpire = 0;
  41. public MySqlAuthenticationData(string connectionString, string realm)
  42. : base(connectionString)
  43. {
  44. m_Realm = realm;
  45. Migration m = new Migration(m_Connection, GetType().Assembly, "AuthStore");
  46. m.Update();
  47. }
  48. public AuthenticationData Get(UUID principalID)
  49. {
  50. AuthenticationData ret = new AuthenticationData();
  51. ret.Data = new Dictionary<string, object>();
  52. MySqlCommand cmd = new MySqlCommand(
  53. "select * from `"+m_Realm+"` where UUID = ?principalID"
  54. );
  55. cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
  56. IDataReader result = ExecuteReader(cmd);
  57. if (result.Read())
  58. {
  59. ret.PrincipalID = principalID;
  60. if (m_ColumnNames == null)
  61. {
  62. m_ColumnNames = new List<string>();
  63. DataTable schemaTable = result.GetSchemaTable();
  64. foreach (DataRow row in schemaTable.Rows)
  65. m_ColumnNames.Add(row["ColumnName"].ToString());
  66. }
  67. foreach (string s in m_ColumnNames)
  68. {
  69. if (s == "UUID")
  70. continue;
  71. ret.Data[s] = result[s].ToString();
  72. }
  73. result.Close();
  74. CloseReaderCommand(cmd);
  75. return ret;
  76. }
  77. result.Close();
  78. CloseReaderCommand(cmd);
  79. return null;
  80. }
  81. public bool Store(AuthenticationData data)
  82. {
  83. if (data.Data.ContainsKey("UUID"))
  84. data.Data.Remove("UUID");
  85. string[] fields = new List<string>(data.Data.Keys).ToArray();
  86. MySqlCommand cmd = new MySqlCommand();
  87. string update = "update `"+m_Realm+"` set ";
  88. bool first = true;
  89. foreach (string field in fields)
  90. {
  91. if (!first)
  92. update += ", ";
  93. update += "`" + field + "` = ?"+field;
  94. first = false;
  95. cmd.Parameters.AddWithValue("?"+field, data.Data[field]);
  96. }
  97. update += " where UUID = ?principalID";
  98. cmd.CommandText = update;
  99. cmd.Parameters.AddWithValue("?principalID", data.PrincipalID.ToString());
  100. if (ExecuteNonQuery(cmd) < 1)
  101. {
  102. string insert = "insert into `" + m_Realm + "` (`UUID`, `" +
  103. String.Join("`, `", fields) +
  104. "`) values (?principalID, ?" + String.Join(", ?", fields) + ")";
  105. cmd.CommandText = insert;
  106. if (ExecuteNonQuery(cmd) < 1)
  107. {
  108. cmd.Dispose();
  109. return false;
  110. }
  111. }
  112. cmd.Dispose();
  113. return true;
  114. }
  115. public bool SetDataItem(UUID principalID, string item, string value)
  116. {
  117. MySqlCommand cmd = new MySqlCommand("update `" + m_Realm +
  118. "` set `" + item + "` = ?" + item + " where UUID = ?UUID");
  119. cmd.Parameters.AddWithValue("?"+item, value);
  120. cmd.Parameters.AddWithValue("?UUID", principalID.ToString());
  121. if (ExecuteNonQuery(cmd) > 0)
  122. return true;
  123. return false;
  124. }
  125. public bool SetToken(UUID principalID, string token, int lifetime)
  126. {
  127. if (System.Environment.TickCount - m_LastExpire > 30000)
  128. DoExpire();
  129. MySqlCommand cmd = new MySqlCommand("insert into tokens (UUID, token, validity) values (?principalID, ?token, date_add(now(), interval ?lifetime minute))");
  130. cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
  131. cmd.Parameters.AddWithValue("?token", token);
  132. cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
  133. if (ExecuteNonQuery(cmd) > 0)
  134. {
  135. cmd.Dispose();
  136. return true;
  137. }
  138. cmd.Dispose();
  139. return false;
  140. }
  141. public bool CheckToken(UUID principalID, string token, int lifetime)
  142. {
  143. if (System.Environment.TickCount - m_LastExpire > 30000)
  144. DoExpire();
  145. MySqlCommand cmd = new MySqlCommand("update tokens set validity = date_add(now(), interval ?lifetime minute) where UUID = ?principalID and token = ?token and validity > now()");
  146. cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
  147. cmd.Parameters.AddWithValue("?token", token);
  148. cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
  149. if (ExecuteNonQuery(cmd) > 0)
  150. {
  151. cmd.Dispose();
  152. return true;
  153. }
  154. cmd.Dispose();
  155. return false;
  156. }
  157. private void DoExpire()
  158. {
  159. MySqlCommand cmd = new MySqlCommand("delete from tokens where validity < now()");
  160. ExecuteNonQuery(cmd);
  161. cmd.Dispose();
  162. m_LastExpire = System.Environment.TickCount;
  163. }
  164. }
  165. }