Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,12 @@ method
0. `exit`: contains current executable's exit code
0. `child_start`: describes a child process that is about to be spawned
0. `child_exit`: describes a child process at exit
0. `cmd_name`: identifies the canonical command and inherited command hierarchy
0. `region_enter`: describes a region (e.g. a timer for a section of code that
is interesting) on entry
0. `region_leave`: describes a region on leaving
0. `data`: records a thread- and region-local key/value pair
0. `data_json`: records a thread- and region-local structured JSON value

You can read more about each of these format targets in the [corresponding
section][trace2-events] of Git's Trace2 API documentation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ public async Task BitbucketOAuth2Client_GetAuthorizationCodeAsync_RespectsClient
[Fact]
public async Task BitbucketOAuth2Client_GetDeviceCodeAsync()
{
var trace2 = new NullTrace2();
var client = new Bitbucket.Cloud.BitbucketOAuth2Client(httpClient.Object, settings.Object, trace2);
await Assert.ThrowsAsync<Trace2InvalidOperationException>(async () => await client.GetDeviceCodeAsync(scopes, ct));
var client = new Bitbucket.Cloud.BitbucketOAuth2Client(httpClient.Object, settings.Object);
await Assert.ThrowsAsync<InvalidOperationException>(async () => await client.GetDeviceCodeAsync(scopes, ct));
}

