banshee r3251 - in trunk/banshee: . src/Extensions/Banshee.Lastfm src/Extensions/Banshee.Lastfm/Banshee.Lastfm src/Libraries/Hyena src/Libraries/Hyena/Hyena src/Libraries/Lastfm src/Libraries/Lastfm.Gui/Lastfm.Gui src/Libraries/Lastfm/Lastfm tests tests/Hyena



Author: abock
Date: Sun Feb 17 01:32:13 2008
New Revision: 3251
URL: http://svn.gnome.org/viewvc/banshee?rev=3251&view=rev

Log:
2008-02-16  Aaron Bockover  <abock gnome org>

    * src/Libraries/Lastfm/Lastfm/Account.cs: Renamed Username to UserName

    * src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmActions.cs:
    * src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmSource.cs:
    * src/Libraries/Lastfm.Gui/Lastfm.Gui/AccountLoginForm.cs:
    * src/Libraries/Lastfm/Lastfm/Connection.cs: Use the UserName property

    * src/Extensions/Banshee.Lastfm/Banshee.Lastfm/StationSource.cs: Store
    the MD5 hashed last.fm password

    * src/Libraries/Hyena/Hyena/CryptoUtil.cs: Moved some MD5 methods from
    Last.fm to here

    * tests/Hyena/CryptoUtilTests.cs: Tests for CryptoUtil



Added:
   trunk/banshee/src/Libraries/Hyena/Hyena/CryptoUtil.cs
   trunk/banshee/src/Libraries/Hyena/Hyena/DateTimeUtil.cs
      - copied unchanged from r3248, /trunk/banshee/src/Libraries/Hyena/Hyena/Utilities.cs
   trunk/banshee/tests/Hyena/CryptoUtilTests.cs
Removed:
   trunk/banshee/src/Libraries/Hyena/Hyena/Utilities.cs
Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.mdp
   trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmActions.cs
   trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmSource.cs
   trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/StationSource.cs
   trunk/banshee/src/Libraries/Hyena/Hyena.mdp
   trunk/banshee/src/Libraries/Hyena/Makefile.am
   trunk/banshee/src/Libraries/Lastfm.Gui/Lastfm.Gui/AccountLoginForm.cs
   trunk/banshee/src/Libraries/Lastfm/Lastfm.mdp
   trunk/banshee/src/Libraries/Lastfm/Lastfm/Account.cs
   trunk/banshee/src/Libraries/Lastfm/Lastfm/Connection.cs
   trunk/banshee/tests/Makefile.am

Modified: trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.mdp
==============================================================================
--- trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.mdp	(original)
+++ trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.mdp	Sun Feb 17 01:32:13 2008
@@ -29,8 +29,9 @@
     <ProjectReference type="Gac" localcopy="True" refto="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     <ProjectReference type="Gac" localcopy="True" refto="glade-sharp, Version=2.8.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
     <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
-    <ProjectReference type="Project" localcopy="False" refto="Banshee.Base" />
     <ProjectReference type="Project" localcopy="False" refto="Banshee.Widgets" />
+    <ProjectReference type="Project" localcopy="True" refto="Lastfm" />
+    <ProjectReference type="Project" localcopy="True" refto="Lastfm.Gui" />
   </References>
   <DeploymentInformation strategy="File">
     <excludeFiles />

