Commits

Devin Martin  committed 84db277

Stub in support for Box.net.

  • Participants
  • Parent commits bc8b02e

Comments (0)

Files changed (11)

File KeeCloud/Forms/FormSelectProvider.cs

-using KeeCloud.Providers;
-using System;
+using System;
 using System.Linq;
 using System.Windows.Forms;
+using KeeCloud.Providers;
+using KeeCloud.Utilities;
 
 namespace KeeCloud.Forms
 {
         public FormSelectProvider()
         {
             InitializeComponent();
-            this.comboBoxProviders.DisplayMember = "FriendlyName";
-            this.comboBoxProviders.DataSource = ProviderRegistry.SupportedWebRequests.Select(wr => new DataItem(wr)).Where(wr => wr.Provider.CanConfigureCredentials).ToArray();
+
+            this.comboBoxProviders.DisplayMember = ReflectionHelpers.MemberNameFromExpression<DataItem, string>(i => i.FriendlyName);
+
+            this.comboBoxProviders.DataSource = ProviderRegistry.SupportedWebRequests.Select(wr => new DataItem(wr))
+                .Where(wr => wr.Provider.CanConfigureCredentials)
+                .OrderBy(i => i.FriendlyName).ToArray();
         }
 
         private void comboBoxProviders_SelectedIndexChanged(object sender, EventArgs e)

File KeeCloud/KeeCloud.csproj

       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\packages\AWSSDK.1.5.12.0\lib\AWSSDK.dll</HintPath>
     </Reference>
+    <Reference Include="BoxSync.Core">
+      <HintPath>..\lib\BoxSync.Core.dll</HintPath>
+    </Reference>
     <Reference Include="DropNet, Version=1.8.2.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\packages\DropNet.1.9.3\lib\net35\DropNet.dll</HintPath>
     <Reference Include="System" />
     <Reference Include="System.Data" />
     <Reference Include="System.Drawing" />
+    <Reference Include="System.Web.Services" />
     <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
   </ItemGroup>
     <Compile Include="Forms\FormSelectProvider.Designer.cs">
       <DependentUpon>FormSelectProvider.cs</DependentUpon>
     </Compile>
+    <Compile Include="Providers\BoxNet\Api.cs" />
     <Compile Include="Providers\CredentialClaimResult.cs" />
     <Compile Include="Forms\FormCredentialWizard.cs">
       <SubType>Form</SubType>
     </Compile>
     <Compile Include="Providers\Dropbox\DropboxCredentialConfigurationProvider.cs" />
     <Compile Include="Providers\Dropbox\Api.cs" />
+    <Compile Include="Providers\BoxNet\BoxNetCredentialConfigurationProvider.cs" />
+    <Compile Include="Providers\BoxNet\BoxNetProvider.cs" />
     <Compile Include="Providers\Dummy\DummyConfiguration.cs" />
     <Compile Include="Providers\Dummy\DummyProvider.cs" />
     <Compile Include="Providers\ICredentialConfigurationProvider.cs" />
     <Compile Include="StandardProtectedStrings.cs" />
     <Compile Include="Utilities\AsyncOperationWithSynchronizationContext.cs" />
     <Compile Include="Utilities\DatabaseHelpers.cs" />
+    <Compile Include="Utilities\ReflectionHelpers.cs" />
     <Compile Include="WebRequests\EntryCredentials.cs" />
     <Compile Include="WebRequests\SuccessWebResponse.cs" />
     <Compile Include="WebRequests\ProviderWebRequest.cs" />

File KeeCloud/ProviderRegistry.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net;
 
 namespace KeeCloud
 {
                 // add a new yield return with a protocol prefix and a delegate to create for each supported handler in the plugin
                 yield return new ProviderItem("dropbox", () => new KeeCloud.Providers.Dropbox.DropboxProvider());
                 yield return new ProviderItem("s3", () => new KeeCloud.Providers.Amazon.AmazonS3Provider());
+                yield return new ProviderItem("boxnet", () => new KeeCloud.Providers.BoxNet.BoxNetProvider());
 
 
                 // this is a dummy provider used mainly for testing the UI of the credential configuration wizard
             var creator = new ProviderWebRequestCreator(host);
             foreach (var supported in SupportedWebRequests)
             {
-                ProviderWebRequest.RegisterPrefix(supported.Protocol + ":", creator);
+                // register with the .net webrequest a protocol. When KeePass gets a WebRequest for the
+                // specified protocol .net will use the ProviderWebRequestCreator to create a specialized
+                // web request for the given protocol.
+                WebRequest.RegisterPrefix(supported.Protocol + ":", creator);
             }
         }
 

File KeeCloud/Providers/BoxNet/Api.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Text;
+using BoxSync.Core;
+
+namespace KeeCloud.Providers.BoxNet
+{
+    class Api
+    {
+        /*
+        The consumer key and the secret key included here are dummy keys.
+        You should go to http://Box.net/developers to create your own application
+        and get your own keys.
+
+        This is done to prevent bots from scraping the keys from the source code posted on the web.
+         
+        Every now and then an accidental checkin of keys may occur, but these are all dummy applications
+        created specifically for development that are deleted frequently.
+        */
+
+        public const string applicationKey = "dummy";
+
+        public const string BaseAuthorizationUrl = "";
+
+        private const string serviceUrl = "";
+
+        public static BoxManager Client
+        {
+            get
+            {
+                return new BoxSync.Core.BoxManager(Api.applicationKey, Api.serviceUrl, null);
+            }
+        }
+
+        public static BoxManager AuthenticatedClient(NetworkCredential credential)
+        {
+            return new BoxManager(Api.applicationKey, Api.serviceUrl, null, credential.Password);
+        }
+    }
+}

File KeeCloud/Providers/BoxNet/BoxNetCredentialConfigurationProvider.cs

+using System;
+using BoxSync.Core.Primitives;
+using BoxSync.Core.ServiceReference;
+
+namespace KeeCloud.Providers.BoxNet
+{
+    public class BoxNetCredentialConfigurationProvider : ICredentialConfigurationProvider
+    {
+        string ticket;
+        InitializeResult ICredentialConfigurationProvider.Initialize()
+        {
+            var client = Api.Client;
+            
+            string ticket = null;
+            var status = client.GetTicket(out ticket);
+
+            if (status == BoxSync.Core.Statuses.GetTicketStatus.Successful)
+            {
+                this.ticket = ticket;
+
+                return InitializeResult.Ok;
+            }
+            else
+                return InitializeResult.Error;
+        }
+
+        Uri ICredentialConfigurationProvider.GetExternalAuthorizationUrl()
+        {
+            return new Uri(string.Format(Api.BaseAuthorizationUrl, this.ticket));
+        }
+
+        CredentialClaimResult ICredentialConfigurationProvider.Claim()
+        {
+            var client = Api.Client;
+
+            string oauthToken = null;
+            User user = null;
+            var status = client.GetAuthenticationToken(this.ticket, out oauthToken, out user);
+            if (status == BoxSync.Core.Statuses.GetAuthenticationTokenStatus.Successful)
+            {
+                return new CredentialClaimResult(user.Email, oauthToken);
+            }
+            else
+                return new CredentialClaimResult();
+        }
+    }
+}

File KeeCloud/Providers/BoxNet/BoxNetProvider.cs

+using System;
+using System.IO;
+using System.Net;
+using BoxSync.Core.ServiceReference;
+
+namespace KeeCloud.Providers.BoxNet
+{
+    public class BoxNetProvider : IProvider
+    {
+        private Uri uri;
+
+        Uri IProvider.Uri { get { return this.uri; } set { this.uri = value; } }
+
+        Stream IProvider.Get(ICredentials credentials)
+        {
+            throw new NotImplementedException();
+        }
+
+        void IProvider.Put(Stream stream, ICredentials credentials)
+        {
+            throw new NotImplementedException();
+        }
+
+        void IProvider.Delete(ICredentials credentials)
+        {
+            throw new NotImplementedException();
+        }
+
+        void IProvider.Move(Uri destination, ICredentials credentials)
+        {
+            throw new NotImplementedException();
+        }
+
+        bool IProvider.CanConfigureCredentials
+        {
+            get { return true; }
+        }
+
+        ICredentialConfigurationProvider IProvider.CredentialConfigurationProvider
+        {
+            get { return new BoxNetCredentialConfigurationProvider(); }
+        }
+
+        string IProvider.FriendlyName
+        {
+            get { return "Box.net"; }
+        }
+
+        private NetworkCredential GetCredential(ICredentials credentials)
+        {
+            return credentials.GetCredential(this.uri, "basic");
+        }
+    }
+}

File KeeCloud/Utilities/ReflectionHelpers.cs

+using System;
+using System.Linq.Expressions;
+
+namespace KeeCloud.Utilities
+{
+    class ReflectionHelpers
+    {
+        /// <summary>
+        /// Takes a lambda expression that accesses a member and returns the name of the member
+        /// </summary>
+        /// <typeparam name="TObject">The type that contains the member</typeparam>
+        /// <typeparam name="TMember">The type of the member</typeparam>
+        /// <param name="expression">Lambda expression that does nothing more than access (return) the value of the member</param>
+        /// <returns>String name of the member that was acessed by the expression</returns>
+        public static string MemberNameFromExpression<TObject, TMember>(Expression<Func<TObject, TMember>> expression)
+        {
+            if (expression.Body is MemberExpression)
+            {
+                MemberExpression body = (MemberExpression)expression.Body;
+                return body.Member.Name;
+            }
+            else
+                throw new Exception("The lambda expression is invalid. Use format () => object.member and nothing more. Ensure that you are accessing a property or a field only");
+        }
+    }
+}

File KeeCloud/WebRequests/ProviderWebRequest.cs

 
 namespace KeeCloud.WebRequests
 {
+    /// <summary>
+    /// Custom web request that uses an IProvider to perform its web operations
+    /// </summary>
     public class ProviderWebRequest : WebRequest
     {
         private readonly Uri requestUri;
             }
             else if ((this.Method ?? string.Empty).ToLowerInvariant() == "post")
             {
-                this.provider.Put(this.requestStream.GetReadableStream(), this.GetCredentials());
-                return new SuccessWebResponse();
+                using (var stream = this.requestStream.GetReadableStream())
+                {
+                    this.provider.Put(stream, this.GetCredentials());
+                    return new SuccessWebResponse();
+                }
             }
             else
             {

File KeeCloud/WebRequests/ProviderWebRequestCreator.cs

 
         public WebRequest Create(Uri uri)
         {
+            // this method is called by the .net framework when it wants a specialized
+            // web request for a registered non-standard protocol.  KeeCloud has only one
+            // specialized WebRequest type, but we pass a provider into the web reqeust.
             var supported = ProviderRegistry.GetProviderForUri(uri);
             return new ProviderWebRequest(uri, supported.Create(uri), this.host);
         }

File lib/BoxSync.Core.dll

Binary file added.

File lib/ICSharpCode.SharpZipLib.dll

Binary file added.