[Theory]
Expand All @@ -80,8 +79,7 @@ public async Task BitbucketOAuth2Client_GetDeviceCodeAsync()
[InlineData("https", "example.com/", "john", "https://example.com/refresh_token")]
public void BitbucketOAuth2Client_GetRefreshTokenServiceName(string protocol, string host, string username, string expectedResult)
{
var trace2 = new NullTrace2();
var client = new Bitbucket.Cloud.BitbucketOAuth2Client(httpClient.Object, settings.Object, trace2);
var client = new Bitbucket.Cloud.BitbucketOAuth2Client(httpClient.Object, settings.Object);
var request = new GitRequest(new Dictionary<string, string>
{
["protocol"] = protocol,
Expand All @@ -102,8 +100,7 @@ private void VerifyAuthorizationCodeResult(OAuth2AuthorizationCodeResult result)

private Bitbucket.Cloud.BitbucketOAuth2Client GetBitbucketOAuth2Client()
{
var trace2 = new NullTrace2();
var client = new Bitbucket.Cloud.BitbucketOAuth2Client(httpClient.Object, settings.Object, trace2);
var client = new Bitbucket.Cloud.BitbucketOAuth2Client(httpClient.Object, settings.Object);
client.CodeGenerator = codeGenerator.Object;
return client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ private void VerifyAuthorizationCodeResult(OAuth2AuthorizationCodeResult result,

private Bitbucket.DataCenter.BitbucketOAuth2Client GetBitbucketOAuth2Client()
{
var trace2 = new NullTrace2();
var client = new Bitbucket.DataCenter.BitbucketOAuth2Client(httpClient.Object, settings.Object, trace2);
var client = new Bitbucket.DataCenter.BitbucketOAuth2Client(httpClient.Object, settings.Object);
client.CodeGenerator = codeGenerator.Object;
return client;
}
Expand Down
10 changes: 8 additions & 2 deletions src/Atlassian.Bitbucket/BitbucketAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public BitbucketAuthentication(ICommandContext context, IRegistry<BitbucketOAuth

public async Task<CredentialsPromptResult> GetCredentialsAsync(Uri targetUri, string userName, AuthenticationModes modes)
{
using var _ = Trace2.StartRegion("bitbucket", "get_creds");

ThrowIfUserInteractionDisabled();

// If we don't have a desktop session/GUI then we cannot offer OAuth since the only
Expand Down Expand Up @@ -234,12 +236,12 @@ private async Task<CredentialsPromptResult> GetCredentialsViaHelperAsync(
{
if (!output.TryGetValue("username", out userName))
{
throw new Trace2Exception(Context.Trace2, "Missing username in response");
throw new Exception("Missing username in response");
}

if (!output.TryGetValue("password", out string password))
{
throw new Trace2Exception(Context.Trace2, "Missing password in response");
throw new Exception("Missing password in response");
}

return new CredentialsPromptResult(
Expand All @@ -250,6 +252,8 @@ private async Task<CredentialsPromptResult> GetCredentialsViaHelperAsync(

public async Task<OAuth2TokenResult> CreateOAuthCredentialsAsync(GitRequest request)
{
using var _ = Trace2.StartRegion("bitbucket", "oauth_browser");

ThrowIfUserInteractionDisabled();

var browserOptions = new OAuth2WebBrowserOptions
Expand All @@ -267,6 +271,8 @@ public async Task<OAuth2TokenResult> CreateOAuthCredentialsAsync(GitRequest requ

public async Task<OAuth2TokenResult> RefreshOAuthCredentialsAsync(GitRequest request, string refreshToken)
{
using var _ = Trace2.StartRegion("bitbucket", "oauth_refresh");

var client = _oauth2ClientRegistry.Get(request);
return await client.GetTokenByRefreshTokenAsync(refreshToken, CancellationToken.None);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Atlassian.Bitbucket/BitbucketHostProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task<GitResponse> GetCredentialAsync(GitRequest request)
StringComparer.OrdinalIgnoreCase.Equals(request.Protocol, "http") &&
BitbucketHelper.IsBitbucketOrg(request))
{
throw new Trace2Exception(_context.Trace2,
throw new Exception(
"Unencrypted HTTP is not recommended for Bitbucket.org. " +
"Ensure the repository remote URL is using HTTPS " +
$"or see {Constants.HelpUrls.GcmUnsafeRemotes} about how to allow unsafe remotes.");
Expand Down Expand Up @@ -158,7 +158,7 @@ private async Task<ICredential> GetRefreshedCredentials(GitRequest request, Auth
{
var message = "User cancelled credential prompt";
_context.Trace.WriteLine(message);
throw new Trace2Exception(_context.Trace2, message);
throw new Exception(message);
}

switch (result.AuthenticationMode)
Expand Down Expand Up @@ -191,7 +191,7 @@ private async Task<ICredential> GetRefreshedCredentials(GitRequest request, Auth
var message = "Failed to refresh existing OAuth credential using refresh token";
_context.Trace.WriteLine(message);
_context.Trace.WriteException(ex);
_context.Trace2.WriteError(message);
Trace2.WriteError(message);

// We failed to refresh the AT using the RT; log the refresh failure and fall through to restart
// the OAuth authentication flow
Expand Down Expand Up @@ -317,7 +317,7 @@ public async Task<AuthenticationModes> GetSupportedAuthenticationModesAsync(GitR

_context.Trace.WriteLine(message);
_context.Trace.WriteException(ex);
_context.Trace2.WriteError(message, format);
Trace2.WriteError(message, format);

_context.Console.WriteWarning(message);

Expand Down Expand Up @@ -374,7 +374,7 @@ private async Task<string> ResolveOAuthUserNameAsync(GitRequest request, string
return result.Response.UserName;
}

throw new Trace2Exception(_context.Trace2,
throw new Exception(
$"Failed to resolve username. HTTP: {result.StatusCode}");
}

Expand All @@ -386,7 +386,7 @@ private async Task<string> ResolveBasicAuthUserNameAsync(GitRequest request, str
return result.Response.UserName;
}

throw new Trace2Exception(_context.Trace2,
throw new Exception(
$"Failed to resolve username. HTTP: {result.StatusCode}");
}

Expand Down Expand Up @@ -427,7 +427,7 @@ private async Task<bool> ValidateCredentialsWork(GitRequest request, ICredential
var message = "Failed to validate existing credentials using OAuth";
_context.Trace.WriteLine(message);
_context.Trace.WriteException(ex);
_context.Trace2.WriteError(message);
Trace2.WriteError(message);
}
}

Expand All @@ -444,7 +444,7 @@ private async Task<bool> ValidateCredentialsWork(GitRequest request, ICredential
var message = "Failed to validate existing credentials using Basic Auth";
_context.Trace.WriteLine(message);
_context.Trace.WriteException(ex);
_context.Trace2.WriteError(message);
Trace2.WriteError(message);
return false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Atlassian.Bitbucket/BitbucketOAuth2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public BitbucketOAuth2Client(HttpClient httpClient,
OAuth2ServerEndpoints endpoints,
string clientId,
Uri redirectUri,
string clientSecret,
ITrace2 trace2) : base(httpClient, endpoints, clientId, trace2, redirectUri, clientSecret, false)
string clientSecret) : base(httpClient, endpoints, clientId, redirectUri, clientSecret, false)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Atlassian.Bitbucket/Cloud/BitbucketOAuth2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Atlassian.Bitbucket.Cloud
{
public class BitbucketOAuth2Client : Bitbucket.BitbucketOAuth2Client
{
public BitbucketOAuth2Client(HttpClient httpClient, ISettings settings, ITrace2 trace2)
public BitbucketOAuth2Client(HttpClient httpClient, ISettings settings)
: base(httpClient, GetEndpoints(),
GetClientId(settings), GetRedirectUri(settings), GetClientSecret(settings), trace2)
GetClientId(settings), GetRedirectUri(settings), GetClientSecret(settings))
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Atlassian.Bitbucket.DataCenter
{
public class BitbucketOAuth2Client : Bitbucket.BitbucketOAuth2Client
{
public BitbucketOAuth2Client(HttpClient httpClient, ISettings settings, ITrace2 trace2)
public BitbucketOAuth2Client(HttpClient httpClient, ISettings settings)
: base(httpClient, GetEndpoints(settings),
GetClientId(settings), GetRedirectUri(settings), GetClientSecret(settings), trace2)
GetClientId(settings), GetRedirectUri(settings), GetClientSecret(settings))
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Atlassian.Bitbucket/OAuth2ClientRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ protected override void ReleaseManagedResources()
private HttpClient HttpClient => _httpClient ??= _context.HttpClientFactory.CreateClient();

private Cloud.BitbucketOAuth2Client CloudClient =>
_cloudClient ??= new Cloud.BitbucketOAuth2Client(HttpClient, _context.Settings, _context.Trace2);
_cloudClient ??= new Cloud.BitbucketOAuth2Client(HttpClient, _context.Settings);

private DataCenter.BitbucketOAuth2Client DataCenterClient =>
_dataCenterClient ??= new DataCenter.BitbucketOAuth2Client(HttpClient, _context.Settings, _context.Trace2);
_dataCenterClient ??= new DataCenter.BitbucketOAuth2Client(HttpClient, _context.Settings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private async Task<int> ExecuteAsync(Uri url, string userName, bool showOAuth, b

if (!viewModel.WindowResult || viewModel.SelectedMode == AuthenticationModes.None)
{
throw new Trace2Exception(Context.Trace2, "User cancelled dialog.");
throw new Exception("User cancelled dialog.");
}

switch (viewModel.SelectedMode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task BasicAuthentication_GetCredentials_NonDesktopSession_NoTermina

var basicAuth = new BasicAuthentication(context);

await Assert.ThrowsAsync<GitCredentialManager.Trace2InvalidOperationException>(() => basicAuth.GetCredentialsAsync(testResource));
await Assert.ThrowsAsync<InvalidOperationException>(() => basicAuth.GetCredentialsAsync(testResource));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task GetTokenForUserAsync_NoInteraction_ThrowsException()
};
var entraAuth = new EntraAuthentication(context, config);

await Assert.ThrowsAsync<Trace2InvalidOperationException>(
await Assert.ThrowsAsync<InvalidOperationException>(
() => entraAuth.GetTokenForUserAsync(scopes, authority));
}

Expand Down
39 changes: 13 additions & 26 deletions src/Core.Tests/Authentication/OAuth2ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public async Task OAuth2Client_GetAuthorizationCodeAsync()

IOAuth2WebBrowser browser = new TestOAuth2WebBrowser(httpHandler);

var trace2 = new NullTrace2();
OAuth2Client client = CreateClient(httpHandler, endpoints, trace2);
OAuth2Client client = CreateClient(httpHandler, endpoints);

OAuth2AuthorizationCodeResult result = await client.GetAuthorizationCodeAsync(expectedScopes, browser, null, CancellationToken.None);

Expand Down Expand Up @@ -81,12 +80,10 @@ public async Task OAuth2Client_GetAuthorizationCodeAsync_RedirectUrlOriginalStri

var redirectUri = new Uri(expectedRedirectUrl);

var trace2 = new NullTrace2();
OAuth2Client client = new OAuth2Client(
new HttpClient(httpHandler),
endpoints,
TestClientId,
trace2,
redirectUri,
TestClientSecret);

Expand Down Expand Up @@ -131,8 +128,7 @@ public async Task OAuth2Client_GetAuthorizationCodeAsync_ExtraQueryParams()

IOAuth2WebBrowser browser = new TestOAuth2WebBrowser(httpHandler);

var trace2 = new NullTrace2();
OAuth2Client client = CreateClient(httpHandler, endpoints, trace2);
OAuth2Client client = CreateClient(httpHandler, endpoints);

OAuth2AuthorizationCodeResult result = await client.GetAuthorizationCodeAsync(expectedScopes, browser, extraParams, CancellationToken.None);

Expand Down Expand Up @@ -167,8 +163,7 @@ public async Task OAuth2Client_GetAuthorizationCodeAsync_ExtraQueryParams_Overri

IOAuth2WebBrowser browser = new TestOAuth2WebBrowser(httpHandler);

var trace2 = new NullTrace2();
OAuth2Client client = CreateClient(httpHandler, endpoints, trace2);
OAuth2Client client = CreateClient(httpHandler, endpoints);

await Assert.ThrowsAsync<ArgumentException>(() =>
client.GetAuthorizationCodeAsync(expectedScopes, browser, extraParams, CancellationToken.None));
Expand Down Expand Up @@ -207,9 +202,8 @@ public async Task OAuth2Client_GetAuthorizationCodeAsync_NonDefaultResponseMode_

IOAuth2WebBrowser browser = new TestOAuth2WebBrowser(httpHandler);

var trace2 = new NullTrace2();
OAuth2Client client = new OAuth2Client(
new HttpClient(httpHandler), endpoints, TestClientId, trace2,
new HttpClient(httpHandler), endpoints, TestClientId,
TestRedirectUri, TestClientSecret, responseMode: responseMode);

OAuth2AuthorizationCodeResult result = await client.GetAuthorizationCodeAsync(
Expand Down Expand Up @@ -246,9 +240,8 @@ public async Task OAuth2Client_GetAuthorizationCodeAsync_DefaultResponseMode_Omi

IOAuth2WebBrowser browser = new TestOAuth2WebBrowser(httpHandler);

var trace2 = new NullTrace2();
OAuth2Client client = new OAuth2Client(
new HttpClient(httpHandler), endpoints, TestClientId, trace2,
new HttpClient(httpHandler), endpoints, TestClientId,
TestRedirectUri, TestClientSecret, responseMode: OAuth2ResponseMode.Default);

OAuth2AuthorizationCodeResult result = await client.GetAuthorizationCodeAsync(
Expand Down Expand Up @@ -278,8 +271,7 @@ public async Task OAuth2Client_GetDeviceCodeAsync()
server.TokenGenerator.UserCodes.Add(expectedUserCode);
server.TokenGenerator.DeviceCodes.Add(expectedDeviceCode);

var trace2 = new NullTrace2();
OAuth2Client client = CreateClient(httpHandler, endpoints, trace2);
OAuth2Client client = CreateClient(httpHandler, endpoints);

OAuth2DeviceCodeResult result = await client.GetDeviceCodeAsync(expectedScopes, CancellationToken.None);

Expand Down Expand Up @@ -310,8 +302,7 @@ public async Task OAuth2Client_GetTokenByAuthorizationCodeAsync()
server.TokenGenerator.AccessTokens.Add(expectedAccessToken);
server.TokenGenerator.RefreshTokens.Add(expectedRefreshToken);

var trace2 = new NullTrace2();
OAuth2Client client = CreateClient(httpHandler, endpoints, trace2);
OAuth2Client client = CreateClient(httpHandler, endpoints);

var authCodeResult = new OAuth2AuthorizationCodeResult(authCode, TestRedirectUri);
OAuth2TokenResult result = await client.GetTokenByAuthorizationCodeAsync(authCodeResult, CancellationToken.None);
Expand Down Expand Up @@ -348,8 +339,7 @@ public async Task OAuth2Client_GetTokenByRefreshTokenAsync()
server.TokenGenerator.AccessTokens.Add(expectedAccessToken);
server.TokenGenerator.RefreshTokens.Add(expectedRefreshToken);

var trace2 = new NullTrace2();
OAuth2Client client = CreateClient(httpHandler, endpoints, trace2);
OAuth2Client client = CreateClient(httpHandler, endpoints);

OAuth2TokenResult result = await client.GetTokenByRefreshTokenAsync(oldRefreshToken, CancellationToken.None);

Expand Down Expand Up @@ -387,8 +377,7 @@ public async Task OAuth2Client_GetTokenByDeviceCodeAsync()
server.TokenGenerator.AccessTokens.Add(expectedAccessToken);
server.TokenGenerator.RefreshTokens.Add(expectedRefreshToken);

var trace2 = new NullTrace2();
OAuth2Client client = CreateClient(httpHandler, endpoints, trace2);
OAuth2Client client = CreateClient(httpHandler, endpoints);

var deviceCodeResult = new OAuth2DeviceCodeResult(expectedDeviceCode, expectedUserCode, null, null);

Expand Down Expand Up @@ -433,8 +422,7 @@ public async Task OAuth2Client_E2E_InteractiveWebFlowAndRefresh()

IOAuth2WebBrowser browser = new TestOAuth2WebBrowser(httpHandler);

var trace2 = new NullTrace2();
OAuth2Client client = CreateClient(httpHandler, endpoints, trace2);
OAuth2Client client = CreateClient(httpHandler, endpoints);

OAuth2AuthorizationCodeResult authCodeResult = await client.GetAuthorizationCodeAsync(
expectedScopes, browser, null, CancellationToken.None);
Expand Down Expand Up @@ -483,8 +471,7 @@ public async Task OAuth2Client_E2E_DeviceFlowAndRefresh()
server.TokenGenerator.AccessTokens.Add(expectedAccessToken1);
server.TokenGenerator.RefreshTokens.Add(expectedRefreshToken1);

var trace2 = new NullTrace2();
OAuth2Client client = CreateClient(httpHandler, endpoints, trace2);
OAuth2Client client = CreateClient(httpHandler, endpoints);

OAuth2DeviceCodeResult deviceResult = await client.GetDeviceCodeAsync(expectedScopes, CancellationToken.None);

Expand Down Expand Up @@ -517,9 +504,9 @@ public async Task OAuth2Client_E2E_DeviceFlowAndRefresh()
RedirectUris = new[] {TestRedirectUri}
};

private static OAuth2Client CreateClient(HttpMessageHandler httpHandler, OAuth2ServerEndpoints endpoints, ITrace2 trace2, IOAuth2CodeGenerator generator = null)
private static OAuth2Client CreateClient(HttpMessageHandler httpHandler, OAuth2ServerEndpoints endpoints, IOAuth2CodeGenerator generator = null)
{
return new OAuth2Client(new HttpClient(httpHandler), endpoints, TestClientId, trace2, TestRedirectUri, TestClientSecret)
return new OAuth2Client(new HttpClient(httpHandler), endpoints, TestClientId, TestRedirectUri, TestClientSecret)
{
CodeGenerator = generator
};
Expand Down
Loading
Loading