Skip to content

Commit

Permalink
Store latest password to local credential storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mushketyk committed Mar 2, 2017
1 parent f350b12 commit fa52506
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
38 changes: 38 additions & 0 deletions VisualStudioPlugin/CredentialsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using CredentialManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuantConnect.VisualStudioPlugin
{
class CredentialsManager
{
private const string CREDENTIAL_TARGET = "QuantConnectPlugin";

public Credentials? GetLastCredential()
{
var cm = new Credential { Target = CREDENTIAL_TARGET };
if (!cm.Load())
{
return null;
}

return new Credentials(cm.Username, cm.Password);
}

public void SetCredentials(Credentials credentials)
{
var credential = new Credential
{
Target = CREDENTIAL_TARGET,
Username = credentials.UserId,
Password = credentials.AccessToken,
PersistanceType = PersistanceType.LocalComputer
};

credential.Save();
}
}
}
21 changes: 21 additions & 0 deletions VisualStudioPlugin/LogInCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,27 @@ namespace QuantConnect.VisualStudioPlugin
/// </summary>
class LogInCommand
{

private static CredentialsManager _credentialsManager = new CredentialsManager();
/// <summary>
/// Perform QuantConnect authentication
/// </summary>
/// <param name="serviceProvider">Visual Studio services provider</param>
/// <returns>true if user logged into QuantConnect, false otherwise</returns>
public static bool DoLogIn(IServiceProvider serviceProvider)
{

var authorizationManager = AuthorizationManager.GetInstance();
if (authorizationManager.IsLoggedIn())
{
return true;
}

if (LoggedInWithLastStorredPassword())
{
return true;
}

var logInDialog = new LogInDialog(authorizationManager);
logInDialog.HasMinimizeButton = false;
logInDialog.HasMaximizeButton = false;
Expand All @@ -45,6 +53,7 @@ public static bool DoLogIn(IServiceProvider serviceProvider)

if (credentials.HasValue)
{
_credentialsManager.SetCredentials(credentials.Value);
VsUtils.DisplayInStatusBar(serviceProvider, "Logged into QuantConnect");
return true;
}
Expand All @@ -53,5 +62,17 @@ public static bool DoLogIn(IServiceProvider serviceProvider)
return false;
}
}

private static bool LoggedInWithLastStorredPassword()
{
var nullableCredentials =_credentialsManager.GetLastCredential();
if (!nullableCredentials.HasValue)
{
return false;
}

var credentials = nullableCredentials.Value;
return AuthorizationManager.GetInstance().LogIn(credentials.UserId, credentials.AccessToken);
}
}
}
6 changes: 6 additions & 0 deletions VisualStudioPlugin/QuantConnect.VisualStudioPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<ItemGroup>
<Compile Include="AuthorizationManager.cs" />
<Compile Include="Credentials.cs" />
<Compile Include="CredentialsManager.cs" />
<Compile Include="LogInCommand.cs" />
<Compile Include="LogInDialog.xaml.cs" />
<Compile Include="NotAuthenticatedException.cs" />
Expand All @@ -66,6 +67,7 @@
<Compile Include="VsUtils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Key.snk" />
<None Include="packages.config" />
<None Include="source.extension.vsixmanifest">
Expand All @@ -89,6 +91,10 @@
<Content Include="Resources\ToolMenuCommand.png" />
</ItemGroup>
<ItemGroup>
<Reference Include="CredentialManagement, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CredentialManagement.1.0.2\lib\net35\CredentialManagement.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
Expand Down
11 changes: 11 additions & 0 deletions VisualStudioPlugin/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
1 change: 1 addition & 0 deletions VisualStudioPlugin/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CredentialManagement" version="1.0.2" targetFramework="net452" />
<package id="Microsoft.VisualStudio.Imaging" version="14.3.25407" targetFramework="net452" />
<package id="Microsoft.VisualStudio.OLE.Interop" version="7.10.6070" targetFramework="net452" />
<package id="Microsoft.VisualStudio.Shell.14.0" version="14.3.25407" targetFramework="net452" />
Expand Down

0 comments on commit fa52506

Please sign in to comment.