Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
fixup: Refactor Program to not be static.
Browse files Browse the repository at this point in the history
  • Loading branch information
J Wyman committed Jun 15, 2017
1 parent 195b4c8 commit 2d6a6fa
Show file tree
Hide file tree
Showing 14 changed files with 1,455 additions and 1,467 deletions.
8 changes: 4 additions & 4 deletions Cli-CredentialHelper.Test/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public void TryReadBooleanTest()

Program program = new Program();

Assert.IsFalse(Program.CommonFunctions.TryReadBoolean(program, opargsMock.Object, "notFound", "notFound", out yesno));
Assert.IsFalse(CommonFunctions.TryReadBoolean(program, opargsMock.Object, "notFound", "notFound", out yesno));
Assert.IsFalse(yesno.HasValue);

Assert.IsTrue(Program.CommonFunctions.TryReadBoolean(program, opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsTrue(CommonFunctions.TryReadBoolean(program, opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsTrue(yesno.HasValue);
Assert.IsFalse(yesno.Value);

Expand All @@ -108,7 +108,7 @@ public void TryReadBooleanTest()
opargsMock.Setup(r => r.EnvironmentVariables)
.Returns(envvars);

Assert.IsTrue(Program.CommonFunctions.TryReadBoolean(program, opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsTrue(CommonFunctions.TryReadBoolean(program, opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsTrue(yesno.HasValue);
Assert.IsTrue(yesno.Value);

Expand All @@ -120,7 +120,7 @@ public void TryReadBooleanTest()
opargsMock.Setup(r => r.EnvironmentVariables)
.Returns(envvars);

Assert.IsFalse(Program.CommonFunctions.TryReadBoolean(program, opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsFalse(CommonFunctions.TryReadBoolean(program, opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsFalse(yesno.HasValue);
}
}
Expand Down
15 changes: 9 additions & 6 deletions Cli-Shared/Cli-Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
<Compile Include="$(MSBuildThisFileDirectory)NativeMethods.cs" />
<Compile Include="$(MSBuildThisFileDirectory)OperationArguments.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Program.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Program.BitbucketFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Program.CommonFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Program.ConsoleFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Program.Delegates.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Program.DialogFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Program.GitHubFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Functions\Bitbucket.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Functions\Common.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Functions\Console.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Delegates.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Functions\Dialog.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Functions\GitHub.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Functions\" />
</ItemGroup>
</Project>
File renamed without changes.
103 changes: 103 additions & 0 deletions Cli-Shared/Functions/Bitbucket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**** Git Credential Manager for Windows ****
*
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
*
* 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.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Alm.Authentication;
using Bitbucket = Atlassian.Bitbucket.Authentication;

namespace Microsoft.Alm.Cli
{
internal static class BitbucketFunctions
{
public static bool CredentialPrompt(Program program, string titleMessage, TargetUri targetUri, out string username, out string password)
{
Credential credential;
if ((credential = program.BasicCredentialPrompt(targetUri, titleMessage)) != null)
{
username = credential.Username;
password = credential.Password;

return true;
}

username = null;
password = null;

return false;
}

public static bool OAuthPrompt(Program program, string title, TargetUri targetUri, Bitbucket.AuthenticationResultType resultType, string username)
{
const int BufferReadSize = 16 * 1024;

Debug.Assert(targetUri != null);

var buffer = new StringBuilder(BufferReadSize);
uint read = 0;
uint written = 0;

string accessToken = null;

var fileAccessFlags = NativeMethods.FileAccess.GenericRead | NativeMethods.FileAccess.GenericWrite;
var fileAttributes = NativeMethods.FileAttributes.Normal;
var fileCreationDisposition = NativeMethods.FileCreationDisposition.OpenExisting;
var fileShareFlags = NativeMethods.FileShare.Read | NativeMethods.FileShare.Write;

using (var stdout = NativeMethods.CreateFile(NativeMethods.ConsoleOutName, fileAccessFlags, fileShareFlags,
IntPtr.Zero, fileCreationDisposition, fileAttributes, IntPtr.Zero))
{
using (var stdin = NativeMethods.CreateFile(NativeMethods.ConsoleInName, fileAccessFlags, fileShareFlags,
IntPtr.Zero, fileCreationDisposition, fileAttributes, IntPtr.Zero))
{
buffer.AppendLine()
.Append(title)
.Append(" OAuth Access Token: ");

if (!NativeMethods.WriteConsole(stdout, buffer, (uint)buffer.Length, out written, IntPtr.Zero))
{
var error = Marshal.GetLastWin32Error();
throw new Win32Exception(error, "Unable to write to standard output (" + NativeMethods.Win32Error.GetText(error) + ").");
}
buffer.Clear();

// read input from the user
if (!NativeMethods.ReadConsole(stdin, buffer, BufferReadSize, out read, IntPtr.Zero))
{
var error = Marshal.GetLastWin32Error();
throw new Win32Exception(error, "Unable to read from standard input (" + NativeMethods.Win32Error.GetText(error) + ").");
}

accessToken = buffer.ToString(0, (int)read);
accessToken = accessToken.Trim(Program.NewLineChars);
}
}
return accessToken != null;
}
}
}
Loading

0 comments on commit 2d6a6fa

Please sign in to comment.