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

Commit 47e32db

Browse files
author
J Wyman
authored
Merge pull request #727 from Foda/BBOauthBackmerge_1.6
bitbucket: Backmerge 1.17 oauth fix to 1.16
2 parents c03cb81 + 185ef99 commit 47e32db

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

Bitbucket.Authentication.Test/AuthenticationTest.cs

+39
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net;
5+
using System.Net.Http;
56
using System.Threading.Tasks;
67
using Atlassian.Bitbucket.Authentication.BasicAuth;
8+
using Atlassian.Bitbucket.Authentication.Rest;
79
using Microsoft.Alm.Authentication;
810
using Moq;
911
using Xunit;
@@ -573,6 +575,43 @@ public async void VerifyInteractiveLoginAquiresAndStoresValidOAuthCredentials()
573575

574576
}
575577

578+
[Fact]
579+
public async void VerifyGetUserFromRestTargetsValidURL()
580+
{
581+
var expectedUri = new TargetUri("https://api.bitbucket.org/2.0/user");
582+
583+
var storage = new Mock<IStorage>();
584+
var trace = new Mock<Microsoft.Alm.Authentication.Git.ITrace>();
585+
var gitWhere = new Mock<Microsoft.Alm.Authentication.Git.IWhere>();
586+
587+
var network = new Mock<INetwork>();
588+
network
589+
.Setup(a => a.HttpGetAsync(It.IsAny<TargetUri>(), It.IsAny<NetworkRequestOptions>()))
590+
.Returns(Task.FromResult<HttpResponseMessage>(new HttpResponseMessage(HttpStatusCode.Unauthorized)));
591+
592+
network
593+
.Setup(a => a.HttpGetAsync(expectedUri, It.IsAny<NetworkRequestOptions>()))
594+
.Returns(Task.FromResult<HttpResponseMessage>(new HttpResponseMessage(HttpStatusCode.Unauthorized)));
595+
596+
network
597+
.Setup(a => a.HttpGetAsync(It.IsAny<TargetUri>()))
598+
.Returns(Task.FromResult<HttpResponseMessage>(new HttpResponseMessage(HttpStatusCode.Unauthorized)));
599+
600+
var restClient = new RestClient(
601+
new RuntimeContext(storage.Object, network.Object, trace.Object, gitWhere.Object));
602+
603+
var user = await restClient.TryGetUser(
604+
new TargetUri($"https://{_validUsername}@bitbucket.org/"),
605+
42,
606+
new Uri("https://api.bitbucket.org/"),
607+
new Credential(_validUsername, _validPassword));
608+
609+
// Verify we got the same string result
610+
network.Verify(a => a.HttpGetAsync(
611+
It.Is<TargetUri>((uri) => uri.ToString() == expectedUri.ToString()),
612+
It.IsAny<NetworkRequestOptions>()), Times.Once);
613+
}
614+
576615
private bool MockValidAquireAuthenticationOAuthCallback(string title, TargetUri targetUri, AuthenticationResultType resultType, string username)
577616
{
578617
return true;

Bitbucket.Authentication.Test/Bitbucket.Authentication.Test.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
</Reference>
3030
<Reference Include="System" />
3131
<Reference Include="System.Configuration" />
32+
<Reference Include="System.Net.Http" />
3233
<Reference Include="System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
3334
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath>
3435
</Reference>

Bitbucket.Authentication/Authority.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public async Task<bool> ValidateCredentials(TargetUri targetUri, string username
166166
}
167167

168168
// If the basic authentication test failed then try again as OAuth
169-
if (await ValidateCredentials(targetUri, new Token(credentials.Password, TokenType.BitbucketPassword)))
169+
if (await ValidateCredentials(targetUri, new Token(credentials.Password, TokenType.BitbucketAccess)))
170170
{
171171
return true;
172172
}

Bitbucket.Authentication/Rest/RestClient.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public async Task<AuthenticationResult> TryGetUser(TargetUri targetUri, int requ
2626
Authorization = authorization,
2727
Timeout = TimeSpan.FromMilliseconds(requestTimeout),
2828
};
29-
var requestUri = new TargetUri(restRootUrl, targetUri.ProxyUri);
29+
30+
var apiUrl = new Uri(restRootUrl, UserUrl);
31+
var requestUri = new TargetUri(apiUrl, targetUri.ProxyUri);
3032

3133
using (var response = await Network.HttpGetAsync(requestUri, options))
3234
{

Microsoft.Alm.Authentication/Network.cs

+1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ private HttpClient GetHttpClient(TargetUri targetUri, HttpMessageHandler handler
440440
switch (token.Type)
441441
{
442442
case TokenType.AzureAccess:
443+
case TokenType.BitbucketAccess:
443444
{
444445
// ADAL access tokens are packed into the Authorization header.
445446
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Value);

Microsoft.Alm.Authentication/TokenType.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,16 @@ public enum TokenType
6060
[System.ComponentModel.Description("Test-only Token")]
6161
Test = 5,
6262

63-
/// <summary>
64-
/// Bitbucket Password Tokens.
65-
/// </summary>
66-
[System.ComponentModel.Description("Bitbucket Password Token")]
67-
BitbucketPassword = 6,
68-
6963
/// <summary>
7064
/// Bitbucket Access Tokens.
7165
/// </summary>
7266
[System.ComponentModel.Description("Bitbucket Access Token")]
73-
BitbucketAccess = 7,
67+
BitbucketAccess = 6,
7468

7569
/// <summary>
7670
/// Used to auto-refresh Bitbucket Access Tokens.
7771
/// </summary>
7872
[System.ComponentModel.Description("Bitbucket Refresh Token")]
79-
BitbucketRefresh = 8,
73+
BitbucketRefresh = 7,
8074
}
8175
}

0 commit comments

Comments
 (0)