1
0

MySQLAuthenticationData.cs 7.6 KB

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