Modified: trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmActions.cs
==============================================================================
--- trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmActions.cs	(original)
+++ trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmActions.cs	Sun Feb 17 01:32:13 2008
@@ -232,7 +232,7 @@
         {
             AccountLoginDialog dialog = new AccountLoginDialog (lastfm.Account, true);
             dialog.SaveOnEdit = true;
-            if (lastfm.Account.Username == null) {
+            if (lastfm.Account.UserName == null) {
                 dialog.AddSignUpButton ();
             }
             dialog.Run ();
@@ -255,7 +255,7 @@
                 updating = true;
             }
 
-            bool have_user = (lastfm.Account.Username != null);
+            bool have_user = (lastfm.Account.UserName != null);
             this["LastfmAddAction"].Sensitive = have_user;
             this["LastfmSortAction"].Sensitive = have_user;
             this["LastfmConnectAction"].Visible = lastfm.Connection.State == ConnectionState.Disconnected;

Modified: trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmSource.cs
==============================================================================
--- trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmSource.cs	(original)
+++ trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/LastfmSource.cs	Sun Feb 17 01:32:13 2008
@@ -68,7 +68,8 @@
 
             // We don't automatically connect to Last.fm, but load the last Last.fm
             // username we used so we can load the user's stations.
-            account.Username = LastUserSchema.Get ();
+            account.UserName = LastUserSchema.Get ();
+            account.CryptedPassword = LastPassSchema.Get ();
 
             connection = new Connection (account, Banshee.Web.Browser.UserAgent);
             connection.UpdateNetworkState (NetworkDetect.Instance.Connected);
@@ -91,6 +92,11 @@
         public void Initialize ()
         {
             Connection.StateChanged += HandleConnectionStateChanged;
+            
+            if (Account.UserName != null && Account.CryptedPassword != null) {
+                Connection.Connect ();
+            }
+            
             UpdateUI ();
         }
 
@@ -196,14 +202,14 @@
         }
 
         private string last_username;
