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