123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Data;
- using OpenMetaverse;
- using OpenSim.Framework;
- using MySql.Data.MySqlClient;
- namespace OpenSim.Data.MySQL
- {
- public class MySqlAuthenticationData : MySqlFramework, IAuthenticationData
- {
- private string m_Realm;
- private List<string> m_ColumnNames;
- private int m_LastExpire;
-
- protected virtual Assembly Assembly
- {
- get { return GetType().Assembly; }
- }
- public MySqlAuthenticationData(string connectionString, string realm)
- : base(connectionString)
- {
- m_Realm = realm;
- m_connectionString = connectionString;
- using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
- {
- dbcon.Open();
- Migration m = new Migration(dbcon, Assembly, "AuthStore");
- m.Update();
- }
- }
- public AuthenticationData Get(UUID principalID)
- {
- AuthenticationData ret = new AuthenticationData();
- ret.Data = new Dictionary<string, object>();
- using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
- {
- dbcon.Open();
- using (MySqlCommand cmd
- = new MySqlCommand("select * from `" + m_Realm + "` where UUID = ?principalID", dbcon))
- {
- cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
- IDataReader result = cmd.ExecuteReader();
-
- if (result.Read())
- {
- ret.PrincipalID = principalID;
-
- CheckColumnNames(result);
-
- foreach (string s in m_ColumnNames)
- {
- if (s == "UUID")
- continue;
-
- ret.Data[s] = result[s].ToString();
- }
-
- return ret;
- }
- else
- {
- return null;
- }
- }
- }
- }
- private void CheckColumnNames(IDataReader result)
- {
- if (m_ColumnNames != null)
- return;
- List<string> columnNames = new List<string>();
- DataTable schemaTable = result.GetSchemaTable();
- foreach (DataRow row in schemaTable.Rows)
- columnNames.Add(row["ColumnName"].ToString());
- m_ColumnNames = columnNames;
- }
- public bool Store(AuthenticationData data)
- {
- if (data.Data.ContainsKey("UUID"))
- data.Data.Remove("UUID");
- string[] fields = new List<string>(data.Data.Keys).ToArray();
- using (MySqlCommand cmd = new MySqlCommand())
- {
- string update = "update `"+m_Realm+"` set ";
- bool first = true;
- foreach (string field in fields)
- {
- if (!first)
- update += ", ";
- update += "`" + field + "` = ?"+field;
-
- first = false;
-
- cmd.Parameters.AddWithValue("?"+field, data.Data[field]);
- }
-
- update += " where UUID = ?principalID";
-
- cmd.CommandText = update;
- cmd.Parameters.AddWithValue("?principalID", data.PrincipalID.ToString());
-
- if (ExecuteNonQuery(cmd) < 1)
- {
- string insert = "insert into `" + m_Realm + "` (`UUID`, `" +
- String.Join("`, `", fields) +
- "`) values (?principalID, ?" + String.Join(", ?", fields) + ")";
-
- cmd.CommandText = insert;
-
- if (ExecuteNonQuery(cmd) < 1)
- return false;
- }
- }
- return true;
- }
- public bool SetDataItem(UUID principalID, string item, string value)
- {
- using (MySqlCommand cmd
- = new MySqlCommand("update `" + m_Realm + "` set `" + item + "` = ?" + item + " where UUID = ?UUID"))
- {
- cmd.Parameters.AddWithValue("?"+item, value);
- cmd.Parameters.AddWithValue("?UUID", principalID.ToString());
-
- if (ExecuteNonQuery(cmd) > 0)
- return true;
- }
- return false;
- }
- public bool SetToken(UUID principalID, string token, int lifetime)
- {
- if (System.Environment.TickCount - m_LastExpire > 30000)
- DoExpire();
- using (MySqlCommand cmd
- = new MySqlCommand(
- "insert into tokens (UUID, token, validity) values (?principalID, ?token, date_add(now(), interval ?lifetime minute))"))
- {
- cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
- cmd.Parameters.AddWithValue("?token", token);
- cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
-
- if (ExecuteNonQuery(cmd) > 0)
- return true;
- }
- return false;
- }
- public bool CheckToken(UUID principalID, string token, int lifetime)
- {
- if (System.Environment.TickCount - m_LastExpire > 30000)
- DoExpire();
- using (MySqlCommand cmd
- = new MySqlCommand(
- "update tokens set validity = date_add(now(), interval ?lifetime minute) where UUID = ?principalID and token = ?token and validity > now()"))
- {
- cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
- cmd.Parameters.AddWithValue("?token", token);
- cmd.Parameters.AddWithValue("?lifetime", lifetime.ToString());
- if (ExecuteNonQuery(cmd) > 0)
- return true;
- }
- return false;
- }
- private void DoExpire()
- {
- using (MySqlCommand cmd = new MySqlCommand("delete from tokens where validity < now()"))
- {
- ExecuteNonQuery(cmd);
- }
- m_LastExpire = System.Environment.TickCount;
- }
- }
- }
|