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

Commit 71f7642

Browse files
author
J Wyman
authored
Merge pull request #711 from whoisj/more-bitbucket
bitbucket: fix data loss from token misuse
2 parents 8b8d52e + cb08b24 commit 71f7642

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

Bitbucket.Authentication/Src/OAuth/OAuthAuthenticator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private Token FindAccessToken(string responseText)
323323
&& tokenMatch.Groups.Count > 1)
324324
{
325325
string tokenText = tokenMatch.Groups[1].Value;
326-
return new Token(tokenText, TokenType.Personal);
326+
return new Token(tokenText, TokenType.BitbucketAccess);
327327
}
328328

329329
return null;

Microsoft.Alm.Authentication/Src/Network.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,17 @@ private HttpClient GetHttpClient(TargetUri targetUri, HttpMessageHandler handler
566566
}
567567
break;
568568

569+
case TokenType.Personal:
570+
{
571+
// Personal access tokens are designed to treated like credentials,
572+
// so treat them like credentials.
573+
var credentials = (Credential)token;
574+
575+
// Credentials are packed into the 'Authorization' header as a base64 encoded pair.
576+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials.ToBase64String());
577+
}
578+
break;
579+
569580
default:
570581
Trace.WriteLine("! unsupported token type, not appending an authentication header to the request.");
571582
break;
@@ -602,7 +613,7 @@ private static IWebProxy GetHttpWebProxy(TargetUri targetUri)
602613

603614
if (proxyUri != null)
604615
{
605-
WebProxy proxy = new WebProxy(proxyUri) { UseDefaultCredentials = true };
616+
var proxy = new WebProxy(proxyUri) { UseDefaultCredentials = true };
606617

607618
// check if the user has specified authentications (comes as UserInfo)
608619
if (!string.IsNullOrWhiteSpace(proxyUri.UserInfo) && proxyUri.UserInfo.Length > 1)
@@ -615,7 +626,7 @@ private static IWebProxy GetHttpWebProxy(TargetUri targetUri)
615626
string userName = proxyUri.UserInfo.Substring(0, tokenIndex);
616627
string password = proxyUri.UserInfo.Substring(tokenIndex + 1);
617628

618-
NetworkCredential proxyCreds = new NetworkCredential(userName, password);
629+
var proxyCreds = new NetworkCredential(userName, password);
619630

620631
proxy.UseDefaultCredentials = false;
621632
proxy.Credentials = proxyCreds;

Microsoft.Alm.Authentication/Src/Token.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public static explicit operator Credential(Token token)
306306
if (token is null)
307307
return null;
308308

309-
if (token.Type != TokenType.Personal)
309+
if (token.Type != TokenType.Personal && token.Type != TokenType.BitbucketAccess)
310310
throw new InvalidCastException($"Cannot cast `{nameof(Token)}` of type '{token.Type}' to `{nameof(Credential)}`");
311311

312312
return new Credential("PersonalAccessToken", token._value);

0 commit comments

Comments
 (0)