From ac40c7a74c15e0f61ba5bfcb4c6a6fb39993a87c Mon Sep 17 00:00:00 2001 From: Melanie Date: Fri, 4 Sep 2009 07:48:09 +0100 Subject: Fully implement unencrypted auth token operations --- OpenSim/Data/IAuthenticationData.cs | 4 ++ OpenSim/Data/MySQL/MySQLAuthenticationData.cs | 52 ++++++++++++++++++++++ .../AuthenticationServiceBase.cs | 11 +++-- 3 files changed, 64 insertions(+), 3 deletions(-) (limited to 'OpenSim') diff --git a/OpenSim/Data/IAuthenticationData.cs b/OpenSim/Data/IAuthenticationData.cs index f848716..7753e04 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 19575ec..1ee64ce 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 dab0598..5056db3 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; } } } -- cgit v1.1