diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLAuthenticationData.cs | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/OpenSim/Data/MySQL/MySQLAuthenticationData.cs b/OpenSim/Data/MySQL/MySQLAuthenticationData.cs new file mode 100644 index 0000000..7627497 --- /dev/null +++ b/OpenSim/Data/MySQL/MySQLAuthenticationData.cs | |||
@@ -0,0 +1,227 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Reflection; | ||
32 | using System.Data; | ||
33 | using OpenMetaverse; | ||
34 | using OpenSim.Framework; | ||
35 | using MySql.Data.MySqlClient; | ||
36 | |||
37 | namespace OpenSim.Data.MySQL | ||
38 | { | ||
39 | public class MySqlAuthenticationData : MySqlFramework, IAuthenticationData | ||
40 | { | ||
41 | private string m_Realm; | ||
42 | private List<string> m_ColumnNames; | ||
43 | private int m_LastExpire; | ||
44 | // private string m_connectionString; | ||
45 | |||
46 | protected virtual Assembly Assembly | ||
47 | { | ||
48 | get { return GetType().Assembly; } | ||
49 | } | ||
50 | |||
51 | public MySqlAuthenticationData(string connectionString, string realm) | ||
52 | : base(connectionString) | ||
53 | { | ||
54 | m_Realm = realm; | ||
55 | m_connectionString = connectionString; | ||
56 | |||
57 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
58 | { | ||
59 | dbcon.Open(); | ||
60 | Migration m = new Migration(dbcon, Assembly, "AuthStore"); | ||
61 | m.Update(); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | public AuthenticationData Get(UUID principalID) | ||
66 | { | ||
67 | AuthenticationData ret = new AuthenticationData(); | ||
68 | ret.Data = new Dictionary<string, object>(); | ||
69 | |||
70 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
71 | { | ||
72 | dbcon.Open(); | ||
73 | |||
74 | using (MySqlCommand cmd | ||
75 | = new MySqlCommand("select * from `" + m_Realm + "` where UUID = ?principalID", dbcon)) | ||
76 | { | ||
77 | cmd.Parameters.AddWithValue("?principalID", principalID.ToString()); | ||
78 | |||
79 | IDataReader result = cmd.ExecuteReader(); | ||
80 | |||
81 | if (result.Read()) | ||
82 | { | ||
83 | ret.PrincipalID = principalID; | ||
84 | |||
85 | CheckColumnNames(result); | ||
86 | |||
87 | foreach (string s in m_ColumnNames) | ||
88 | { | ||
89 | if (s == "UUID") | ||
90 | continue; | ||
91 | |||
92 | ret.Data[s] = result[s].ToString(); | ||
93 | } | ||
94 | |||
95 | return ret; | ||
96 | } | ||
97 | else | ||
98 | { | ||
99 | return null; | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | |||
105 | private void CheckColumnNames(IDataReader result) | ||
106 | { | ||
107 | if (m_ColumnNames != null) | ||
108 | return; | ||
109 | |||
110 | List<string> columnNames = new List<string>(); | ||
111 | |||
112 | DataTable schemaTable = result.GetSchemaTable(); | ||
113 | foreach (DataRow row in schemaTable.Rows) | ||
114 | columnNames.Add(row["ColumnName"].ToString()); | ||
115 | |||
116 | m_ColumnNames = columnNames; | ||
117 | } | ||
118 | |||
119 | public bool Store(AuthenticationData data) | ||
120 | { | ||
121 | if (data.Data.ContainsKey("UUID")) | ||
122 | data.Data.Remove("UUID"); | ||
123 | |||
124 | string[] fields = new List<string>(data.Data.Keys).ToArray(); | ||
125 | |||
126 | using (MySqlCommand cmd = new MySqlCommand()) | ||
127 | { | ||
128 | string update = "update `"+m_Realm+"` set "; | ||
129 | bool first = true; | ||
130 | foreach (string field in fields) | ||
131 | { | ||
132 | if (!first) | ||
133 | update += ", "; | ||
134 | update += "`" + field + "` = ?"+field; | ||
135 | |||
136 | first = false; | ||
137 | |||
138 | cmd.Parameters.AddWithValue("?"+field, data.Data[field]); | ||
139 | } | ||
140 | |||
141 | update += " where UUID = ?principalID"; | ||
142 | |||
143 | cmd.CommandText = update; | ||
144 | cmd.Parameters.AddWithValue("?principalID", data.PrincipalID.ToString()); | ||
145 | |||
146 | if (ExecuteNonQuery(cmd) < 1) | ||
147 | { | ||
148 | string insert = "insert into `" + m_Realm + "` (`UUID`, `" + | ||
149 | String.Join("`, `", fields) + | ||
150 | "`) values (?principalID, ?" + String.Join(", ?", fields) + ")"; | ||
151 | |||
152 | cmd.CommandText = insert; | ||
153 | |||
154 | if (ExecuteNonQuery(cmd) < 1) | ||
155 | return false; | ||
156 | } | ||
157 | } | ||
158 | |||
159 | return true; | ||
160 | } | ||
161 | |||
162 | public bool SetDataItem(UUID principalID, string item, string value) | ||
163 | { | ||
164 | using (MySqlCommand cmd | ||
165 | = new MySqlCommand("update `" + m_Realm + "` set `" + item + "` = ?" + item + " where UUID = ?UUID")) | ||
166 | { | ||
167 | cmd.Parameters.AddWithValue("?"+item, value); | ||
168 | cmd.Parameters.AddWithValue("?UUID", principalID.ToString()); | ||
169 | |||
170 | if (ExecuteNonQuery(cmd) > 0) | ||
171 | return true; | ||
172 | } | ||
173 | |||
174 | return false; | ||
175 | } | ||
176 | |||
177 | public bool SetToken(UUID principalID, string token, int lifetime) | ||
178 | { | ||
179 | if (System.Environment.TickCount - m_LastExpire > 30000) | ||
180 | DoExpire(); | ||
181 | |||
182 | using (MySqlCommand cmd | ||
183 | = new MySqlCommand( | ||
184 | "insert into tokens (UUID, token, validity) values (?principalID, ?token, date_add(now(), interval ?lifetime minute))")) | ||
185 | { | ||
186 | cmd.Parameters.AddWithValue("?principalID", principalID.ToString()); | ||
187 | cmd.Parameters.AddWithValue("?token", token); | ||
188 | cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString()); | ||
189 | |||
190 | if (ExecuteNonQuery(cmd) > 0) | ||
191 | return true; | ||
192 | } | ||
193 | |||
194 | return false; | ||
195 | } | ||
196 | |||
197 | public bool CheckToken(UUID principalID, string token, int lifetime) | ||
198 | { | ||
199 | if (System.Environment.TickCount - m_LastExpire > 30000) | ||
200 | DoExpire(); | ||
201 | |||
202 | using (MySqlCommand cmd | ||
203 | = new MySqlCommand( | ||
204 | "update tokens set validity = date_add(now(), interval ?lifetime minute) where UUID = ?principalID and token = ?token and validity > now()")) | ||
205 | { | ||
206 | cmd.Parameters.AddWithValue("?principalID", principalID.ToString()); | ||
207 | cmd.Parameters.AddWithValue("?token", token); | ||
208 | cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString()); | ||
209 | |||
210 | if (ExecuteNonQuery(cmd) > 0) | ||
211 | return true; | ||
212 | } | ||
213 | |||
214 | return false; | ||
215 | } | ||
216 | |||
217 | private void DoExpire() | ||
218 | { | ||
219 | using (MySqlCommand cmd = new MySqlCommand("delete from tokens where validity < now()")) | ||
220 | { | ||
221 | ExecuteNonQuery(cmd); | ||
222 | } | ||
223 | |||
224 | m_LastExpire = System.Environment.TickCount; | ||
225 | } | ||
226 | } | ||
227 | } \ No newline at end of file | ||