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