diff --git a/OpenSim/Data/IAuthenticationData.cs b/OpenSim/Data/IAuthenticationData.cs index f84871690c..7753e04a8c 100644 --- a/OpenSim/Data/IAuthenticationData.cs +++ b/OpenSim/Data/IAuthenticationData.cs @@ -48,5 +48,9 @@ namespace OpenSim.Data bool Store(AuthenticationData data); bool SetDataItem(UUID principalID, string item, string value); + + bool SetToken(UUID principalID, string token, int lifetime); + + bool CheckToken(UUID principalID, string token, int lifetime); } } diff --git a/OpenSim/Data/MySQL/MySQLAuthenticationData.cs b/OpenSim/Data/MySQL/MySQLAuthenticationData.cs index 19575ec8e0..1ee64cec0e 100644 --- a/OpenSim/Data/MySQL/MySQLAuthenticationData.cs +++ b/OpenSim/Data/MySQL/MySQLAuthenticationData.cs @@ -39,6 +39,7 @@ namespace OpenSim.Data.MySQL { private string m_Realm; private List m_ColumnNames = null; + private int m_LastExpire = 0; public MySqlAuthenticationData(string connectionString, string realm) : base(connectionString) @@ -153,5 +154,56 @@ namespace OpenSim.Data.MySQL return false; } + + public bool SetToken(UUID principalID, string token, int lifetime) + { + if (System.Environment.TickCount - m_LastExpire > 30000) + DoExpire(); + + 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) + { + cmd.Dispose(); + return true; + } + + cmd.Dispose(); + return false; + } + + public bool CheckToken(UUID principalID, string token, int lifetime) + { + if (System.Environment.TickCount - m_LastExpire > 30000) + DoExpire(); + + 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) + { + cmd.Dispose(); + return true; + } + + cmd.Dispose(); + + return false; + } + + private void DoExpire() + { + MySqlCommand cmd = new MySqlCommand("delete from tokens where validity < now()"); + ExecuteNonQuery(cmd); + + cmd.Dispose(); + + m_LastExpire = System.Environment.TickCount; + } } } diff --git a/OpenSim/Services/AuthenticationService/AuthenticationServiceBase.cs b/OpenSim/Services/AuthenticationService/AuthenticationServiceBase.cs index dab0598d83..5056db3f93 100644 --- a/OpenSim/Services/AuthenticationService/AuthenticationServiceBase.cs +++ b/OpenSim/Services/AuthenticationService/AuthenticationServiceBase.cs @@ -97,7 +97,7 @@ namespace OpenSim.Services.AuthenticationService public bool Verify(UUID principalID, string token, int lifetime) { - return false; + return m_Database.CheckToken(principalID, token, lifetime); } public bool VerifyEncrypted(byte[] cyphertext, byte[] key) @@ -107,7 +107,7 @@ namespace OpenSim.Services.AuthenticationService public virtual bool Release(UUID principalID, string token) { - return false; + return m_Database.CheckToken(principalID, token, 0); } public virtual bool ReleaseEncrypted(byte[] cyphertext, byte[] key) @@ -117,7 +117,12 @@ namespace OpenSim.Services.AuthenticationService protected string GetToken(UUID principalID, int lifetime) { - return "OK"; + UUID token = UUID.Random(); + + if (m_Database.SetToken(principalID, token.ToString(), lifetime)) + return token.ToString(); + + return String.Empty; } } }