-        public void SetUsername (string username)
+        public void SetUserName (string username)
         {
             if (username != last_username) {
                 last_username = username;
                 LastfmSource.LastUserSchema.Set (last_username);
                 ClearChildSources ();
                 sorting = true;
-                foreach (StationSource child in StationSource.LoadAll (this, Account.Username)) {
+                foreach (StationSource child in StationSource.LoadAll (this, Account.UserName)) {
                     if (!child.Type.SubscribersOnly || Connection.Subscriber) {
                         AddChildSource (child);
                         //SourceManager.AddSource (child);
@@ -256,10 +262,15 @@
 
         private void UpdateUI ()
         {
-            bool have_user = (Account.Username != null);
-
+            bool have_user = Account.UserName != null;
+            bool have_pass = Account.CryptedPassword != null;
+            
+            if (have_pass) {
+                LastPassSchema.Set (Account.CryptedPassword);
+            }
+            
             if (have_user) {
-                SetUsername (Account.Username);
+                SetUserName (Account.UserName);
             } else {
                 ClearChildSources ();
             }
@@ -280,6 +291,10 @@
             "plugins.lastfm", "username", "", "Last.fm user", "Last.fm username"
         );
 
+        public static readonly SchemaEntry<string> LastPassSchema = new SchemaEntry<string> (
+            "plugins.lastfm", "password_hash", "", "Last.fm password", "Last.fm password (hashed)"
+        );
+
         public static readonly SchemaEntry<bool> ExpandedSchema = new SchemaEntry<bool> (
             "plugins.lastfm", "expanded", false, "Last.fm expanded", "Last.fm expanded"
         );

Modified: trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/StationSource.cs
==============================================================================
--- trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/StationSource.cs	(original)
+++ trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm/StationSource.cs	Sun Feb 17 01:32:13 2008
@@ -163,7 +163,7 @@
             HyenaSqliteCommand command = new HyenaSqliteCommand (
                 @"INSERT INTO LastfmStations (Creator, Name, Type, Arg, PlayCount)
                     VALUES (?, ?, ?, ?, ?)",
-                lastfm.Account.Username, Name, 
+                lastfm.Account.UserName, Name, 
                 Type.ToString (), Arg, PlayCount
             );
 

Modified: trunk/banshee/src/Libraries/Hyena/Hyena.mdp
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.mdp	(original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.mdp	Sun Feb 17 01:32:13 2008
@@ -54,7 +54,7 @@
     <File name="Hyena.Query/QueryValue.cs" subtype="Code" buildaction="Compile" />
     <File name="Hyena.Query/StringQueryValue.cs" subtype="Code" buildaction="Compile" />
     <File name="Hyena/Timer.cs" subtype="Code" buildaction="Compile" />
-    <File name="Hyena/Utilities.cs" subtype="Code" buildaction="Compile" />
+    <File name="Hyena/DateTimeUtil.cs" subtype="Code" buildaction="Compile" />
     <File name="Hyena.SExpEngine/ArithmeticFunctionSet.cs" subtype="Code" buildaction="Compile" />
     <File name="Hyena.SExpEngine/CastFunctionSet.cs" subtype="Code" buildaction="Compile" />
     <File name="Hyena.SExpEngine/CompareFunctionSet.cs" subtype="Code" buildaction="Compile" />
@@ -79,6 +79,7 @@
     <File name="Hyena.Query/QueryOrder.cs" subtype="Code" buildaction="Compile" />
     <File name="Hyena.Data.Sqlite/SqliteUtils.cs" subtype="Code" buildaction="Compile" />
     <File name="Hyena/Log.cs" subtype="Code" buildaction="Compile" />
+    <File name="Hyena/CryptoUtil.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

Added: trunk/banshee/src/Libraries/Hyena/Hyena/CryptoUtil.cs
==============================================================================
--- (empty file)
+++ trunk/banshee/src/Libraries/Hyena/Hyena/CryptoUtil.cs	Sun Feb 17 01:32:13 2008
@@ -0,0 +1,66 @@
+//
+// CryptoUtil.cs
+//
+// Author:
+//   Aaron Bockover <abockover novell com>
+//
+// Copyright (C) 2008 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Security.Cryptography;
+
+namespace Hyena
+{
+    public static class CryptoUtil
+    {
+        public static bool IsMd5Encoded (string text)
+        {
+            // A-Z is ignored on purpose
+            return text.Length != 32 ? false : Regex.IsMatch (text, "^[a-f0-9]{32}$");
+        }
+        
+        public static string Md5Encode (string text)
+        {
+            return Md5Encode (text, Encoding.ASCII);
+        }
+        
+        public static string Md5Encode (string text, Encoding encoding)
+        {
+            if (String.IsNullOrEmpty (text)) {
+                return String.Empty;
+            }
+            
+            MD5 md5 = MD5.Create ();
+            byte [] hash = md5.ComputeHash (encoding.GetBytes (text));
+
+            StringBuilder shash = new StringBuilder ();
+            for (int i = 0; i < hash.Length; i++) {
+                shash.Append (hash[i].ToString ("x2"));
+            }
+
+            return shash.ToString ();
+        }
+    }
+}

Modified: trunk/banshee/src/Libraries/Hyena/Makefile.am
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Makefile.am	(original)
+++ trunk/banshee/src/Libraries/Hyena/Makefile.am	Sun Feb 17 01:32:13 2008
@@ -67,12 +67,13 @@
 	Hyena.SExpEngine/StringFunctionSet.cs \
 	Hyena.SExpEngine/TreeNode.cs \
 	Hyena.SExpEngine/UtilityFunctionSet.cs \
+	Hyena/CryptoUtil.cs \
+	Hyena/DateTimeUtil.cs \
 	Hyena/IUndoAction.cs \
 	Hyena/Log.cs \
 	Hyena/StringUtil.cs \
 	Hyena/Timer.cs \
-	Hyena/UndoManager.cs \
-	Hyena/Utilities.cs
+	Hyena/UndoManager.cs
 
 include $(top_srcdir)/build/build.mk
 

Modified: trunk/banshee/src/Libraries/Lastfm.Gui/Lastfm.Gui/AccountLoginForm.cs
==============================================================================
--- trunk/banshee/src/Libraries/Lastfm.Gui/Lastfm.Gui/AccountLoginForm.cs	(original)
+++ trunk/banshee/src/Libraries/Lastfm.Gui/Lastfm.Gui/AccountLoginForm.cs	Sun Feb 17 01:32:13 2008
@@ -79,7 +79,7 @@
             Attach (password_entry, 1, 2, 1, 2, AttachOptions.Fill | AttachOptions.Expand, 
                 AttachOptions.Shrink, 0, 0);
                 
-            username_entry.Text = account.Username;
+            username_entry.Text = account.UserName;
             password_entry.Text = account.Password;
 
             username_entry.Changed += OnEntryChanged;
@@ -126,7 +126,7 @@
 
         private void UpdateLogin ()
         {
-            account.Username = username_entry.Text.Trim ();
+            account.UserName = username_entry.Text.Trim ();
             account.Password = password_entry.Text.Trim ();
             account.Save ();
         }

Modified: trunk/banshee/src/Libraries/Lastfm/Lastfm.mdp
==============================================================================
--- trunk/banshee/src/Libraries/Lastfm/Lastfm.mdp	(original)
+++ trunk/banshee/src/Libraries/Lastfm/Lastfm.mdp	Sun Feb 17 01:32:13 2008
@@ -16,5 +16,6 @@
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
     <ProjectReference type="Gac" localcopy="True" refto="gnome-sharp, Version=2.8.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
+    <ProjectReference type="Project" localcopy="True" refto="Hyena" />
   </References>
 </Project>
\ No newline at end of file

Modified: trunk/banshee/src/Libraries/Lastfm/Lastfm/Account.cs
==============================================================================
--- trunk/banshee/src/Libraries/Lastfm/Lastfm/Account.cs	(original)
+++ trunk/banshee/src/Libraries/Lastfm/Lastfm/Account.cs	Sun Feb 17 01:32:13 2008
@@ -30,7 +30,6 @@
 using System;
 using System.Collections;
 using System.Text;
-using System.Security.Cryptography;
 
 namespace Lastfm
 {
@@ -39,7 +38,7 @@
         public event EventHandler Updated;
 
         private string username;
-        public string Username {
+        public string UserName {
             get { return username; }
             set { username = value; }
         }
@@ -51,8 +50,20 @@
         }
 
         public string CryptedPassword {
-            get { return password == null ? null : Md5Encode (password); }
-            set { password = value; }
+            get {
+                // Okay, so this will explode if someone has a raw text password 
+                // that matches ^[a-f0-9]{32}$ ... likely? I hope not.
+            
+                if (password == null) {
+                    return null;
+                } else if (Hyena.CryptoUtil.IsMd5Encoded (password)) {
+                    return password;
+                }
+                
+                password = Hyena.CryptoUtil.Md5Encode (password);
+                return password;
+            }
+            set { password = String.IsNullOrEmpty (value) ? null : value; }
         }
 
         public void SignUp ()
@@ -70,23 +81,6 @@
             //Browser.Open ("http://last.fm/";);
         }
         
-        public static string Md5Encode (string text)
-        {
-            if (text == null || text == String.Empty)
-                return String.Empty;
-                
-            MD5 md5 = MD5.Create ();
-            byte[] hash = md5.ComputeHash (Encoding.ASCII.GetBytes (text));
-
-            StringBuilder shash = new StringBuilder ();
-            for (int i = 0; i < hash.Length; ++i) {
-                shash.Append (hash[i].ToString ("x2"));
-            }
-
-
-            return shash.ToString ();
-        }
-
         public virtual void Save ()
         {
             OnUpdated ();

Modified: trunk/banshee/src/Libraries/Lastfm/Lastfm/Connection.cs
==============================================================================
--- trunk/banshee/src/Libraries/Lastfm/Lastfm/Connection.cs	(original)
+++ trunk/banshee/src/Libraries/Lastfm/Lastfm/Connection.cs	Sun Feb 17 01:32:13 2008
@@ -151,7 +151,7 @@
             if (State == ConnectionState.Connecting || State == ConnectionState.Connected)
                 return;
 
-            if (account.Username == null || account.CryptedPassword == null) {
+            if (account.UserName == null || account.CryptedPassword == null) {
                 State = ConnectionState.NoAccount;
                 return;
             }
@@ -273,14 +273,14 @@
                         "http://ws.audioscrobbler.com/radio/handshake.php?version={0}&platform={1}&username={2}&passwordmd5={3}&language={4}&session=324234";,
                         "1.1.1",
                         "linux", // FIXME
-                        account.Username, account.CryptedPassword,
+                        account.UserName, account.CryptedPassword,
                         "en" // FIXME
                     ));
 
                     // Set us as connecting, assuming the connection attempt wasn't changed out from under us
                     if (ParseHandshake (new StreamReader (stream).ReadToEnd ()) && session != null) {
                         State = ConnectionState.Connected;
-                        Log.Debug (String.Format ("Logged into Last.fm as {0}", account.Username), null);
+                        Log.Debug (String.Format ("Logged into Last.fm as {0}", account.UserName), null);
                         return;
                     }
                 } catch (Exception e) {
@@ -481,8 +481,8 @@
         private LameXmlRpcRequest LastFMXmlRpcRequest (string method)
         {
             string time = UnixTime ();
-            string auth_hash = Account.Md5Encode (account.CryptedPassword + time);
-            return new LameXmlRpcRequest (method).AddStringParams (account.Username, time, auth_hash);
+            string auth_hash = Hyena.CryptoUtil.Md5Encode (account.CryptedPassword + time);
+            return new LameXmlRpcRequest (method).AddStringParams (account.UserName, time, auth_hash);
         }
 
         protected class LameXmlRpcRequest

Added: trunk/banshee/tests/Hyena/CryptoUtilTests.cs
==============================================================================
--- (empty file)
+++ trunk/banshee/tests/Hyena/CryptoUtilTests.cs	Sun Feb 17 01:32:13 2008
@@ -0,0 +1,23 @@
+using System;
+using NUnit.Framework;
+using Hyena;
+
+[TestFixture]
+public class CryptoUtilTests
+{
+    [Test]
+    public void Md5Encode ()
+    {
+        Assert.AreEqual ("ae2b1fca515949e5d54fb22b8ed95575", CryptoUtil.Md5Encode ("testing"));
+    }
+
+    [Test]
+    public void IsMd5Encoded ()
+    {
+        Assert.IsTrue (CryptoUtil.IsMd5Encoded ("ae2b1fca515949e5d54fb22b8ed95575"));
+        Assert.IsFalse (CryptoUtil.IsMd5Encoded ("abc233"));
+        Assert.IsFalse (CryptoUtil.IsMd5Encoded ("lebowski"));
+        Assert.IsFalse (CryptoUtil.IsMd5Encoded ("ae2b1fca515949e5g54fb22b8ed95575"));
+    }
+}
+

Modified: trunk/banshee/tests/Makefile.am
==============================================================================
--- trunk/banshee/tests/Makefile.am	(original)
+++ trunk/banshee/tests/Makefile.am	Sun Feb 17 01:32:13 2008
@@ -10,9 +10,10 @@
 	Banshee.Core/KernelTests.cs \
 	Banshee.Services/PlaylistFormatTests.cs \
 	Banshee.Services/Xspf/Xspf.cs \
-	Hyena/RangeCollectionTests.cs
+	Hyena/RangeCollectionTests.cs \
+	Hyena/CryptoUtilTests.cs
 
-CFS=Hyena/RangeCollectionTests.cs
+CFS=Hyena/RangeCollectionTests.cs Hyena/CryptoUtilTests.cs
 
 NUNIT_TESTER_NAME = ConsoleUi
 NUNIT_TESTER = $(NUNIT_TESTER_NAME).exe
@@ -30,7 +31,7 @@
 all: $(ASSEMBLY)
 
 run-test: $(NUNIT_TESTER) $(ASSEMBLY)
-	MONO_PATH="$(RUN_PATH)" mono --debug $(NUNIT_TESTER) $(ASSEMBLY)
+	MONO_PATH="$(top_builddir)/bin" mono --debug $(NUNIT_TESTER) $(ASSEMBLY)
 endif
 
 CLEANFILES = $(ASSEMBLY) $(NUNIT_TESTER) TestResult.xml



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]