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

Commit ed7e810

Browse files
author
J Wyman
authored
Merge pull request #721 from Foda/BBBasicLoginFixMaster
bitbucket: Fixed basic auth with new username (master)
2 parents 04460d2 + a8fc862 commit ed7e810

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

Bitbucket.Authentication/Src/Authentication.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,12 @@ private Credential GenerateCredentials(TargetUri targetUri, string username,
407407
ref AuthenticationResult result)
408408
{
409409
var credentials = (Credential)result.Token;
410-
var realUsername = GetRealUsername(result.RemoteUsername, username);
411410

412-
if (!targetUri.ContainsUserInfo)
411+
// No user info in Uri, or it's a basic login so we need to personalize the credentials.
412+
if (!targetUri.ContainsUserInfo || result.Token.Type == TokenType.Personal)
413413
{
414414
// No user info in Uri so personalize the credentials.
415+
var realUsername = GetRealUsername(result.RemoteUsername, username);
415416
credentials = new Credential(realUsername, credentials.Password);
416417
}
417418

Bitbucket.Authentication/Src/OAuth/SimpleServer.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ public static async Task<string> WaitForURLAsync(string url, CancellationToken c
5656
var context = await listener.GetContextAsync().RunWithCancellation(cancellationToken);
5757
rawUrl = context.Request.RawUrl;
5858

59+
Thread.Sleep(100); // Wait 100ms without this the server closes before the complete response has been written
60+
5961
// Serve back a simple authentication message.
6062
var html = GetSuccessString();
61-
context.Response.ContentType = "text/html";
62-
context.Response.OutputStream.WriteStringUtf8(html);
63-
64-
Thread.Sleep(100); // Wait 100ms without this the server closes before the complete response has been written
63+
var buffer = System.Text.Encoding.UTF8.GetBytes(html);
64+
context.Response.ContentLength64 = buffer.Length;
65+
Task responseTask = context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length).ContinueWith((task) =>
66+
{
67+
context.Response.OutputStream.Close();
68+
listener.Stop();
69+
});
6570

66-
context.Response.Close();
71+
Thread.Sleep(100);
6772
}
6873
catch (TimeoutException ex)
6974
{

Bitbucket.Authentication/Test/AuthenticationTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,46 @@ public async void VerifyInteractiveLoginDoesNotAquireInvalidBasicAuthCredentials
471471
credentialStore.Verify(c => c.WriteCredentials(It.IsAny<TargetUri>(), It.IsAny<Credential>()), Times.Never);
472472
}
473473

474+
[Fact]
475+
public async void VerifyInteractiveLoginDoesNotAquireInvalidBasicAuthCredentialsWithUsername()
476+
{
477+
var bitbucketUrl = "https://bitbucket.org";
478+
var credentialStore = new Mock<ICredentialStore>();
479+
480+
// mock the result that normally causes issues
481+
var validAuthenticationResult = new AuthenticationResult(AuthenticationResultType.Success)
482+
{
483+
Token = new Token(_validPassword, TokenType.Personal),
484+
RemoteUsername = _validUsername
485+
};
486+
487+
var targetUri = new TargetUri(bitbucketUrl);
488+
489+
// Mock the behaviour of IAuthority.AcquireToken() to basically mimic BasicAuthAuthenticator.GetAuthAsync() validating the useername/password
490+
var authority = new Mock<IAuthority>();
491+
authority
492+
.Setup(a => a.AcquireToken(It.IsAny<TargetUri>(), It.IsAny<Credential>(), It.IsAny<AuthenticationResultType>(), It.IsAny<TokenScope>()))
493+
// return 'success' with the validated credentials
494+
.Returns(Task.FromResult(validAuthenticationResult));
495+
496+
var bbAuth = new Authentication(RuntimeContext.Default, credentialStore.Object,
497+
MockInvalidBasicAuthCredentialsAquireCredentialsCallback, MockValidAquireAuthenticationOAuthCallback, authority.Object);
498+
499+
// perform login with username
500+
var credentials = await bbAuth.InteractiveLogon(targetUri, _validUsername);
501+
502+
Assert.NotNull(credentials);
503+
Assert.Equal(_validUsername, credentials.Username);
504+
Assert.Equal(_validPassword, credentials.Password);
505+
506+
// attempted to validate credentials
507+
authority.Verify(a => a.AcquireToken(It.IsAny<TargetUri>(), It.IsAny<Credential>(), It.IsAny<AuthenticationResultType>(),
508+
It.IsAny<TokenScope>()), Times.Once);
509+
510+
// must have a valid attempt to store the valid credentials
511+
credentialStore.Verify(c => c.WriteCredentials(It.IsAny<TargetUri>(), credentials), Times.Once);
512+
}
513+
474514
[Fact]
475515
public async void VerifyInteractiveLoginDoesNothingIfUserDoesNotEnterCredentials()
476516
{

0 commit comments

Comments
 (0)