Skip to content

Commit 18e7f5c

Browse files
committed
helpers: allow auth helpers to be overriden by config
Allow authentication UI helpers to be overriden by environment variables and Git configuration options.
1 parent f58ca8c commit 18e7f5c

File tree

8 files changed

+70
-53
lines changed

8 files changed

+70
-53
lines changed

src/shared/Atlassian.Bitbucket/BitbucketAuthentication.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
// Licensed under the MIT license.
33
using System;
44
using System.Collections.Generic;
5-
using System.IO;
65
using System.Net.Http;
7-
using System.Reflection;
86
using System.Text;
97
using System.Threading;
108
using System.Threading.Tasks;
@@ -150,16 +148,11 @@ public async Task<OAuth2TokenResult> RefreshOAuthCredentialsAsync(string refresh
150148

151149
private bool TryFindHelperExecutablePath(out string path)
152150
{
153-
string helperName = BitbucketConstants.BitbucketAuthHelperName;
154-
155-
if (PlatformUtils.IsWindows())
156-
{
157-
helperName += ".exe";
158-
}
159-
160-
string executableDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
161-
path = Path.Combine(executableDirectory, helperName);
162-
return Context.FileSystem.FileExists(path);
151+
return TryFindHelperExecutablePath(
152+
BitbucketConstants.EnvironmentVariables.AuthenticationHelper,
153+
BitbucketConstants.GitConfiguration.Credential.AuthenticationHelper,
154+
BitbucketConstants.DefaultAuthenticationHelper,
155+
out path);
163156
}
164157

165158
private HttpClient _httpClient;

src/shared/Atlassian.Bitbucket/BitbucketConstants.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static class BitbucketConstants
88
{
99
public const string BitbucketBaseUrlHost = "bitbucket.org";
1010
public static readonly Uri BitbucketApiUri = new Uri("https://api.bitbucket.org");
11-
public const string BitbucketAuthHelperName = "Atlassian.Bitbucket.UI";
11+
public const string DefaultAuthenticationHelper = "Atlassian.Bitbucket.UI";
1212

1313
// TODO: use the GCM client ID and secret once we have this approved.
1414
// Until then continue to use Sourcetree's values like GCM Windows.
@@ -30,6 +30,7 @@ public static class OAuthScopes
3030

3131
public static class EnvironmentVariables
3232
{
33+
public const string AuthenticationHelper = "GCM_BITBUCKET_HELPER";
3334
public const string DevOAuthClientId = "GCM_DEV_BITBUCKET_CLIENTID";
3435
public const string DevOAuthClientSecret = "GCM_DEV_BITBUCKET_CLIENTSECRET";
3536
public const string DevOAuthRedirectUri = "GCM_DEV_BITBUCKET_REDIRECTURI";
@@ -39,6 +40,7 @@ public static class GitConfiguration
3940
{
4041
public static class Credential
4142
{
43+
public const string AuthenticationHelper = "bitbucketHelper";
4244
public const string DevOAuthClientId = "bitbucketDevClientId";
4345
public const string DevOAuthClientSecret = "bitbucketDevClientSecret";
4446
public const string DevOAuthRedirectUri = "bitbucketDevRedirectUri";

src/shared/GitHub/GitHubAuthentication.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
using System;
44
using System.Threading.Tasks;
55
using System.Collections.Generic;
6-
using System.IO;
76
using System.Net.Http;
8-
using System.Reflection;
97
using System.Text;
108
using System.Threading;
119
using Microsoft.Git.CredentialManager;
@@ -231,29 +229,11 @@ public async Task<OAuth2TokenResult> GetOAuthTokenAsync(Uri targetUri, IEnumerab
231229

232230
private bool TryFindHelperExecutablePath(out string path)
233231
{
234-
string helperName = GitHubConstants.AuthHelperName;
235-
236-
if (PlatformUtils.IsWindows())
237-
{
238-
helperName += ".exe";
239-
}
240-
241-
string executableDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
242-
path = Path.Combine(executableDirectory, helperName);
243-
if (!Context.FileSystem.FileExists(path))
244-
{
245-
Context.Trace.WriteLine($"Did not find helper '{helperName}' in '{executableDirectory}'");
246-
247-
// We currently only have a helper on Windows. If we failed to find the helper we should warn the user.
248-
if (PlatformUtils.IsWindows())
249-
{
250-
Context.Streams.Error.WriteLine($"warning: missing '{helperName}' from installation.");
251-
}
252-
253-
return false;
254-
}
255-
256-
return true;
232+
return TryFindHelperExecutablePath(
233+
GitHubConstants.EnvironmentVariables.AuthenticationHelper,
234+
GitHubConstants.GitConfiguration.Credential.AuthenticationHelper,
235+
GitHubConstants.DefaultAuthenticationHelper,
236+
out path);
257237
}
258238

259239
private HttpClient _httpClient;

src/shared/GitHub/GitHubConstants.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class GitHubConstants
99
public const string GitHubBaseUrlHost = "github.com";
1010
public const string GistBaseUrlHost = "gist." + GitHubBaseUrlHost;
1111

12-
public const string AuthHelperName = "GitHub.UI";
12+
public const string DefaultAuthenticationHelper = "GitHub.UI";
1313

1414
public const string OAuthClientId = "0120e057bd645470c1ed";
1515
public const string OAuthClientSecret = "18867509d956965542b521a529a79bb883344c90";
@@ -57,6 +57,7 @@ public static class OAuthScopes
5757

5858
public static class EnvironmentVariables
5959
{
60+
public const string AuthenticationHelper = "GCM_GITHUB_HELPER";
6061
public const string AuthenticationModes = "GCM_GITHUB_AUTHMODES";
6162
public const string DevOAuthClientId = "GCM_DEV_GITHUB_CLIENTID";
6263
public const string DevOAuthClientSecret = "GCM_DEV_GITHUB_CLIENTSECRET";
@@ -67,7 +68,8 @@ public static class GitConfiguration
6768
{
6869
public static class Credential
6970
{
70-
public const string AuthModes = "gitHubAuthModes";
71+
public const string AuthenticationHelper = "gitHubHelper";
72+
public const string AuthenticationModes = "gitHubAuthModes";
7173
public const string DevOAuthClientId = "gitHubDevClientId";
7274
public const string DevOAuthClientSecret = "gitHubDevClientSecret";
7375
public const string DevOAuthRedirectUri = "gitHubDevRedirectUri";

src/shared/GitHub/GitHubHostProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ internal async Task<AuthenticationModes> GetSupportedAuthenticationModesAsync(Ur
143143
// Check for an explicit override for supported authentication modes
144144
if (Context.Settings.TryGetSetting(
145145
GitHubConstants.EnvironmentVariables.AuthenticationModes,
146-
Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.AuthModes,
146+
Constants.GitConfiguration.Credential.SectionName, GitHubConstants.GitConfiguration.Credential.AuthenticationModes,
147147
out string authModesStr))
148148
{
149149
if (Enum.TryParse(authModesStr, true, out AuthenticationModes authModes) && authModes != AuthenticationModes.None)

src/shared/Microsoft.Git.CredentialManager/Authentication/AuthenticationBase.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.IO;
7+
using System.Reflection;
68
using System.Threading.Tasks;
79

810
namespace Microsoft.Git.CredentialManager.Authentication
@@ -86,5 +88,47 @@ protected void ThrowIfTerminalPromptsDisabled()
8688
throw new InvalidOperationException("Cannot prompt because terminal prompts have been disabled.");
8789
}
8890
}
91+
92+
protected bool TryFindHelperExecutablePath(string envar, string configName, string defaultValue, out string path)
93+
{
94+
bool isOverride = false;
95+
if (Context.Settings.TryGetSetting(
96+
envar, Constants.GitConfiguration.Credential.SectionName, configName, out string helperName))
97+
{
98+
Context.Trace.WriteLine($"UI helper override specified: '{helperName}'.");
99+
isOverride = true;
100+
}
101+
else
102+
{
103+
// Use the default helper if none was specified.
104+
// On Windows append ".exe" for the default helpers only. If a user has specified their own
105+
// helper they should append the correct extension.
106+
helperName = PlatformUtils.IsWindows() ? $"{defaultValue}.exe" : defaultValue;
107+
}
108+
109+
if (Path.IsPathRooted(helperName))
110+
{
111+
path = helperName;
112+
}
113+
else
114+
{
115+
string executableDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
116+
path = Path.Combine(executableDirectory!, helperName);
117+
}
118+
119+
if (!Context.FileSystem.FileExists(path))
120+
{
121+
// Only warn for missing helpers specified by the user, not in-box ones
122+
if (isOverride)
123+
{
124+
Context.Trace.WriteLine($"UI helper '{helperName}' was not found at '{path}'.");
125+
Context.Streams.Error.WriteLine($"warning: could not find configured UI helper '{helperName}'");
126+
}
127+
128+
return false;
129+
}
130+
131+
return true;
132+
}
89133
}
90134
}

src/shared/Microsoft.Git.CredentialManager/Authentication/MicrosoftAuthentication.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Net.Http;
7-
using System.Reflection;
87
using System.Threading.Tasks;
98
using Microsoft.Identity.Client;
109
using Microsoft.Identity.Client.Extensions.Msal;
@@ -201,16 +200,11 @@ private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
201200

202201
private bool TryFindHelperExecutablePath(out string path)
203202
{
204-
string helperName = Constants.MicrosoftAuthHelperName;
205-
206-
if (PlatformUtils.IsWindows())
207-
{
208-
helperName += ".exe";
209-
}
210-
211-
string executableDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
212-
path = Path.Combine(executableDirectory, helperName);
213-
return Context.FileSystem.FileExists(path);
203+
return TryFindHelperExecutablePath(
204+
Constants.EnvironmentVariables.MsAuthHelper,
205+
Constants.GitConfiguration.Credential.MsAuthHelper,
206+
Constants.DefaultMsAuthHelper,
207+
out path);
214208
}
215209

216210
private async Task RegisterVisualStudioTokenCacheAsync(IPublicClientApplication app)

src/shared/Microsoft.Git.CredentialManager/Constants.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class Constants
99
{
1010
public const string PersonalAccessTokenUserName = "PersonalAccessToken";
1111
public const string OAuthTokenUserName = "OAuthToken";
12-
public const string MicrosoftAuthHelperName = "Microsoft.Authentication.Helper";
12+
public const string DefaultMsAuthHelper = "Microsoft.Authentication.Helper";
1313

1414
public const string ProviderIdAuto = "auto";
1515
public const string AuthorityIdAuto = "auto";
@@ -49,6 +49,7 @@ public static class EnvironmentVariables
4949
public const string GitSslNoVerify = "GIT_SSL_NO_VERIFY";
5050
public const string GcmInteractive = "GCM_INTERACTIVE";
5151
public const string GcmParentWindow = "GCM_MODAL_PARENTHWND";
52+
public const string MsAuthHelper = "GCM_MSAUTH_HELPER";
5253
}
5354

5455
public static class Http
@@ -74,6 +75,7 @@ public static class Credential
7475
public const string HttpsProxy = "httpsProxy";
7576
public const string UseHttpPath = "useHttpPath";
7677
public const string Interactive = "interactive";
78+
public const string MsAuthHelper = "msauthHelper";
7779
}
7880

7981
public static class Http

0 commit comments

Comments
 (0)